Class: Centaman::Wrapper

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/centaman/wrapper.rb

Overview

:nodoc:

Direct Known Subclasses

Service

Constant Summary collapse

DEFAULT_TIMEOUT_TIME =
15

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Wrapper

Returns a new instance of Wrapper.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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

Instance Attribute Details

#api_passwordObject (readonly)

Returns the value of attribute api_password.



9
10
11
# File 'lib/centaman/wrapper.rb', line 9

def api_password
  @api_password
end

#api_tokenObject (readonly)

Returns the value of attribute api_token.



9
10
11
# File 'lib/centaman/wrapper.rb', line 9

def api_token
  @api_token
end

#api_urlObject (readonly)

Returns the value of attribute api_url.



9
10
11
# File 'lib/centaman/wrapper.rb', line 9

def api_url
  @api_url
end

#api_usernameObject (readonly)

Returns the value of attribute api_username.



9
10
11
# File 'lib/centaman/wrapper.rb', line 9

def api_username
  @api_username
end

#proxie_hostObject (readonly)

Returns the value of attribute proxie_host.



9
10
11
# File 'lib/centaman/wrapper.rb', line 9

def proxie_host
  @proxie_host
end

#proxie_passwordObject (readonly)

Returns the value of attribute proxie_password.



9
10
11
# File 'lib/centaman/wrapper.rb', line 9

def proxie_password
  @proxie_password
end

#proxie_portObject (readonly)

Returns the value of attribute proxie_port.



9
10
11
# File 'lib/centaman/wrapper.rb', line 9

def proxie_port
  @proxie_port
end

#proxie_userObject (readonly)

Returns the value of attribute proxie_user.



9
10
11
# File 'lib/centaman/wrapper.rb', line 9

def proxie_user
  @proxie_user
end

Instance Method Details

#after_init(args = {}) ⇒ Object



50
51
52
# File 'lib/centaman/wrapper.rb', line 50

def after_init(args = {})
  # hook method for subclasses
end

#generate_tokenObject



33
34
35
# File 'lib/centaman/wrapper.rb', line 33

def generate_token
  Base64.encode64("#{api_username}:#{api_password}")
end

#headersObject



29
30
31
# File 'lib/centaman/wrapper.rb', line 29

def headers
  { 'authorization' => "Basic #{api_token}", 'Content-Type' => 'application/json' }
end

#optionsObject



37
38
39
# File 'lib/centaman/wrapper.rb', line 37

def options
  [] # overwritten by children
end

#options_hashObject



41
42
43
44
45
46
47
48
# 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) ⇒ Object



54
55
56
57
# 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) ⇒ Object



59
60
61
# File 'lib/centaman/wrapper.rb', line 59

def payload_key(request_type)
  request_type == :get ? :query : :body
end

#proxy_hashObject



63
64
65
66
67
68
69
70
71
# 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 = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# 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