class Aviator::Service

Manages a service

Attributes

default_session_data[RW]
provider[R]
service[R]

Public Class Methods

new(opts={}) click to toggle source
# File lib/aviator/core/service.rb, line 69
def initialize(opts={})
  @provider = opts[:provider] || (raise ProviderNotDefinedError.new)
  @service  = opts[:service]  || (raise ServiceNameNotDefinedError.new)
  @log_file = opts[:log_file]
  @default_options = opts[:default_options] || {}

  @default_session_data = opts[:default_session_data]

  load_requests
end

Public Instance Methods

request(request_name, options={}, ¶ms) click to toggle source

No longer recommended for public use. Use Aviator::Session#request instead

# File lib/aviator/core/service.rb, line 83
def request(request_name, options={}, &params)
  if options[:api_version].nil? && @default_options[:api_version]
    options[:api_version] = @default_options[:api_version]
  end

  session_data = options[:session_data] || default_session_data

  raise SessionDataNotProvidedError.new(@service, request_name) unless session_data

  [:base_url].each do |k|
    session_data[k] = options[k] if options[k]
  end

  request_class = provider_module.find_request(service, request_name, session_data, options)

  raise UnknownRequestError.new(request_name, options) unless request_class

  # Always use :params over &params if provided
  if options[:params]
    params = lambda do |params|
      options[:params].each do |key, value|
        begin
          params[key] = value
        rescue NameError => e
          raise NameError.new("Unknown param name '#{key}'")
        end
      end
    end
  end

  request  = request_class.new(session_data, &params)

  response = http_connection.send(request.http_method) do |r|
    r.url        request.url
    r.headers.merge!(request.headers)        if request.headers?
    r.query    = request.querystring         if request.querystring?
    r.body     = JSON.generate(request.body) if request.body?
  end

  Aviator::Response.send(:new, response, request)
end
request_classes() click to toggle source
# File lib/aviator/core/service.rb, line 126
def request_classes
  @request_classes
end