intercom 3.9.0 → 3.9.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1b9cbc7fa1ebcd80a2cab246e89dbab5f86b0f1ec94995b1d2ebdda342dc9dce
4
- data.tar.gz: c926b260d154dea87dc2f666d27cfd6e5c61c986071969f0c22c5f7389da3f14
3
+ metadata.gz: b05fef934a0732652abcfa3f996dedcd0fe373e2d445313d91fd317e964d8972
4
+ data.tar.gz: 2cb22c404e5bdead26355feadae48df299124157850d0366eb4a57f7523ef1be
5
5
  SHA512:
6
- metadata.gz: a14065260cca33928e257df6970c3dcfe0c9fc73478d6a4f0f0f5c0f03f20bb82f9662bc8ba59a7531308090072f69f9b4ab6d78be7534898430479ac65a1c2d
7
- data.tar.gz: fb99bfcc7e38da3119efff1f1ff8e905f9204e785cac8600f0f1dadbc98dbaf0d62efc66559936212ee045c34915a5b37931778fe4c4c5ad6bceccae312d3d35
6
+ metadata.gz: 64f7face5cb2170fbe3e5f96a26ced8d869f9c0d529e2b061962d21ef0932276750903889af53f209e740a696c6847429bc7cd04a176266d78ad9be97de19d79
7
+ data.tar.gz: c4d837db173d1c67e99aee611248ff9e62965c1b3f5d01f94ef7e131bccfc137174df75316e4caf41be4d25a92fd4b7a32c08272a4d04a0dd020405968f1ccd7
@@ -1,12 +1,35 @@
1
1
  version: 2
2
2
  jobs:
3
- build:
3
+ "Test against Ruby 2.4":
4
4
  docker:
5
- - image: circleci/ruby:2.4.6
6
-
5
+ - image: circleci/ruby:2.4.9
6
+ working_directory: ~/intercom-ruby
7
+ steps:
8
+ - checkout
9
+ - run: bundle install
10
+ - run: bundle exec rake
11
+ "Test against Ruby 2.5":
12
+ docker:
13
+ - image: circleci/ruby:2.5.7
7
14
  working_directory: ~/intercom-ruby
8
-
9
15
  steps:
10
16
  - checkout
11
17
  - run: bundle install
12
18
  - run: bundle exec rake
19
+ "Test against Ruby 2.6":
20
+ docker:
21
+ - image: circleci/ruby:2.6.5
22
+ working_directory: ~/intercom-ruby
23
+ steps:
24
+ - checkout
25
+ - run: bundle install
26
+ - run: bundle exec rake
27
+
28
+ workflows:
29
+ version: 2
30
+ build_and_test:
31
+ jobs:
32
+ - "Test against Ruby 2.4"
33
+ - "Test against Ruby 2.5"
34
+ - "Test against Ruby 2.6"
35
+
data/README.md CHANGED
@@ -23,7 +23,7 @@ This version of the gem is compatible with `Ruby 2.1` and above.
23
23
 
24
24
  Using bundler:
25
25
 
26
- gem 'intercom', '~> 3.8.1'
26
+ gem 'intercom', '~> 3.9.0'
27
27
 
28
28
  ## Basic Usage
29
29
 
@@ -470,9 +470,13 @@ intercom.contacts.scroll.each { |lead| puts lead.id}
470
470
 
471
471
  #### Customers
472
472
 
473
+ Our Customer API is a central place for all the information on your customers, whether they're users or leads. More detail in our API documentation on [Customers](https://developers.intercom.com/intercom-api-reference/v0/reference#customers).
474
+
475
+ ```ruby
473
476
  # Search for customers
474
477
  customers = intercom.customers.search(query: { "field": "name", "operator": "=", "value": "Alice"}, per_page: 50, sort_field: "name", sort_order: "ascending")
475
478
  customers.each { |customer| p customer.name }
479
+ ```
476
480
 
477
481
  #### Counts
478
482
 
@@ -556,3 +560,24 @@ intercom = Intercom::Client.new(token: ENV['AT'], handle_rate_limit: true)
556
560
  - **Send coherent history**. Make sure each individual commit in your pull
557
561
  request is meaningful. If you had to make multiple intermediate commits while
558
562
  developing, please squash them before sending them to us.
