intercom 3.5.26 → 3.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0a1defabdef4a55f98769450ea8363083783d649
4
- data.tar.gz: 140dfa0321bbabb1cf80cbdc132c8b3ceb30e6d4
3
+ metadata.gz: da3b7fbe7b404dd5989bd60385c83931f9d58460
4
+ data.tar.gz: 2ee3931952cc325396e6d43d6c6b2cf08504b6f7
5
5
  SHA512:
6
- metadata.gz: 1799b76b57bac79de2bb3a1f8dda2aa0b28b4f9dd73fea1839169125d25ca228bdee677d1bd8b9d464baa51d54b8ffe6d8fa2e4f59399080c7df995a44dc7bc6
7
- data.tar.gz: 8e37c3ed1b57d739b5b9922b890b0e35e32d30190154be0aebd0ee4e85d9b23594d9630c380ad626111d84e71a495309069932d43d36d7b705956d0e835fcff0
6
+ metadata.gz: 30eec0038ac5ab50f2c031fffc9596bb563060ae72dab806921dd78fedf44683c7b48d57b9c2c083c5f50bfafc5f8bd44ae9f3fe72c727d8db8a3271a2a608d9
7
+ data.tar.gz: 301ca8d4de5e006b41046de14d6498fa9b03a0c72d4f82fca15e10a12a7ca2f80c00d6fbe85efbef118a524a910a5bd1f725f6e55752920fd431fbf7f8cbffcd
data/README.md CHANGED
@@ -22,7 +22,7 @@ This version of the gem is compatible with `Ruby 2.1` and above.
22
22
 
23
23
  Using bundler:
24
24
 
25
- gem 'intercom', '~> 3.5.25'
25
+ gem 'intercom', '~> 3.6.0'
26
26
 
27
27
  ## Basic Usage
28
28
 
@@ -132,8 +132,10 @@ intercom.companies.save(company)
132
132
  # Iterate over all companies
