Class: Centaman::Wrapper
- Inherits:
-
Object
- Object
- Centaman::Wrapper
- Includes:
- HTTParty
- Defined in:
- lib/centaman/wrapper.rb
Overview
:nodoc:
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_TIMEOUT_TIME =
15
Instance Attribute Summary collapse
-
#api_password ⇒ Object
readonly
Returns the value of attribute api_password.
-
#api_token ⇒ Object
readonly
Returns the value of attribute api_token.
-
#api_url ⇒ Object
readonly
Returns the value of attribute api_url.
-
#api_username ⇒ Object
readonly
Returns the value of attribute api_username.
-
#proxie_host ⇒ Object
readonly
Returns the value of attribute proxie_host.
-
#proxie_password ⇒ Object
readonly
Returns the value of attribute proxie_password.
-
#proxie_port ⇒ Object
readonly
Returns the value of attribute proxie_port.
-
#proxie_user ⇒ Object
readonly
Returns the value of attribute proxie_user.
Instance Method Summary collapse
- #after_init(args = {}) ⇒ Object
- #generate_token ⇒ Object
- #headers ⇒ Object
-
#initialize(args = {}) ⇒ Wrapper
constructor
A new instance of Wrapper.
- #options ⇒ Object
- #options_hash ⇒ Object
- #payload(request_type = :get) ⇒ Object
- #payload_key(request_type) ⇒ Object
- #proxy_hash ⇒ Object
- #wrap_request_in_case_of_timeout(proc, options = {}) ⇒ Object
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_password ⇒ Object (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_token ⇒ Object (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_url ⇒ Object (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_username ⇒ Object (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_host ⇒ Object (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_password ⇒ Object (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_port ⇒ Object (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_user ⇒ Object (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_token ⇒ Object
33 34 35 |
# File 'lib/centaman/wrapper.rb', line 33 def generate_token Base64.encode64("#{api_username}:#{api_password}") end |
#headers ⇒ Object
29 30 31 |
# File 'lib/centaman/wrapper.rb', line 29 def headers { 'authorization' => "Basic #{api_token}", 'Content-Type' => 'application/json' } end |
#options ⇒ Object
37 38 39 |
# File 'lib/centaman/wrapper.rb', line 37 def [] # overwritten by children end |
#options_hash ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/centaman/wrapper.rb', line 41 def hash = {} .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) => } 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_hash ⇒ Object
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, = {}) time = .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 |