563
+
564
+ ### Development
565
+
566
+ #### Running tests
567
+
568
+ ```bash
569
+ # all tests
570
+ bundle exec spec
571
+
572
+ # unit tests
573
+ bundle exec spec:unit
574
+
575
+ # integration tests
576
+ bundle exec spec:integration
577
+
578
+ # single test file
579
+ bundle exec m spec/unit/intercom/job_spec.rb
580
+
581
+ # single test
582
+ bundle exec m spec/unit/intercom/job_spec.rb:49
583
+ ```
@@ -1,3 +1,6 @@
1
+ 3.9.0
2
+ Added Teams endpoint functionality
3
+
1
4
  3.8.1
2
5
  Added error handling for company_not_found
3
6
 
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_development_dependency 'minitest', '~> 5.4'
22
+ spec.add_development_dependency "m", "~> 1.5.0"
22
23
  spec.add_development_dependency 'rake', '~> 10.3'
23
24
  spec.add_development_dependency 'mocha', '~> 1.0'
24
25
  spec.add_development_dependency "fakeweb", ["~> 1.3"]
@@ -63,6 +63,9 @@ module Intercom
63
63
  # Raised when you have exceeded the API rate limit
64
64
  class RateLimitExceeded < IntercomError; end
65
65
 
66
+ # Raised when some attribute of the response cannot be handled
67
+ class UnexpectedResponseError < IntercomError; end
68
+
66
69
  # Raised when the request throws an error not accounted for
67
70
  class UnexpectedError < IntercomError; end
68
71
 
@@ -3,64 +3,48 @@ require 'net/https'
3
3
 
4
4
  module Intercom
5
5
  class Request
6
- attr_accessor :path, :net_http_method, :rate_limit_details, :handle_rate_limit
7
-
8
- def initialize(path, net_http_method)
9
- self.path = path
10
- self.net_http_method = net_http_method
11
- self.handle_rate_limit = false
12
- end
13
-
14
- def set_common_headers(method, base_uri)
15
- method.add_field('AcceptEncoding', 'gzip, deflate')
16
- end
17
-
18
- def set_basic_auth(method, username, secret)
19
- method.basic_auth(CGI.unescape(username), CGI.unescape(secret))
20
- end
6
+ class << self
7
+ def get(path, params)
8
+ new(path, Net::HTTP::Get.new(append_query_string_to_url(path, params), default_headers))
9
+ end
21
10
 
22
- def set_api_version(method, api_version)
23
- method.add_field('Intercom-Version', api_version)
24
- end
11
+ def post(path, form_data)
12
+ new(path, method_with_body(Net::HTTP::Post, path, form_data))
13
+ end
25
14
 
26
- def self.get(path, params)
27
- new(path, Net::HTTP::Get.new(append_query_string_to_url(path, params), default_headers))
28
- end
15
+ def delete(path, params)
16
+ new(path, method_with_body(Net::HTTP::Delete, path, params))
17
+ end
29
18
 
30
- def self.post(path, form_data)
31
- new(path, method_with_body(Net::HTTP::Post, path, form_data))
32
- end
19
+ def put(path, form_data)
20
+ new(path, method_with_body(Net::HTTP::Put, path, form_data))
21
+ end
33
22
 
34
- def self.delete(path, params)
35
- new(path, method_with_body(Net::HTTP::Delete, path, params))
36
- end
23
+ private def method_with_body(http_method, path, params)
24
+ request = http_method.send(:new, path, default_headers)
25
+ request.body = params.to_json
26
+ request["Content-Type"] = "application/json"
27
+ request
28
+ end
37
29
 
38
- def self.put(path, form_data)
39
- new(path, method_with_body(Net::HTTP::Put, path, form_data))
40
- end
30
+ private def default_headers
31
+ {'Accept-Encoding' => 'gzip, deflate', 'Accept' => 'application/vnd.intercom.3+json', 'User-Agent' => "Intercom-Ruby/#{Intercom::VERSION}"}
32
+ end
41
33
 
42
- def self.method_with_body(http_method, path, params)
43
- request = http_method.send(:new, path, default_headers)
44
- request.body = params.to_json
45
- request["Content-Type"] = "application/json"
46
- request
34
+ private def append_query_string_to_url(url, params)
35
+ return url if params.empty?
36
+ query_string = params.map { |k, v| "#{k.to_s}=#{CGI::escape(v.to_s)}" }.join('&')
37
+ url + "?#{query_string}"
38
+ end
47
39
  end
