Class: Sinatra::URL::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/sinatra/url.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Route

Returns a new instance of Route.



31
32
33
34
# File 'lib/sinatra/url.rb', line 31

def initialize(path)
  @path = path
  @pattern, @keys = compile(path)
end

Instance Method Details

#call(params) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sinatra/url.rb', line 44

def call(params)
  params[:splat] = Array(params[:splat])
  url = @path.dup

  keys.each do |key|
    match = key == "splat" ? "*" : ":#{key}"
    value = key == "splat" ? params[:splat].shift : params[key.to_sym]
    replacement = to_param(value)

    unless url.sub! match, replacement
      get_params[key] = params[key.to_sym]
    end
  end

  get_params = {}
  params.each do |key, value|
    get_params[key] = to_param(value) unless
      key == :splat || keys.include?(key.to_s)
  end

  url << "?" + Rack::Utils.build_query(get_params) unless get_params.empty?
  url
end

#keysObject



40
41
42
# File 'lib/sinatra/url.rb', line 40

def keys
  @keys
end

#match(str) ⇒ Object



36
37
38
# File 'lib/sinatra/url.rb', line 36

def match(str)
  @pattern.match(str)
end