133
133
  intercom.companies.all.each {|company| puts %Q(#{company.name} - #{company.custom_attributes["referral_source"]}) }
134
134
  intercom.companies.all.map {|company| company.name }
135
- # Get a list of users in a company
136
- intercom.companies.users(company.id)
135
+ # Get a list of users in a company by Intercom Company ID
136
+ intercom.companies.users_by_intercom_company_id(company.id)
137
+ # Get a list of users in a company by external company_id
138
+ intercom.companies.users_by_company_id(company.company_id)
137
139
  # Get a large list of companies using scroll
138
140
  intercom.companies.scroll.each { |comp| puts comp.name}
139
141
  # Please see users scroll for more details of how to use scroll
@@ -149,7 +151,7 @@ intercom.tags.untag(name: 'blue', users: [{user_id: "42ea2f1b93891f6a99000427"}
149
151
  intercom.tags.all.each {|tag| "#{tag.id} - #{tag.name}" }
150
152
  intercom.tags.all.map {|tag| tag.name }
151
153
  # Tag companies
152
- tag = intercom.tags.tag(name: 'blue', companies: [{id: "42ea2f1b93891f6a99000427"}])
154
+ tag = intercom.tags.tag(name: 'blue', companies: [{company_id: "42ea2f1b93891f6a99000427"}])
153
155
  ```
154
156
 
155
157
  #### Segments
@@ -469,7 +471,7 @@ intercom.rate_limit_details
469
471
  ```
470
472
 
471
473
  You can handle the rate limits yourself but a simple option is to use the handle_rate_limit flag.
472
- This will automatically catch the 429 rate limit exceeded error and wait until the reset time to retry.
474
+ This will automatically catch the 429 rate limit exceeded error and wait until the reset time to retry. After three retries a rate limit exception will be raised. Encountering this error frequently may require a revisiting of your usage of the API.
473
475
 
474
476
  ```
475
477
  intercom = Intercom::Client.new(token: ENV['AT'], handle_rate_limit: true)
@@ -2,7 +2,7 @@ module Intercom
2
2
  class MisconfiguredClientError < StandardError; end
3
3
  class Client
4
4
  include Options
5
- attr_reader :base_url, :rate_limit_details, :username_part, :password_part, :handle_rate_limit
5
+ attr_reader :base_url, :rate_limit_details, :username_part, :password_part, :handle_rate_limit, :timeouts
6
6
 
7
7
  class << self
8
8
  def set_base_url(base_url)
@@ -12,6 +12,17 @@ module Intercom
12
12
  Proc.new { |obj| set_base_url(old_url).call(o) }
13
13
  end
14
14
  end
15
+
16
+ def set_timeouts(open_timeout: nil, read_timeout: nil)
17
+ return Proc.new do |o|
18
+ old_timeouts = o.timeouts
19
+ timeouts = {}
20
+ timeouts[:open_timeout] = open_timeout if open_timeout
21
+ timeouts[:read_timeout] = read_timeout if read_timeout
22
+ o.send(:timeouts=, timeouts)
23
+ Proc.new { |obj| set_timeouts(old_timeouts).call(o) }
24
+ end
25
+ end
15
26
  end
16
27
 
17
28
  def initialize(app_id: 'my_app_id', api_key: 'my_api_key', token: nil, base_url:'https://api.intercom.io', handle_rate_limit: false)
@@ -27,6 +38,10 @@ module Intercom
27
38
  @base_url = base_url
28
39
  @rate_limit_details = {}
29
40
  @handle_rate_limit = handle_rate_limit
41
+ @timeouts = {
42
+ open_timeout: 30,
43
+ read_timeout: 90
44
+ }
30
45
  end
31
46
 
32
47
  def admins
@@ -110,7 +125,7 @@ module Intercom
110
125
 
111
126
  def execute_request(request)
112
127
  request.handle_rate_limit = handle_rate_limit
113
- request.execute(@base_url, username: @username_part, secret: @password_part)
128
+ request.execute(@base_url, username: @username_part, secret: @password_part, **timeouts)
114
129
  ensure
115
130
  @rate_limit_details = request.rate_limit_details
116
131
  end
@@ -118,5 +133,9 @@ module Intercom
118
133
  def base_url=(new_url)
119
134
  @base_url = new_url
120
135
  end
136
+
137
+ def timeouts=(timeouts)
138
+ @timeouts = @timeouts.merge(timeouts)
139
+ end
121
140
  end
122
141
  end
@@ -46,25 +46,25 @@ module Intercom
46
46
  {'Accept-Encoding' => 'gzip, deflate', 'Accept' => 'application/vnd.intercom.3+json', 'User-Agent' => "Intercom-Ruby/#{Intercom::VERSION}"}
47
47
  end
48
48
 
49
- def client(uri)
49
+ def client(uri, read_timeout:, open_timeout:)
50
50
  net = Net::HTTP.new(uri.host, uri.port)
51
51
  if uri.is_a?(URI::HTTPS)
52
52
  net.use_ssl = true
53
53
  net.verify_mode = OpenSSL::SSL::VERIFY_PEER
54
54
  net.ca_file = File.join(File.dirname(__FILE__), '../data/cacert.pem')
55
55
  end
56
- net.read_timeout = 90
57
- net.open_timeout = 30
56
+ net.read_timeout = read_timeout
57
+ net.open_timeout = open_timeout
58
58
  net
59
59
  end
60
60
 
61
- def execute(target_base_url=nil, username:, secret: nil)
61
+ def execute(target_base_url=nil, username:, secret: nil, read_timeout: 90, open_timeout: 30)
62
62
  retries = 3
63
63
  base_uri = URI.parse(target_base_url)
64
64
  set_common_headers(net_http_method, base_uri)
65
65
  set_basic_auth(net_http_method, username, secret)
66
66
  begin
67
- client(base_uri).start do |http|
67
+ client(base_uri, read_timeout: read_timeout, open_timeout: open_timeout).start do |http|
68
68
  begin
69
69
  response = http.request(net_http_method)
70
70
  set_rate_limit_details(response)
@@ -75,8 +75,12 @@ module Intercom
75
75
  rescue Intercom::RateLimitExceeded => e
76
76
  if @handle_rate_limit
77
77
  seconds_to_retry = (@rate_limit_details[:reset_at] - Time.now.utc).ceil
78
- sleep seconds_to_retry unless seconds_to_retry < 0
79
- retry unless (retries -=1).zero?
78
+ if (retries -= 1) < 0
79
+ raise Intercom::RateLimitExceeded.new('Rate limit retries exceeded. Please examine current API Usage.')
80
+ else
81
+ sleep seconds_to_retry unless seconds_to_retry < 0
82
+ retry
83
+ end
80
84
  else
81
85
  raise e
82
86
  end
@@ -5,7 +5,6 @@ require 'intercom/api_operations/find'
5
5
  require 'intercom/api_operations/find_all'
6
6
  require 'intercom/api_operations/save'
7
7
  require 'intercom/api_operations/load'
8
- require 'intercom/extended_api_operations/users'
9
8
  require 'intercom/extended_api_operations/tags'
10
9
  require 'intercom/extended_api_operations/segments'
11
10
 
@@ -18,13 +17,24 @@ module Intercom
18
17
  include ApiOperations::List
19
18
  include ApiOperations::Scroll
20
19
  include ApiOperations::Save
21
- include ExtendedApiOperations::Users
22
20
  include ExtendedApiOperations::Tags
23
21
  include ExtendedApiOperations::Segments
24
22
 
25
23
  def collection_class
26
24
  Intercom::Company
27
25
  end
26
+
27
+ def users_by_intercom_company_id(id)
28
+ get_users(url: "/companies/#{id}/users")
29
+ end
30
+
31
+ def users_by_company_id(id)
32
+ get_users(url: "/companies", params: { company_id: id, type: "user" })
33
+ end
34
+
35
+ private def get_users(url:, params: {})
36
+ ClientCollectionProxy.new("users", finder_details: { url: url, params: params }, client: @client)
37
+ end
28
38
  end
29
39
  end
30
40
  end
@@ -1,3 +1,3 @@
1
1
  module Intercom #:nodoc:
2
- VERSION = "3.5.26"
2
+ VERSION = "3.6.0"
3
3
  end
@@ -17,6 +17,27 @@ module Intercom
17
17
  client.base_url.must_equal('https://api.intercom.io')
18
18
  end
19
19
 
20
+ it 'should be able to change the timeouts' do
21
+ prev = client.options(Intercom::Client.set_timeouts(open_timeout: 10, read_timeout: 15))
22
+ client.timeouts.must_equal(open_timeout: 10, read_timeout: 15)
23
+ client.options(prev)
24
+ client.timeouts.must_equal(open_timeout: 30, read_timeout: 90)
25
+ end
26
+
27
+ it 'should be able to change the open timeout individually' do
28
+ prev = client.options(Intercom::Client.set_timeouts(open_timeout: 50))
29
+ client.timeouts.must_equal(open_timeout: 50, read_timeout: 90)
30
+ client.options(prev)
31
+ client.timeouts.must_equal(open_timeout: 30, read_timeout: 90)
32
+ end
33
+
34
+ it 'should be able to change the read timeout individually' do
35
+ prev = client.options(Intercom::Client.set_timeouts(read_timeout: 50))
36
+ client.timeouts.must_equal(open_timeout: 30, read_timeout: 50)
37
+ client.options(prev)
38
+ client.timeouts.must_equal(open_timeout: 30, read_timeout: 90)
39
+ end
40
+
20
41
  it 'should raise on nil credentials' do
21
42
  proc { Client.new(app_id: nil, api_key: nil) }.must_raise MisconfiguredClientError
22
43
  end
@@ -1,36 +1,40 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
  describe Intercom::Company do
4
- let (:client) { Intercom::Client.new(app_id: 'app_id', api_key: 'api_key') }
4
+ let (:client) { Intercom::Client.new(app_id: "app_id", api_key: "api_key") }
5
5
 
6
- describe 'when no response raises error' do
7
- it 'on find' do
8
- client.expects(:get).with("/companies", {:company_id => '4'}).returns(nil)
9
- proc {client.companies.find(:company_id => '4')}.must_raise Intercom::HttpError
6
+ describe "when no response raises error" do
7
+ it "on find" do
8
+ client.expects(:get).with("/companies", {:company_id => "4"}).returns(nil)
9
+ proc {client.companies.find(:company_id => "4")}.must_raise Intercom::HttpError
10
10
  end
11
11
 
12
- it 'on find_all' do
12
+ it "on find_all" do
13
13
  client.expects(:get).with("/companies", {}).returns(nil)
14
14
  proc {client.companies.all.each {|company| }}.must_raise Intercom::HttpError
15
15
  end
16
16
 
17
- it 'on load' do
18
- client.expects(:get).with("/companies", {:company_id => '4'}).returns({'type' =>'user', 'id' =>'aaaaaaaaaaaaaaaaaaaaaaaa', 'company_id' => '4', 'name' => 'MyCo'})
19
- company = client.companies.find(:company_id => '4')
20
- client.expects(:get).with('/companies/aaaaaaaaaaaaaaaaaaaaaaaa', {}).returns(nil)
17
+ it "on load" do
18
+ client.expects(:get).with("/companies", {:company_id => "4"}).returns({"type" =>"user", "id" =>"aaaaaaaaaaaaaaaaaaaaaaaa", "company_id" => "4", "name" => "MyCo"})
19
+ company = client.companies.find(:company_id => "4")
20
+ client.expects(:get).with("/companies/aaaaaaaaaaaaaaaaaaaaaaaa", {}).returns(nil)
21
21
  proc {client.companies.load(company)}.must_raise Intercom::HttpError
22
22
  end
23
23
  end
24
24
 
25
- it 'gets users in a company' do
25
+ it "gets users in a company by intercom ID" do
26
26
  client.expects(:get).with("/companies/abc123/users", {}).returns(page_of_users(false))
27
- client.companies.users('abc123').each do |u|
28
- end
27
+ client.companies.users_by_intercom_company_id("abc123").each { |u| }
28
+ end
29
+
30
+ it "gets users in a company by external company ID" do
31
+ client.expects(:get).with("/companies", { company_id: "abc123", type: "user" }).returns(page_of_users(false))
32
+ client.companies.users_by_company_id("abc123").each { |u| }
29
33
  end
30
34
 
31
- it 'finds a company' do
35
+ it "finds a company" do
32
36
  client.expects(:get).with("/companies/531ee472cce572a6ec000006", {}).returns(test_company)
33
- company = client.companies.find(id: '531ee472cce572a6ec000006')
37
+ company = client.companies.find(id: "531ee472cce572a6ec000006")
34
38
  company.name.must_equal("Blue Sun")
35
39
  end
36
40
  end
@@ -30,14 +30,15 @@ describe 'Intercom::Request' do
30
30
  client.handle_rate_limit.must_equal(true)
31
31
  end
32
32
 
33
- it 'should call sleep for rate limit error three times' do
34
- # Use webmock to mock the HTTP request
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(target_base_url=uri, username: "ted", secret: "")
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(target_base_url=uri, username: "ted", secret: "")
41
+ }.must_raise(Intercom::RateLimitExceeded)
41
42
  end
42
43
 
43
44
  it 'should not call sleep for rate limit error' do
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.5.26
4
+ version: 3.6.0
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: 2018-09-10 00:00:00.000000000 Z
18
+ date: 2018-10-02 00:00:00.000000000 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: minitest
@@ -152,7 +152,6 @@ files:
152
152
  - lib/intercom/event.rb
153
153
  - lib/intercom/extended_api_operations/segments.rb
154
154
  - lib/intercom/extended_api_operations/tags.rb
155
- - lib/intercom/extended_api_operations/users.rb
156
155
  - lib/intercom/job.rb
157
156
  - lib/intercom/lib/dynamic_accessors.rb
158
157
  - lib/intercom/lib/dynamic_accessors_on_method_missing.rb
@@ -1,16 +0,0 @@
1
- require 'intercom/client_collection_proxy'
2
- require 'intercom/utils'
3
-
4
- module Intercom
5
- module ExtendedApiOperations
6
- module Users
7
- def users(id)
8
- collection_name = Utils.resource_class_to_collection_name(collection_class)
9
- finder_details = {}
10
- finder_details[:url] = "/#{collection_name}/#{id}/users"
11
- finder_details[:params] = {}
12
- ClientCollectionProxy.new("users", finder_details: finder_details, client: @client)
13
- end
14
- end
15
- end
16
- end