48
40
 
49
- def self.default_headers
50
- {'Accept-Encoding' => 'gzip, deflate', 'Accept' => 'application/vnd.intercom.3+json', 'User-Agent' => "Intercom-Ruby/#{Intercom::VERSION}"}
41
+ def initialize(path, net_http_method)
42
+ self.path = path
43
+ self.net_http_method = net_http_method
44
+ self.handle_rate_limit = false
51
45
  end
52
46
 
53
- def client(uri, read_timeout:, open_timeout:)
54
- net = Net::HTTP.new(uri.host, uri.port)
55
- if uri.is_a?(URI::HTTPS)
56
- net.use_ssl = true
57
- net.verify_mode = OpenSSL::SSL::VERIFY_PEER
58
- net.ca_file = File.join(File.dirname(__FILE__), '../data/cacert.pem')
59
- end
60
- net.read_timeout = read_timeout
61
- net.open_timeout = open_timeout
62
- net
63
- end
47
+ attr_accessor :handle_rate_limit
64
48
 
65
49
  def execute(target_base_url=nil, username:, secret: nil, read_timeout: 90, open_timeout: 30, api_version: nil)
66
50
  retries = 3
@@ -72,10 +56,16 @@ module Intercom
72
56
  client(base_uri, read_timeout: read_timeout, open_timeout: open_timeout).start do |http|
73
57
  begin
74
58
  response = http.request(net_http_method)
59
+
75
60
  set_rate_limit_details(response)
76
- decoded_body = decode_body(response)
77
- parsed_body = parse_body(decoded_body, response)
78
61
  raise_errors_on_failure(response)
62
+
63
+ parsed_body = extract_response_body(response)
64
+
65
+ return nil if parsed_body.nil?
66
+
67
+ raise_application_errors_on_failure(parsed_body, response.code.to_i) if parsed_body['type'] == 'error.list'
68
+
79
69
  parsed_body
80
70
  rescue Intercom::RateLimitExceeded => e
81
71
  if @handle_rate_limit
@@ -98,24 +88,51 @@ module Intercom
98
88
  end
99
89
  end
100
90
 
101
- def decode_body(response)
102
- decode(response['content-encoding'], response.body)
103
- end
91
+ attr_accessor :path,
92
+ :net_http_method,
93
+ :rate_limit_details
104
94
 
105
- def parse_body(decoded_body, response)
106
- parsed_body = nil
107
- return parsed_body if decoded_body.nil? || decoded_body.strip.empty?
108
- begin
109
- parsed_body = JSON.parse(decoded_body)
110
- rescue JSON::ParserError => _
111
- raise_errors_on_failure(response)
95
+ private :path,
96
+ :net_http_method,
97
+ :rate_limit_details
98
+
99
+ private def client(uri, read_timeout:, open_timeout:)
100
+ net = Net::HTTP.new(uri.host, uri.port)
101
+ if uri.is_a?(URI::HTTPS)
102
+ net.use_ssl = true
103
+ net.verify_mode = OpenSSL::SSL::VERIFY_PEER
104
+ net.ca_file = File.join(File.dirname(__FILE__), '../data/cacert.pem')
112
105
  end
113
- raise_errors_on_failure(response) if parsed_body.nil?
114
- raise_application_errors_on_failure(parsed_body, response.code.to_i) if parsed_body['type'] == 'error.list'
115
- parsed_body
106
+ net.read_timeout = read_timeout
107
+ net.open_timeout = open_timeout
108
+ net
116
109
  end
117
110
 
118
- def set_rate_limit_details(response)
111
+ private def extract_response_body(response)
112
+ decoded_body = decode(response['content-encoding'], response.body)
113
+
114
+ json_parse_response(decoded_body, response.code)
115
+ end
116
+
117
+ private def decode(content_encoding, body)
118
+ return body if (!body) || body.empty? || content_encoding != 'gzip'
119
+ Zlib::GzipReader.new(StringIO.new(body)).read.force_encoding("utf-8")
120
+ end
121
+
122
+ private def json_parse_response(str, code)
123
+ return nil if str.to_s.empty?
124
+
125
+ JSON.parse(str)
126
+ rescue JSON::ParserError
127
+ msg = <<~MSG.gsub(/[[:space:]]+/, " ").strip # #squish from ActiveSuppor
128
+ Expected a JSON response body. Instead got '#{str}'
129
+ with status code '#{code}'.
130
+ MSG
131
+
132
+ raise UnexpectedResponseError, msg
133
+ end
134
+
135
+ private def set_rate_limit_details(response)
119
136
  rate_limit_details = {}
