class ChimeraHttpClient::Base

Constants

USER_AGENT

Public Class Methods

new(options = {}) click to toggle source
# File lib/chimera_http_client/base.rb, line 5
def initialize(options = {})
  fail(ChimeraHttpClient::ParameterMissingError, "base_url expected, but not given") if options[:base_url].nil?

  @base_url = options.fetch(:base_url)
  @deserializer = default_deserializer.merge(options.fetch(:deserializer, {}))
  @logger = options[:logger]
  @monitor = options[:monitor]
  @timeout = options[:timeout]

  Typhoeus::Config.cache = options[:cache]
  Typhoeus::Config.memoize = false # hydra setting, prevents a possible memory leak
  Typhoeus::Config.user_agent = options.fetch(:user_agent, USER_AGENT)
  Typhoeus::Config.verbose = options.fetch(:verbose, false)
end

Private Instance Methods

augmented_options(options) click to toggle source

Add default values to call options

# File lib/chimera_http_client/base.rb, line 23
def augmented_options(options)
  options[:timeout] ||= @timeout

  options
end
default_deserializer() click to toggle source
# File lib/chimera_http_client/base.rb, line 47
def default_deserializer
  { error: ::ChimeraHttpClient::Deserializer.json_error, response: ::ChimeraHttpClient::Deserializer.json_response }
end
default_headers() click to toggle source
# File lib/chimera_http_client/base.rb, line 43
def default_headers
  { "Content-Type" => "application/json" }
end
extract_body(options) click to toggle source
# File lib/chimera_http_client/base.rb, line 29
def extract_body(options)
  body = options.delete(:body)
  body_optional = options.delete(:body_optional)

  fail(ChimeraHttpClient::ParameterMissingError, "body expected, but not given") if body.nil? && !body_optional

  body
end
extract_headers(options, headers) click to toggle source
# File lib/chimera_http_client/base.rb, line 38
def extract_headers(options, headers)
  given_headers = options.delete(:headers) || {}
  headers.merge(given_headers)
end
trim(element) click to toggle source

Remove leading and trailing “/” from a give part of a String (usually URL or endpoint)

# File lib/chimera_http_client/base.rb, line 58
def trim(element)
  element.to_s.sub(%r{^/}, "").chomp("/")
end
url(endpoint) click to toggle source

Build URL out of @base_url and endpoint given as String or Array, while trimming redundant “/”

# File lib/chimera_http_client/base.rb, line 52
def url(endpoint)
  trimmed_endpoint = Array(endpoint).map { |e| trim(e) }
  [@base_url.chomp("/"), trimmed_endpoint].flatten.reject(&:empty?).join("/")
end