class ApiRecipes::Api

Constants

BASE_CONFIGS_KEYS

Attributes

authorization[RW]
base_configs[R]
basic_auth[RW]
configs[RW]
name[RW]
object[R]

Public Class Methods

new(name, configs, object) click to toggle source
# File lib/api_recipes/api.rb, line 9
def initialize(name, configs, object)
  @name = name
  @configs = ApiRecipes::Settings::DEFAULT.merge configs
  @object = object

  # Generate   some_api.some_endpoint  methods
  # e.g.  github.users
  endpoints = @configs[:endpoints]
  if endpoints && endpoints.is_a?(Hash)
    endpoints.each do |ep_name, params|
      unless respond_to? ep_name
        # TODO: store endpoints, routes and routes_urls
        define_singleton_method ep_name do |*request_params, &block|
          # puts "API params: #{params}"
          Endpoint.new api: self, name: ep_name, path: path, params: params, request_params: request_params, &block
        end
      end
    end
  end
end

Public Instance Methods

authorization=(auth) click to toggle source
# File lib/api_recipes/api.rb, line 30
def authorization=(auth)
  @authorization = auth

  # Check if I'm the global api
  if global?
    # Set authorization also on every "children" (classes that define the same api)
    ApiRecipes.set_authorization_for_api auth, name
  end
end
basic_auth=(auth) click to toggle source
# File lib/api_recipes/api.rb, line 40
def basic_auth=(auth)
  @basic_auth = auth

  # Check if I'm the global api
  if global?
    # Set authorization also on every "children" (classes that define the same api)
    ApiRecipes.set_basic_auth_for_api auth, name
  end
end

Private Instance Methods

check_route_name_does_not_clash(route_name) click to toggle source
# File lib/api_recipes/api.rb, line 56
def check_route_name_does_not_clash(route_name)
  # Check if a method named route_name has already been defined on this object
  if respond_to? route_name
    raise RouteNameClashWithExistentMethod.new(@name, route_name)
  end
end
global?() click to toggle source
# File lib/api_recipes/api.rb, line 63
def global?
  ApiRecipes._aprcps_global_storage[name] == self
end
path() click to toggle source
# File lib/api_recipes/api.rb, line 67
def path
  "/#{configs[:base_url]}/"
end