Class: Intercom::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/intercom/request.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, net_http_method) ⇒ Request

Returns a new instance of Request.



44
45
46
47
48
# File 'lib/intercom/request.rb', line 44

def initialize(path, net_http_method)
  self.path = path
  self.net_http_method = net_http_method
  self.handle_rate_limit = false
end

Instance Attribute Details

#handle_rate_limitObject

Returns the value of attribute handle_rate_limit.



50
51
52
# File 'lib/intercom/request.rb', line 50

def handle_rate_limit
  @handle_rate_limit
end

#net_http_method=(value) ⇒ Object

Sets the attribute net_http_method

Parameters:

  • value

    the value to set the attribute net_http_method to.



94
95
96
# File 'lib/intercom/request.rb', line 94

def net_http_method=(value)
  @net_http_method = value
end

#path=(value) ⇒ Object

Sets the attribute path

Parameters:

  • value

    the value to set the attribute path to.



94
95
96
# File 'lib/intercom/request.rb', line 94

def path=(value)
  @path = value
end

#rate_limit_detailsObject

Returns the value of attribute rate_limit_details.



94
95
96
# File 'lib/intercom/request.rb', line 94

def rate_limit_details
  @rate_limit_details
end

Class Method Details

.delete(path, params) ⇒ Object



17
18
19
# File 'lib/intercom/request.rb', line 17

def delete(path, params)
  new(path, method_with_body(Net::HTTP::Delete, path, params))
end

.get(path, params) ⇒ Object



9
10
11
# File 'lib/intercom/request.rb', line 9

def get(path, params)
  new(path, Net::HTTP::Get.new(append_query_string_to_url(path, params), default_headers))
end

.post(path, form_data) ⇒ Object



13
14
15
# File 'lib/intercom/request.rb', line 13

def post(path, form_data)
  new(path, method_with_body(Net::HTTP::Post, path, form_data))
end

.put(path, form_data) ⇒ Object



21
22
23
# File 'lib/intercom/request.rb', line 21

def put(path, form_data)
  new(path, method_with_body(Net::HTTP::Put, path, form_data))
end

Instance Method Details

#execute(target_base_url = nil, token:, read_timeout: 90, open_timeout: 30, api_version: nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/intercom/request.rb', line 52

def execute(target_base_url = nil, token:, read_timeout: 90, open_timeout: 30, api_version: nil)
  retries = 3
  base_uri = URI.parse(target_base_url)
  set_common_headers(net_http_method, base_uri)
  set_auth_header(net_http_method, token)
  set_api_version(net_http_method, api_version) if api_version
  begin
    client(base_uri, read_timeout: read_timeout, open_timeout: open_timeout).start do |http|
      begin
        response = http.request(net_http_method)

        set_rate_limit_details(response)
        raise_errors_on_failure(response)

        parsed_body = extract_response_body(response)

        return nil if parsed_body.nil?

        raise_application_errors_on_failure(parsed_body, response.code.to_i) if parsed_body['type'] == 'error.list'

        parsed_body
      rescue Intercom::RateLimitExceeded => e
        if @handle_rate_limit
          seconds_to_retry = (@rate_limit_details[:reset_at] - Time.now.utc).ceil
          if (retries -= 1) < 0
            raise Intercom::RateLimitExceeded, 'Rate limit retries exceeded. Please examine current API Usage.'
          else
            sleep seconds_to_retry unless seconds_to_retry < 0
            retry
          end
        else
          raise e
        end
      rescue Timeout::Error
        raise Intercom::ServiceUnavailableError, 'Service Unavailable [request timed out]'
      end
    end
  rescue Timeout::Error
    raise Intercom::ServiceConnectionError, 'Failed to connect to service [connection attempt timed out]'
  end
end