class Dialers::Caller

Constants

IDEMPOTENT_AND_SAFE_METHODS
MAX_RETRIES

Public Class Methods

setup_api(*args, &block) click to toggle source

Setups a connection using {github.com/lostisland/faraday Faraday}.

@param [Array] Arguments to pass to the faraday connection. @yield A block to pass to the faraday connection

@return [Faraday::Connection] a connection

# File lib/dialers/caller.rb, line 13
def setup_api(*args, &block)
  api = Faraday.new(*args) { |faraday| block.call(faraday) }
  const_set "API", api
end
short_circuits() click to toggle source

@return [ShortCircuitsCollection] a collection of short circuits that can stop the process.

# File lib/dialers/caller.rb, line 19
def short_circuits
  @short_circuits ||= Dialers::ShortCircuitsCollection.new
end

Private Class Methods

body_holder_request_method(http_method) click to toggle source

@!macro [attach] body_holder_request_method

@method $1
Make a $1 request.

@param url [String] The path for the request.
@param payload [Hash] The request body.
@param headers [Hash] The headers.

@return [Transformable] a transformable object
# File lib/dialers/caller.rb, line 55
def body_holder_request_method(http_method)
  define_method(http_method) do |url, payload = {}, headers = {}|
    options = RequestOptions.new
    options.url = url
    options.http_method = http_method
    options.payload = payload
    options.headers = headers

    transform(http_call(options))
  end
end
query_holder_request_method(http_method) click to toggle source

@!macro [attach] query_holder_request_method

@method $1
Make a $1 request.

@param url [String] The path for the request.
@param params [Hash] The query params to attach to the url.
@param headers [Hash] The headers.

@return [Transformable] a transformable object
# File lib/dialers/caller.rb, line 34
def query_holder_request_method(http_method)
  define_method(http_method) do |url, params = {}, headers = {}|
    options = RequestOptions.new
    options.url = url
    options.http_method = http_method
    options.query_params = params
    options.headers = headers

    transform(http_call(options))
  end
end

Private Instance Methods

api() click to toggle source
# File lib/dialers/caller.rb, line 114
def api
  @api ||= get_api
end
call_api(request_options) click to toggle source
# File lib/dialers/caller.rb, line 103
def call_api(request_options)
  api.public_send(
    request_options.http_method, request_options.url, request_options.query_params || {}
  ) do |request|
    request.body = request_options.payload
    (request_options.headers || {}).each do |key, value|
      request.headers[key] = value
    end
  end
end
get_api() click to toggle source
# File lib/dialers/caller.rb, line 118
def get_api
  self.class::API
rescue NameError
  raise Dialers::InexistentApiError.new(self.class)
end
http_call(request_options, current_retries = 0) click to toggle source
# File lib/dialers/caller.rb, line 85
def http_call(request_options, current_retries = 0)
  call_api(request_options)
rescue Faraday::ParsingError => exception
  raise Dialers::ParsingError.new(exception)
rescue Faraday::ConnectionFailed => exception
  raise Dialers::UnreachableError.new(exception)
rescue Faraday::TimeoutError => exception
  retry_call(request_options, exception, current_retries)
end
idempotent_and_safe_method?(http_method) click to toggle source
# File lib/dialers/caller.rb, line 124
def idempotent_and_safe_method?(http_method)
  IDEMPOTENT_AND_SAFE_METHODS.include?(http_method)
end
retry_call(request_options, exception, current_retries) click to toggle source
# File lib/dialers/caller.rb, line 95
def retry_call(request_options, exception, current_retries)
  if idempotent_and_safe_method?(request_options.http_method) && current_retries <= MAX_RETRIES
    http_call(request_options, current_retries + 1)
  else
    fail Dialers::UnreachableError.new(exception)
  end
end
transform(response) click to toggle source
# File lib/dialers/caller.rb, line 80
def transform(response)
  self.class.short_circuits.search_for_stops(response)
  Dialers::Transformable.new(response)
end