120
137
  rate_limit_details[:limit] = response['X-RateLimit-Limit'].to_i if response['X-RateLimit-Limit']
121
138
  rate_limit_details[:remaining] = response['X-RateLimit-Remaining'].to_i if response['X-RateLimit-Remaining']
@@ -123,30 +140,39 @@ module Intercom
123
140
  @rate_limit_details = rate_limit_details
124
141
  end
125
142
 
126
- def decode(content_encoding, body)
127
- return body if (!body) || body.empty? || content_encoding != 'gzip'
128
- Zlib::GzipReader.new(StringIO.new(body)).read.force_encoding("utf-8")
143
+ private def set_common_headers(method, base_uri)
144
+ method.add_field('AcceptEncoding', 'gzip, deflate')
129
145
  end
130
146
 
131
- def raise_errors_on_failure(res)
132
- if res.code.to_i.eql?(404)
147
+ private def set_basic_auth(method, username, secret)
148
+ method.basic_auth(CGI.unescape(username), CGI.unescape(secret))
149
+ end
150
+
151
+ private def set_api_version(method, api_version)
152
+ method.add_field('Intercom-Version', api_version)
153
+ end
154
+
155
+ private def raise_errors_on_failure(res)
156
+ code = res.code.to_i
157
+
158
+ if code == 404
133
159
  raise Intercom::ResourceNotFound.new('Resource Not Found')
134
- elsif res.code.to_i.eql?(401)
160
+ elsif code == 401
135
161
  raise Intercom::AuthenticationError.new('Unauthorized')
136
- elsif res.code.to_i.eql?(403)
162
+ elsif code == 403
137
163
  raise Intercom::AuthenticationError.new('Forbidden')
138
- elsif res.code.to_i.eql?(429)
164
+ elsif code == 429
139
165
  raise Intercom::RateLimitExceeded.new('Rate Limit Exceeded')
140
- elsif res.code.to_i.eql?(500)
166
+ elsif code == 500
141
167
  raise Intercom::ServerError.new('Server Error')
142
- elsif res.code.to_i.eql?(502)
168
+ elsif code == 502
143
169
  raise Intercom::BadGatewayError.new('Bad Gateway Error')
144
- elsif res.code.to_i.eql?(503)
170
+ elsif code == 503
145
171
  raise Intercom::ServiceUnavailableError.new('Service Unavailable')
146
172
  end
147
173
  end
148
174
 
149
- def raise_application_errors_on_failure(error_list_details, http_code)
175
+ private def raise_application_errors_on_failure(error_list_details, http_code)
150
176
  # Currently, we don't support multiple errors
151
177
  error_details = error_list_details['errors'].first
152
178
  error_code = error_details['type'] || error_details['code']
@@ -198,18 +224,12 @@ module Intercom
198
224
  end
199
225
  end
200
226
 
201
- def message_for_unexpected_error_with_type(error_details, parsed_http_code)
227
+ private def message_for_unexpected_error_with_type(error_details, parsed_http_code)
202
228
  "The error of type '#{error_details['type']}' is not recognized. It occurred with the message: #{error_details['message']} and http_code: '#{parsed_http_code}'. Please contact Intercom with these details."
203
229
  end
204
230
 
205
- def message_for_unexpected_error_without_type(error_details, parsed_http_code)
231
+ private def message_for_unexpected_error_without_type(error_details, parsed_http_code)
206
232
  "An unexpected error occured. It occurred with the message: #{error_details['message']} and http_code: '#{parsed_http_code}'. Please contact Intercom with these details."
207
233
  end
208
-
209
- def self.append_query_string_to_url(url, params)
210
- return url if params.empty?
211
- query_string = params.map { |k, v| "#{k.to_s}=#{CGI::escape(v.to_s)}" }.join('&')
212
- url + "?#{query_string}"
213
- end
214
234
  end
215
235
  end
@@ -1,3 +1,3 @@
1
1
  module Intercom #:nodoc:
2
- VERSION = "3.9.0"
2
+ VERSION = "3.9.2"
3
3
  end
@@ -3,6 +3,7 @@ require 'minitest/autorun'
3
3
  require 'mocha/setup'
