Module: Otto::Core::UriGenerator

Included in:
Otto
Defined in:
lib/otto/core/uri_generator.rb

Overview

URI generation module providing path and URL generation for route definitions

Instance Method Summary collapse

Instance Method Details

#uri(route_definition, params = {}) ⇒ Object

Return the URI path for the given route_definition e.g.

Otto.default.path 'YourClass.somemethod'  #=> /some/path


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/otto/core/uri_generator.rb', line 16

def uri(route_definition, params = {})
  # raise RuntimeError, "Not working"
  route = @route_definitions[route_definition]
  return if route.nil?

  local_params = params.clone
  local_path   = route.path.clone

  keys_to_remove = []
  local_params.each_pair do |k, v|
    next unless local_path.match(":#{k}")

    local_path.gsub!(":#{k}", v.to_s)
    keys_to_remove << k
  end
  keys_to_remove.each { |k| local_params.delete(k) }

  uri = URI::HTTP.new(nil, nil, nil, nil, nil, local_path, nil, nil, nil)
  unless local_params.empty?
    query_string = local_params.map do |k, v|
      "#{URI.encode_www_form_component(k)}=#{URI.encode_www_form_component(v)}"
    end.join('&')
    uri.query = query_string
  end
  uri.to_s
end