class Centaman::Wrapper

Constants

DEFAULT_TIMEOUT_TIME

Attributes

api_password[R]
api_token[R]
api_url[R]
api_username[R]
proxie_host[R]
proxie_password[R]
proxie_port[R]
proxie_user[R]

Public Class Methods

new(args = {}) click to toggle source
# File lib/centaman/wrapper.rb, line 12
def initialize(args = {})
  # required
  @api_username = args.fetch(:api_username) { ENV['CENTAMAN_API_USERNAME'] }
  @api_password = args.fetch(:api_password) { ENV['CENTAMAN_API_PASSWORD'] }
  @api_url = args.fetch(:api_url) { ENV['CENTAMAN_API_URL'] }
  
  # optional
  @api_token = args.fetch(:api_token) { ENV['CENTAMAN_API_TOKEN'] || generate_token }      
  fixie_url = args.fetch(:fixie_url) { ENV['FIXIE_URL'] }
  fixie = fixie_url ? URI.parse(fixie_url) : nil
  @proxie_host = args.fetch(:proxie_host) { fixie.try(:host) }
  @proxie_port = args.fetch(:proxie_port) { fixie.try(:port) }
  @proxie_user = args.fetch(:proxie_user) { fixie.try(:user) }
  @proxie_password = args.fetch(:proxie_password) { fixie.try(:password) }
  after_init(args)
end

Public Instance Methods

after_init(args = {}) click to toggle source
# File lib/centaman/wrapper.rb, line 50
def after_init(args = {})
  # hook method for subclasses
end
generate_token() click to toggle source
# File lib/centaman/wrapper.rb, line 33
def generate_token
  Base64.encode64("#{api_username}:#{api_password}")
end
headers() click to toggle source
# File lib/centaman/wrapper.rb, line 29
def headers
  { 'authorization' => "Basic #{api_token}", 'Content-Type' => 'application/json' }
end
options() click to toggle source
# File lib/centaman/wrapper.rb, line 37
def options
  [] # overwritten by children
end
options_hash() click to toggle source
# File lib/centaman/wrapper.rb, line 41
def options_hash
  hash = {}
  options.each do |option_hash|
    next unless option_hash[:value].present?
    hash[option_hash[:key]] = option_hash[:value]
  end
  hash
end
payload(request_type = :get) click to toggle source
# File lib/centaman/wrapper.rb, line 54
def payload(request_type = :get)
  hash = { payload_key(request_type) => options_hash }
  hash.merge(headers: headers).merge(proxy_hash)
end
payload_key(request_type) click to toggle source
# File lib/centaman/wrapper.rb, line 59
def payload_key(request_type)
  request_type == :get ? :query : :body
end
proxy_hash() click to toggle source
# File lib/centaman/wrapper.rb, line 63
def proxy_hash
  return {} unless proxie_host
  {
    http_proxyaddr: proxie_host,
    http_proxyport: proxie_port,
    http_proxyuser: proxie_user,
    http_proxypass: proxie_password,
  }
end
wrap_request_in_case_of_timeout(proc, options = {}) click to toggle source
# File lib/centaman/wrapper.rb, line 73
def wrap_request_in_case_of_timeout(proc, options = {})
  time = options.fetch(:timeout_time, DEFAULT_TIMEOUT_TIME)
  resp = nil
  begin
    resp = Timeout.timeout(time) do
      proc.call
    end
  rescue Timeout::Error
    p "*** CENTAMAN GEM TIMEOUT ***"
    raise Exceptions::CentamanTimeout
  end
  resp
end