4
4
  require 'webmock'
5
5
  require 'time'
6
+ require 'pry'
6
7
  include WebMock::API
7
8
 
8
9
  def test_customer(email="bob@example.com")
@@ -4,12 +4,22 @@ module Intercom
4
4
  describe Client do
5
5
  let(:app_id) { 'myappid' }
6
6
  let(:api_key) { 'myapikey' }
7
- let(:client) { Client.new(app_id: app_id, api_key: api_key) }
7
+ let(:client) do
8
+ Client.new(
9
+ app_id: app_id,
10
+ api_key: api_key,
11
+ handle_rate_limit: true
12
+ )
13
+ end
8
14
 
9
15
  it 'should set the base url' do
10
16
  client.base_url.must_equal('https://api.intercom.io')
11
17
  end
12
18
 
19
+ it 'should have handle_rate_limit set' do
20
+ _(client.handle_rate_limit).must_equal(true)
21
+ end
22
+
13
23
  it 'should be able to change the base url' do
14
24
  prev = client.options(Intercom::Client.set_base_url('https://mymockintercom.io'))
15
25
  client.base_url.must_equal('https://mymockintercom.io')
@@ -3,107 +3,146 @@ require 'ostruct'
3
3
 
4
4
  WebMock.enable!
5
5
 
6
- describe 'Intercom::Request' do
7
- it 'raises an error when a html error page rendered' do
8
- response = OpenStruct.new(:code => 500)
9
- req = Intercom::Request.new('path/', 'GET')
10
- proc {req.parse_body('<html>something</html>', response)}.must_raise(Intercom::ServerError)
6
+ describe 'Intercom::Request', '#execute' do
7
+ let(:uri) {"https://api.intercom.io/users"}
8
+ let(:req) { Intercom::Request.get(uri, {}) }
9
+ let(:default_body) { { data: "test" }.to_json }
10
+
11
+ def execute!
12
+ req.execute(uri, username: 'ted', secret: '')
11
13
  end
12
14
 
13
- it 'raises a RateLimitExceeded error when the response code is 429' do
14
- response = OpenStruct.new(:code => 429)
15
- req = Intercom::Request.new('path/', 'GET')
16
- proc {req.parse_body('<html>something</html>', response)}.must_raise(Intercom::RateLimitExceeded)
15
+ it 'should call sleep for rate limit error three times and raise a rate limit error otherwise' do
16
+ stub_request(:any, uri).to_return(
17
+ status: [429, "Too Many Requests"],
18
+ headers: { 'X-RateLimit-Reset' => (Time.now.utc + 10).to_i.to_s },
19
+ body: default_body
20
+ )
21
+
22
+ req.handle_rate_limit=true
23
+
24
+ req.expects(:sleep).times(3).with(any_parameters)
25
+
26
+ expect { execute! }.must_raise(Intercom::RateLimitExceeded)
17
27
  end
18
28
 
19
- it 'parse_body raises an error if the decoded_body is "null"' do
20
- response = OpenStruct.new(:code => 500)
21
- req = Intercom::Request.new('path/', 'GET')
22
- proc { req.parse_body('null', response)}.must_raise(Intercom::ServerError)
29
+ it 'should not call sleep for rate limit error' do
30
+ stub_request(:any, uri).to_return(
31
+ status: [200, "OK"],
32
+ headers: { 'X-RateLimit-Reset' => Time.now.utc + 10 },
33
+ body: default_body
34
+ )
35
+
36
+ req.handle_rate_limit=true
37
+ req.expects(:sleep).never.with(any_parameters)
38
+
39
+ execute!
23
40
  end
24
41
 
25
- describe 'Intercom::Client' do
26
- let(:client) { Intercom::Client.new(token: 'foo', handle_rate_limit: true) }
27
- let(:uri) {"https://api.intercom.io/users"}
42
+ it 'should call sleep for rate limit error just once' do
43
+ stub_request(:any, uri).to_return(
44
+ status: [429, "Too Many Requests"],
45
+ headers: { 'X-RateLimit-Reset' => (Time.now.utc + 10).to_i.to_s },
46
+ ).then.to_return(status: [200, "OK"], body: default_body)
28
47
 
