Class: InContact::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/incontact/connection.rb

Constant Summary collapse

TIMEOUT =
60
OPEN_TIMEOUT =
10
MAX_TRIES =
2
RETRY_SLEEP =
1
API_PATH_PREFIX =
"services/v8.0/"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, authorization, options = {}) ⇒ Connection

Returns a new instance of Connection.



13
14
15
16
17
# File 'lib/incontact/connection.rb', line 13

def initialize(url, authorization, options = {})
  @url                = url
  @authorization      = authorization
  @default_data_model = options[:default_data_model] || OpenStruct
end

Instance Attribute Details

#authorizationObject (readonly)

Returns the value of attribute authorization.



10
11
12
# File 'lib/incontact/connection.rb', line 10

def authorization
  @authorization
end

#default_data_modelObject (readonly)

Returns the value of attribute default_data_model.



11
12
13
# File 'lib/incontact/connection.rb', line 11

def default_data_model
  @default_data_model
end

#urlObject (readonly)

Returns the value of attribute url.



9
10
11
# File 'lib/incontact/connection.rb', line 9

def url
  @url
end

Instance Method Details

#get(path, params = {}) ⇒ Object



19
20
21
# File 'lib/incontact/connection.rb', line 19

def get(path, params = {})
  request :get, path, params
end

#post(path, payload = "", params = {}) ⇒ Object



23
24
25
# File 'lib/incontact/connection.rb', line 23

def post(path, payload = "", params = {})
  request :post, path, params, payload
end

#request(type, path, params = {}, body = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/incontact/connection.rb', line 27

def request(type, path, params = {}, body=nil)
  timeout_override      = params.delete :timeout
  open_timeout_override = params.delete :open_timeout
  data_model_override   = params.delete :data_model_override
  response              = Retryable.retryable(retryable_options(params)) do
    request_with_exception_handling do
      connection.send(type) do |req|
        req.path                   = API_PATH_PREFIX + path unless path.blank?
        req.options[:timeout]      = timeout_override      || TIMEOUT
        req.options[:open_timeout] = open_timeout_override || OPEN_TIMEOUT
        req.params                 = params
        req.body                   = body.to_json unless body.blank?
      end
    end
  end
  raise_response_errors(response)
  parse_response(response, data_model_override)
end