class Marvelite::API::Router

Attributes

api_version[RW]

Public Class Methods

new(attrs = {}) click to toggle source
# File lib/marvelite/api/router.rb, line 7
def initialize(attrs = {})
  @api_version = attrs.fetch(:api_version) { 'v1' }
  @routes = {}
  load_routes_from_file("#{api_version}.yml")
end

Public Instance Methods

add_route(name, endpoint) click to toggle source
# File lib/marvelite/api/router.rb, line 29
def add_route(name, endpoint)
  routes["#{name}_path".to_sym] = { :name => name, :endpoint => endpoint }
end
inspect() click to toggle source
# File lib/marvelite/api/router.rb, line 13
def inspect
  "#<#{self.class}:#{self.object_id}>"
end
load_routes_from_file(filename) click to toggle source
# File lib/marvelite/api/router.rb, line 17
def load_routes_from_file(filename)
  file_path =  File.join(File.dirname(__FILE__), "config", filename)
  f = YAML.load(File.read(file_path))
  f.each do |_, route|
    add_route(route['name'], route['path'])
  end
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/marvelite/api/router.rb, line 33
def method_missing(method, *args, &block)
  if routes.keys.include?(method)
    endpoint = "#{routes[method][:endpoint]}"
    params = *args
    if params.any?
      params[0].each do |p_key, p_value|
        endpoint.gsub!(":#{p_key.to_s}", p_value.to_s)
      end
    end
    "/#{api_version}#{endpoint}"
  else
    super
  end
end
routes() click to toggle source
# File lib/marvelite/api/router.rb, line 25
def routes
  @routes
end