29
- it 'should have handle_rate_limit set' do
30
- client.handle_rate_limit.must_equal(true)
31
- end
48
+ req.handle_rate_limit=true
49
+ req.expects(:sleep).with(any_parameters)
50
+
51
+ execute!
52
+ end
53
+
54
+ it 'should not sleep if rate limit reset time has passed' do
55
+ stub_request(:any, uri).to_return(
56
+ status: [429, "Too Many Requests"],
57
+ headers: { 'X-RateLimit-Reset' => Time.parse("February 25 2010").utc.to_i.to_s },
58
+ body: default_body
59
+ ).then.to_return(status: [200, "OK"], body: default_body)
60
+
61
+ req.handle_rate_limit=true
62
+ req.expects(:sleep).never.with(any_parameters)
63
+
64
+ execute!
65
+ end
66
+
67
+ it 'handles an empty body gracefully' do
68
+ stub_request(:any, uri).to_return(
69
+ status: 200,
70
+ body: nil
71
+ )
32
72
 
33
- it 'should call sleep for rate limit error three times and raise a rate limit error otherwise' do
34
- expect {
35
- stub_request(:any, uri).\
36
- to_return(status: [429, "Too Many Requests"], headers: { 'X-RateLimit-Reset' => (Time.now.utc + 10).to_i.to_s })
37
- req = Intercom::Request.get(uri, "")
38
- req.handle_rate_limit=true
39
- req.expects(:sleep).times(3).with(any_parameters)
40
- req.execute(uri, username: "ted", secret: "")
41
- }.must_raise(Intercom::RateLimitExceeded)
73
+ assert_nil(execute!)
74
+ end
75
+
76
+ describe 'HTTP error handling' do
77
+ it 'raises an error when the response is successful but the body is not JSON' do
78
+ stub_request(:any, uri).to_return(
79
+ status: 200,
80
+ body: '<html>something</html>'
81
+ )
82
+
83
+ expect { execute! }.must_raise(Intercom::UnexpectedResponseError)
42
84
  end
43
85
 
44
- it 'should not call sleep for rate limit error' do
45
- # Use webmock to mock the HTTP request
46
- stub_request(:any, uri).\
47
- to_return(status: [200, "OK"], headers: { 'X-RateLimit-Reset' => Time.now.utc + 10 })
48
- req = Intercom::Request.get(uri, "")
49
- req.handle_rate_limit=true
50
- req.expects(:sleep).never.with(any_parameters)
51
- req.execute(uri, username: "ted", secret: "")
86
+ it 'raises an error when an html error page rendered' do
87
+ stub_request(:any, uri).to_return(
88
+ status: 500,
89
+ body: '<html>something</html>'
90
+ )
91
+
92
+ expect { execute! }.must_raise(Intercom::ServerError)
52
93
  end
53
94
 
54
- it 'should call sleep for rate limit error just once' do
55
- # Use webmock to mock the HTTP request
56
- stub_request(:any, uri).\
57
- to_return(status: [429, "Too Many Requests"], headers: { 'X-RateLimit-Reset' => (Time.now.utc + 10).to_i.to_s }).\
58
- then.to_return(status: [200, "OK"])
59
- req = Intercom::Request.get(uri, "")
60
- req.handle_rate_limit=true
61
- req.expects(:sleep).with(any_parameters)
62
- req.execute(uri, username: "ted", secret: "")
95
+ it 'raises an error if the decoded_body is "null"' do
96
+ stub_request(:any, uri).to_return(
97
+ status: 500,
98
+ body: 'null'
99
+ )
100
+
101
+ expect { execute! }.must_raise(Intercom::ServerError)
63
102
  end
64
103
 
65
- it 'should not sleep if rate limit reset time has passed' do
66
- # Use webmock to mock the HTTP request
67
- stub_request(:any, uri).\
68
- to_return(status: [429, "Too Many Requests"], headers: { 'X-RateLimit-Reset' => Time.parse("February 25 2010").utc.to_i.to_s }).\
69
- then.to_return(status: [200, "OK"])
70
- req = Intercom::Request.get(uri, "")
71
- req.handle_rate_limit=true
72
- req.expects(:sleep).never.with(any_parameters)
73
- req.execute(uri, username: "ted", secret: "")
104
+ it 'raises a RateLimitExceeded error when the response code is 429' do
105
+ stub_request(:any, uri).to_return(
106
+ status: 429,
107
+ body: 'null'
108
+ )
109
+
110
+ expect { execute! }.must_raise(Intercom::RateLimitExceeded)
74
111
  end
