class Galactus::Router

Attributes

api_version[RW]
routes[R]

Public Class Methods

new(settings = {}) click to toggle source
# File lib/galactus/router.rb, line 8
def initialize(settings = {})
  @routes = {}
  @api_version = settings[:api_version] || 'v1'
  load_api_routes("#{api_version}.yml")
end

Private Instance Methods

add_api_route(name, path) click to toggle source
# File lib/galactus/router.rb, line 25
def add_api_route(name, path)
  routes["#{name}_path".to_sym] = { name: name, endpoint: path }
end
check_for_api_file(file) click to toggle source
# File lib/galactus/router.rb, line 29
def check_for_api_file(file)
  script_path = current_script_path
  file_path = File.join(script_path, 'config', file)
  return file_path if File.exist?(file_path)
  file_path = File.join(File.dirname(__FILE__), 'config', file)
  file_path if File.exist?(file_path)
end
current_script_path() click to toggle source
# File lib/galactus/router.rb, line 37
def current_script_path
  File.expand_path(File.dirname(File.expand_path $PROGRAM_NAME))
end
load_api_routes(file) click to toggle source
# File lib/galactus/router.rb, line 16
def load_api_routes(file)
  api_routes_path = check_for_api_file(file)
  contents = YAML.load(File.read(api_routes_path))

  contents.each do |_route_id, route_info|
    add_api_route(route_info['name'], route_info['path'])
  end
end
method_missing(method, *args) click to toggle source
# File lib/galactus/router.rb, line 41
def method_missing(method, *args)
  return unless 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}", p_value.to_s)
    end
  end

  "/#{api_version}#{endpoint}"
end