Class: Leadcli::ApiCaller
- Inherits:
-
Object
- Object
- Leadcli::ApiCaller
- Includes:
- Routes
- Defined in:
- lib/leadcli/api_caller.rb
Constant Summary collapse
- DEFAULT_RETRY_LIMIT =
1
Instance Attribute Summary collapse
-
#connection_attempts ⇒ Object
readonly
Returns the value of attribute connection_attempts.
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
-
#max_connection_attempts ⇒ Object
Returns the value of attribute max_connection_attempts.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #call(obj) ⇒ Object
-
#initialize(endpoint, options = {}) ⇒ ApiCaller
constructor
A new instance of ApiCaller.
Methods included from Routes
#create_endpoint, #create_url, #delete_endpoint, #delete_url
Constructor Details
#initialize(endpoint, options = {}) ⇒ ApiCaller
Returns a new instance of ApiCaller.
12 13 14 15 16 |
# File 'lib/leadcli/api_caller.rb', line 12 def initialize(endpoint, ={}) @endpoint, = endpoint, @connection_attempts = 0 @max_connection_attempts = [:max_connection_attempts] || DEFAULT_RETRY_LIMIT end |
Instance Attribute Details
#connection_attempts ⇒ Object (readonly)
Returns the value of attribute connection_attempts.
8 9 10 |
# File 'lib/leadcli/api_caller.rb', line 8 def connection_attempts @connection_attempts end |
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
8 9 10 |
# File 'lib/leadcli/api_caller.rb', line 8 def endpoint @endpoint end |
#max_connection_attempts ⇒ Object
Returns the value of attribute max_connection_attempts.
7 8 9 |
# File 'lib/leadcli/api_caller.rb', line 7 def max_connection_attempts @max_connection_attempts end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/leadcli/api_caller.rb', line 8 def end |
Instance Method Details
#call(obj) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/leadcli/api_caller.rb', line 18 def call(obj) method, url = send("#{endpoint}_endpoint", [:url_options] || {}) Leadcli.debug("API CALL: #{method} #{url}") while connection_attempts < max_connection_attempts response = make_call(method, url) valid_response_codes = (200..207).to_a if valid_response_codes.include?(response.code.to_i) if [:success] Leadcli.debug("CALLING SUCCESS HANDLER: #{options[:success]}") obj.send([:success], response) end success = true break end end if !success && [:failure] obj.send([:failure], response) end rescue Errno::ECONNREFUSED => error Leadcli.debug("Remote server down : #{error}") end |