Module: RViki::Routable::ClassMethods

Defined in:
lib/rviki/routable.rb

Instance Method Summary collapse

Instance Method Details

#route_get(route_pattern, args) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rviki/routable.rb', line 14

def route_get(route_pattern, args)
  param_keys = route_pattern.scan(/:\w+/).map { |key| key[1..-1].to_sym }
  api_name = args[:as]
  routes[api_name] = [ route_pattern, param_keys ]

  define_method(api_name) do |params={}|
    request_path = route_pattern.dup
    param_keys.each do |param_key|
      if (value = params[param_key])
        request_path.gsub!(":#{param_key}", value.to_s)
        params.delete(param_key)
      end
    end
    self.class.get(request_path, query: params)
  end
end

#routesObject



10
11
12
# File 'lib/rviki/routable.rb', line 10

def routes
  @routes ||= {}
end