module ApiStruct::Extensions::ApiClient

Constants

REJECTED_METHODS

Attributes

clients[R]

Public Instance Methods

client_service(*services, **options) click to toggle source
# File lib/api_struct/extensions/api_client.rb, line 10
def client_service(*services, **options)
  @clients ||= {}
  services.each { |service| register_service(service, options) }
end

Private Instance Methods

allowed_methods(service, options) click to toggle source
# File lib/api_struct/extensions/api_client.rb, line 36
def allowed_methods(service, options)
  return Array(options[:only]) if options[:only]
  rejected = REJECTED_METHODS.concat(Array(options[:except]))
  service.instance_methods(false).reject { |method| rejected.include?(method) }
end
define_client_method(method, options) click to toggle source
# File lib/api_struct/extensions/api_client.rb, line 25
def define_client_method(method, options)
  method_name = options[:prefix] ? [options[:prefix], method].join('_') : method
  define_singleton_method method_name do |*args|
    from_monad(clients[options[:client_key]].new.send(method, *args))
  end
end
prefix_from_class(klass) click to toggle source
# File lib/api_struct/extensions/api_client.rb, line 32
def prefix_from_class(klass)
  underscore(klass.name.split('::').last).to_sym
end
register_service(service, options) click to toggle source
# File lib/api_struct/extensions/api_client.rb, line 17
def register_service(service, options)
  options[:prefix] = prefix_from_class(service) if options[:prefix] == true
  options[:client_key] = options[:prefix] || :base

  @clients[options[:client_key]] = service
  allowed_methods(service, options).each { |method| define_client_method(method, options) }
end