Class: Brut::FrontEnd::Routing::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/brut/front_end/routing.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, path_template) ⇒ Route

Returns a new instance of Route.



195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/brut/front_end/routing.rb', line 195

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_classObject (readonly)

Returns the value of attribute handler_class.



193
194
195
# File 'lib/brut/front_end/routing.rb', line 193

def handler_class
  @handler_class
end

#http_methodObject (readonly)

Returns the value of attribute http_method.



193
194
195
# File 'lib/brut/front_end/routing.rb', line 193

def http_method
  @http_method
end

#path_templateObject (readonly)

Returns the value of attribute path_template.



193
194
195
# File 'lib/brut/front_end/routing.rb', line 193

def path_template
  @path_template
end

Instance Method Details

#==(other) ⇒ Object



270
271
272
# File 'lib/brut/front_end/routing.rb', line 270

def ==(other)
  self.method == other.method && self.path == other.path
end

#path(**query_string_params) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/brut/front_end/routing.rb', line 218

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_paramsObject



208
209
210
211
212
213
214
215
216
# File 'lib/brut/front_end/routing.rb', line 208

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



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/brut/front_end/routing.rb', line 251

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