class Sports::Butler::Base

Constants

ALIASES
AVAILABLE_ENDPOINTS

Attributes

api_class[RW]
api_name[RW]
available_endpoints[RW]
endpoints[RW]
sport[RW]
sport_class[RW]
valid_configuration[RW]

Public Class Methods

new(sport:, api_name:) click to toggle source
# File lib/sports/butler/base.rb, line 64
def initialize(sport:, api_name:)
  @sport        = sport
  @api_name     = api_name
  @api_class    = api_name.to_s.camelize
  @sport_class  = "#{sport.to_s.camelize}Api"

  @endpoints    = {}

  @available_endpoints  = get_available_endpoints
  @valid_configuration  = !Configuration.invalid_config?(sport, api_name)
end

Public Instance Methods

method_missing(method, *args, &block) click to toggle source
# File lib/sports/butler/base.rb, line 76
def method_missing(method, *args, &block)
  endpoint_name = eval_endpoint_name(method)
  if endpoint_name.present?
    @endpoints[method].present? ?
      @endpoints[method] : @endpoints[method] = build_endpoint_classes(endpoint_name)
  else
    raise MissingEndpoint, endpoint_not_available(method)
  end
end

Private Instance Methods

build_api_object() click to toggle source
# File lib/sports/butler/base.rb, line 117
def build_api_object
  Api.new(sport, api_name)
end
build_endpoint_classes(name) click to toggle source
# File lib/sports/butler/base.rb, line 121
def build_endpoint_classes(name)
  "#{scope}::#{name}".constantize.new(sport: sport, api_name: api_name, api: build_api_object)
end
endpoint_not_available(name) click to toggle source
# File lib/sports/butler/base.rb, line 125
def endpoint_not_available(name)
  "NOT AVAILABLE: the endpoint '#{name}' is not available for this sport/api combination."
end
eval_endpoint_name(endpoint_name) click to toggle source
# File lib/sports/butler/base.rb, line 108
def eval_endpoint_name(endpoint_name)
  endpoint = ALIASES[endpoint_name].present? ? ALIASES[endpoint_name] : endpoint_name
  Kernel.const_defined?( "#{scope}::#{endpoint.to_s.camelize}") ? endpoint.to_s.camelize : nil
end
get_available_endpoints() click to toggle source
# File lib/sports/butler/base.rb, line 88
def get_available_endpoints
  result = {}.with_indifferent_access

  AVAILABLE_ENDPOINTS[sport][api_name].each do |path|
    next if File.basename(path, ".*") == 'base'

    endpoint = File.basename(path, ".*")
    endpoint_methods = self.send(endpoint).available_endpoint_methods

    result_endpoints = {}.with_indifferent_access
    endpoint_methods.each do |endpoint_method|
      result_endpoints[endpoint_method] = self.send(endpoint).method(endpoint_method).parameters
    end

    result[endpoint] = result_endpoints
  end

  result
end
scope() click to toggle source
# File lib/sports/butler/base.rb, line 113
def scope
  "Sports::Butler::#{sport_class}::#{api_class}"
end