class ContactData::Fetcher

Constants

API
LOGLEVEL
URL

Attributes

api_method[R]
http_method[R]
options[R]

Public Class Methods

new(h, a, o = {}) click to toggle source
# File lib/contact-data/fetcher.rb, line 39
def initialize(h, a, o = {})
  @http_method = h
  @api_method = a
  @options = o
  set_log_level
end

Public Instance Methods

parsed_json() click to toggle source
# File lib/contact-data/fetcher.rb, line 25
def parsed_json
  logger.debug { "Parsing #{json.length} characters of JSON" }
  JSON.parse(json, symbolize_names: true, allow_nan: true)
end
url() click to toggle source
# File lib/contact-data/fetcher.rb, line 30
def url
  return @url if @url
  @url = api_method.is_a?(String) ? "#{url_base}/#{api_method}" : "#{api_base}/#{method_base}#{api_method}"
  @url = "#{@url}.#{options[:format] || :json}" unless options[:noformat]
  @url
end

Private Instance Methods

api_base() click to toggle source
# File lib/contact-data/fetcher.rb, line 50
def api_base
  @api_base ||= "#{url_base}/#{options[:api_base] || API}"
end
args() click to toggle source
# File lib/contact-data/fetcher.rb, line 71
def args
  return @args if @args

  @args = { url: url, method: http_method }
  @args[:headers] = { params: options[:params] } if options.key? :params

  [
    :method, :url, :headers, :cookies, :payload, :user, :password, :timeout, :max_redirects, :open_timeout,
    :raw_response, :processed_headers, :ssl_opts, :verify_ssl, :ssl_client_cert, :ssl_client_key, :ssl_ca_file,
    :ssl_ca_path, :ssl_cert_store, :ssl_verify_callback, :ssl_verify_callback_warnings, :ssl_version, :ssl_ciphers
  ].each { |key| @args[key] = options[key] if options.key? key } # TODO: surely this is just a merge?

  @args
end
display_method() click to toggle source
# File lib/contact-data/fetcher.rb, line 58
def display_method
  @display_method ||= http_method.to_s.upcase
end
json() click to toggle source
# File lib/contact-data/fetcher.rb, line 62
def json
  return @json if @json
  logger.debug { "Using #{display_method} for #{url}" }
  @json = RestClient::Request.new(args).execute
rescue RestClient::Exception, SocketError, Net::HTTPBadGateway => e
  message = "#{e.message} when trying to #{display_method} url: #{url} with options #{options}"
  raise ContactData::FetchError, message, e.backtrace
end
logger() click to toggle source
# File lib/contact-data/fetcher.rb, line 90
def logger
  @logger ||= Logger.new(STDOUT)
end
method_base() click to toggle source
# File lib/contact-data/fetcher.rb, line 54
def method_base
  @method_base ||= (options.key?(:base) ? "#{options[:base]}/" : '')
end
set_log_level() click to toggle source
# File lib/contact-data/fetcher.rb, line 94
def set_log_level
  logger.level = verbose? ? Logger::INFO : LOGLEVEL
end
url_base() click to toggle source
# File lib/contact-data/fetcher.rb, line 46
def url_base
  @url_base ||= (options[:url_base] || URL)
end
verbose?() click to toggle source
# File lib/contact-data/fetcher.rb, line 86
def verbose?
  options[:verbose]
end