75
112
  end
76
113
 
77
-
78
- describe "Application errors on failure" do
114
+ describe "application error handling" do
79
115
  let(:uri) {"https://api.intercom.io/conversations/reply"}
116
+ let(:req) { Intercom::Request.put(uri, {}) }
117
+
80
118
  it 'should raise ResourceNotUniqueError error on resource_conflict code' do
81
- # Use webmock to mock the HTTP request
82
- stub_request(:put, uri).\
83
- to_return(status: [409, "Resource Already Exists"], headers: { 'X-RateLimit-Reset' => (Time.now.utc + 10).to_i.to_s }, body: {type: "error.list", errors: [ code: "resource_conflict" ]}.to_json)
84
- req = Intercom::Request.put(uri, "")
85
- expect { req.execute(uri, username: "ted", secret: "") }.must_raise(Intercom::ResourceNotUniqueError)
119
+ stub_request(:put, uri).to_return(
120
+ status: [409, "Resource Already Exists"],
121
+ headers: { 'X-RateLimit-Reset' => (Time.now.utc + 10).to_i.to_s },
122
+ body: { type: "error.list", errors: [ code: "resource_conflict" ] }.to_json
123
+ )
124
+
125
+ expect { execute! }.must_raise(Intercom::ResourceNotUniqueError)
86
126
  end
87
127
 
88
128
  it 'should raise ApiVersionInvalid error on intercom_version_invalid code' do
89
- # Use webmock to mock the HTTP request
90
- stub_request(:put, uri).\
91
- to_return(status: [400, "Bad Request"], headers: { 'X-RateLimit-Reset' => (Time.now.utc + 10).to_i.to_s }, body: {type: "error.list", errors: [ code: "intercom_version_invalid" ]}.to_json)
92
- req = Intercom::Request.put(uri, "")
93
- expect { req.execute(uri, username: "ted", secret: "") }.must_raise(Intercom::ApiVersionInvalid)
129
+ stub_request(:put, uri).to_return(
130
+ status: [400, "Bad Request"],
131
+ headers: { 'X-RateLimit-Reset' => (Time.now.utc + 10).to_i.to_s },
132
+ body: { type: "error.list", errors: [ code: "intercom_version_invalid" ] }.to_json
133
+ )
134
+
135
+ expect { execute! }.must_raise(Intercom::ApiVersionInvalid)
94
136
  end
95
137
 
96
138
  it 'should raise ResourceNotFound error on company_not_found code' do
97
- stub_request(:put, uri).\
98
- to_return(status: [404, "Not Found"], headers: { 'X-RateLimit-Reset' => (Time.now.utc + 10).to_i.to_s }, body: {type: "error.list", errors: [ code: "company_not_found" ]}.to_json)
99
- req = Intercom::Request.put(uri, "")
100
- expect { req.execute(uri, username: "ted", secret: "") }.must_raise(Intercom::ResourceNotFound)
101
- end
102
- end
139
+ stub_request(:put, uri).to_return(
140
+ status: [404, "Not Found"],
141
+ headers: { 'X-RateLimit-Reset' => (Time.now.utc + 10).to_i.to_s },
142
+ body: { type: "error.list", errors: [ code: "company_not_found" ] }.to_json
143
+ )
103
144
 
104
- it 'parse_body returns nil if decoded_body is nil' do
105
- response = OpenStruct.new(:code => 500)
106
- req = Intercom::Request.new('path/', 'GET')
107
- assert_nil(req.parse_body(nil, response))
145
+ expect { execute! }.must_raise(Intercom::ResourceNotFound)
146
+ end
108
147
  end
109
148
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: intercom
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.9.0
4
+ version: 3.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben McRedmond
@@ -15,7 +15,7 @@ authors:
15
15
  autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
- date: 2019-10-09 00:00:00.000000000 Z
18
+ date: 2019-11-05 00:00:00.000000000 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: minitest
@@ -31,6 +31,20 @@ dependencies:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '5.4'
34
+ - !ruby/object:Gem::Dependency
35
+ name: m
36
+ requirement: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.5.0
41
+ type: :development
42
+ prerelease: false
43
+ version_requirements: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.5.0
34
48
  - !ruby/object:Gem::Dependency
35
49
  name: rake
36
50
  requirement: !ruby/object:Gem::Requirement