Class: Brut::FrontEnd::Routing::Route
- Inherits:
-
Object
- Object
- Brut::FrontEnd::Routing::Route
- Defined in:
- lib/brut/front_end/routing.rb
Direct Known Subclasses
FormHandlerRoute, FormRoute, MissingHandler, MissingPage, MissingPath, PageRoute
Instance Attribute Summary collapse
-
#handler_class ⇒ Object
readonly
Returns the value of attribute handler_class.
-
#http_method ⇒ Object
readonly
Returns the value of attribute http_method.
-
#path_template ⇒ Object
readonly
Returns the value of attribute path_template.
Instance Method Summary collapse
-
#==(other) ⇒ Object
-
#initialize(method, path_template) ⇒ Route
constructor
A new instance of Route.
-
#path(**query_string_params) ⇒ Object
-
#path_params ⇒ Object
-
#url(**query_string_params) ⇒ Object
Constructor Details
#initialize(method, path_template) ⇒ Route
Returns a new instance of Route.
173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/brut/front_end/routing.rb', line 173 def initialize(method,path_template) http_method = Brut::FrontEnd::HttpMethod.new(method) if ![:get, :post].include?(http_method.to_sym) raise ArgumentError,"Only GET and POST are supported. '#{method}' is not" end if path_template !~ /^\// raise ArgumentError,"Routes must start with a slash: '#{path_template}'" end @http_method = http_method @path_template = path_template @handler_class = self.locate_handler_class(self.suffix,self.preposition) end |
Instance Attribute Details
#handler_class ⇒ Object (readonly)
Returns the value of attribute handler_class.
171 172 173 |
# File 'lib/brut/front_end/routing.rb', line 171 def handler_class @handler_class end |
#http_method ⇒ Object (readonly)
Returns the value of attribute http_method.
171 172 173 |
# File 'lib/brut/front_end/routing.rb', line 171 def http_method @http_method end |
#path_template ⇒ Object (readonly)
Returns the value of attribute path_template.
171 172 173 |
# File 'lib/brut/front_end/routing.rb', line 171 def path_template @path_template end |
Instance Method Details
#==(other) ⇒ Object
248 249 250 |
# File 'lib/brut/front_end/routing.rb', line 248 def ==(other) self.method == other.method && self.path == other.path end |
#path(**query_string_params) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/brut/front_end/routing.rb', line 196 def path(**query_string_params) anchor = query_string_params.delete(:anchor) || query_string_params.delete("anchor") path = @path_template.split(/\//).map { |path_part| if path_part =~ /^:(.+)$/ param_name = $1.to_sym if !query_string_params.key?(param_name) raise Brut::Framework::Errors::MissingParameter.new( param_name, params_received: query_string_params.keys, context: ":#{param_name} was used as a path parameter for #{@handler_class} (path '#{@path_template}')" ) end query_string_params.delete(param_name) else path_part end } joined_path = path.join("/") if joined_path == "" joined_path = "/" end if anchor joined_path = joined_path + "#" + URI.encode_uri_component(anchor) end uri = URI(joined_path) query_string = URI.encode_www_form(query_string_params) if query_string.to_s.strip != "" uri.query = query_string end uri.extend(Phlex::SGML::SafeObject) end |
#path_params ⇒ Object
186 187 188 189 190 191 192 193 194 |
# File 'lib/brut/front_end/routing.rb', line 186 def path_params @path_template.split(/\//).map { |path_part| if path_part =~ /^:(.+)$/ $1.to_sym else nil end }.compact end |
#url(**query_string_params) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/brut/front_end/routing.rb', line 229 def url(**query_string_params) request_context = Brut::FrontEnd::RequestContext.current path = self.path(**query_string_params) host = if request_context request_context[:host] end host ||= Brut.container.fallback_host host.merge(path) rescue ArgumentError => ex request_context_note = if request_context "the RequestContext did not contain request.host, which is unusual" else "the RequestContext was not available (likely due to calling `full_routing` outside an HTTP request)" end raise Brut::Framework::Errors::MissingConfiguration( :fallback_host, "Attempting to get the full URL for route #{self.path_template}, #{request_context_note}, and Brut.container.fallback_host was not set. You must set this value if you expect to generate complete URLs outside of an HTTP request") end |