Skip to content

Commit 4a91065

Browse files
authored
Merge branch 'master' into ak/handle_rate_limit_retries
2 parents 5e5cc53 + 5519a12 commit 4a91065

File tree

8 files changed

+82
-21
lines changed

8 files changed

+82
-21
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#### Why?
2+
Why are you making this change?
3+
4+
#### How?
5+
Technical details on your change

.travis.yml

Lines changed: 0 additions & 10 deletions
This file was deleted.

Gemfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ gemspec
55

66
group :development, :test do
77
platforms :jruby do
8-
gem 'json-jruby'
98
gem 'jruby-openssl'
109
end
1110
end

README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ intercom.tags.untag(name: 'blue', users: [{user_id: "42ea2f1b93891f6a99000427"}
149149
intercom.tags.all.each {|tag| "#{tag.id} - #{tag.name}" }
150150
intercom.tags.all.map {|tag| tag.name }
151151
# Tag companies
152-
tag = intercom.tags.tag(name: 'blue', companies: [{id: "42ea2f1b93891f6a99000427"}])
152+
tag = intercom.tags.tag(name: 'blue', companies: [{company_id: "42ea2f1b93891f6a99000427"}])
153153
```
154154

155155
#### Segments
@@ -191,6 +191,11 @@ intercom.conversations.find_all(email: '[email protected]', type: 'user').each {|c
191191
intercom.conversations.find_all(email: '[email protected]', type: 'user', unread: false).each {|convo| ... }
192192
# Iterate over all unread conversations with a user based on the users email
193193
intercom.conversations.find_all(email: '[email protected]', type: 'user', unread: true).each {|convo| ... }
194+
# Iterate over all conversations for a user with their Intercom user ID
195+
intercom.conversations.find_all(intercom_user_id: '536e564f316c83104c000020', type: 'user').each {|convo| ... }
196+
# Iterate over all conversations for a lead
197+
# NOTE: to iterate over a lead's conversations you MUST use their Intercom User ID and type User
198+
intercom.conversations.find_all(intercom_user_id: lead.id, type: 'user').each {|convo| ... }
194199

195200
# FINDING A SINGLE CONVERSATION
196201
conversation = intercom.conversations.find(id: '1')
@@ -369,8 +374,31 @@ intercom.contacts.save(contact)
369374
# Find contacts by email
370375
contacts = intercom.contacts.find_all(email: "[email protected]")
371376

377+
# Using find to search for contacts by email
378+
contact_list = intercom.contacts.find(email: "[email protected]")
379+
# This returns a Contact object with type contact.list
380+
# Note: Multiple contacts can be returned in this list if there are multiple matching contacts found
381+
# #<Intercom::Contact:0x00007ff3a80789f8
382+
# @changed_fields=#<Set: {}>,
383+
# @contacts=
384+
# [{"type"=>"contact",
385+
# "id"=>"5b7fd9b683681ac52274b9c7",
386+
# "user_id"=>"05bc4d17-72cc-433e-88ae-0bf88db5d0e6",
387+
# "anonymous"=>true,
388+
# "email"=>"[email protected]",
389+
# ...}],
390+
# @custom_attributes={},
391+
# @limited=false,
392+
# @pages=#<Intercom::Pages:0x00007ff3a7413c58 @changed_fields=#<Set: {}>, @next=nil, @page=1, @per_page=50, @total_pages=1, @type="pages">,
393+
# @total_count=1,
394+
# @type="contact.list">
395+
# Access the contact's data
396+
contact_list.contacts.first
397+
372398
# Convert a contact into a user
373-
intercom.contacts.convert(contact, user)
399+
contact = intercom.contacts.find(id: "536e564f316c83104c000020")
400+
intercom.contacts.convert(contact, Intercom::User.new(email: email))
401+
# Using find with email will not work here. See https://github.com/intercom/intercom-ruby/issues/419 for more information
374402

375403
# Delete a contact
376404
intercom.contacts.delete(contact)

intercom.gemspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ Gem::Specification.new do |spec|
2525
spec.add_development_dependency "fakeweb", ["~> 1.3"]
2626
spec.add_development_dependency "pry"
2727

28-
spec.add_dependency 'json', '>= 1.8'
2928
spec.required_ruby_version = '>= 2.1.0'
3029
spec.add_development_dependency 'gem-release'
3130
end

lib/intercom/client.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Intercom
22
class MisconfiguredClientError < StandardError; end
33
class Client
44
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
66

77
class << self
88
def set_base_url(base_url)
@@ -12,6 +12,17 @@ def set_base_url(base_url)
1212
Proc.new { |obj| set_base_url(old_url).call(o) }
1313
end
1414
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
1526
end
1627

1728
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 @@ def initialize(app_id: 'my_app_id', api_key: 'my_api_key', token: nil, base_url:
2738
@base_url = base_url
2839
@rate_limit_details = {}
2940
@handle_rate_limit = handle_rate_limit
41+
@timeouts = {
42+
open_timeout: 30,
43+
read_timeout: 90
44+
}
3045
end
3146

3247
def admins
@@ -110,13 +125,17 @@ def validate_credentials!
110125

111126
def execute_request(request)
112127
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)
114129
ensure
115130
@rate_limit_details = request.rate_limit_details
116131
end
117132

118133
def base_url=(new_url)
119134
@base_url = new_url
120135
end
136+
137+
def timeouts=(timeouts)
138+
@timeouts = @timeouts.merge(timeouts)
139+
end
121140
end
122141
end

lib/intercom/request.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,25 @@ def self.default_headers
4646
{'Accept-Encoding' => 'gzip, deflate', 'Accept' => 'application/vnd.intercom.3+json', 'User-Agent' => "Intercom-Ruby/#{Intercom::VERSION}"}
4747
end
4848

49-
def client(uri)
49+
def client(uri, read_timeout:, open_timeout:)
5050
net = Net::HTTP.new(uri.host, uri.port)
5151
if uri.is_a?(URI::HTTPS)
5252
net.use_ssl = true
5353
net.verify_mode = OpenSSL::SSL::VERIFY_PEER
5454
net.ca_file = File.join(File.dirname(__FILE__), '../data/cacert.pem')
5555
end
56-
net.read_timeout = 90
57-
net.open_timeout = 30
56+
net.read_timeout = read_timeout
57+
net.open_timeout = open_timeout
5858
net
5959
end
6060

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)
6262
retries = 3
6363
base_uri = URI.parse(target_base_url)
6464
set_common_headers(net_http_method, base_uri)
6565
set_basic_auth(net_http_method, username, secret)
6666
begin
67-
client(base_uri).start do |http|
67+
client(base_uri, read_timeout: read_timeout, open_timeout: open_timeout).start do |http|
6868
begin
6969
response = http.request(net_http_method)
7070
set_rate_limit_details(response)

spec/unit/intercom/client_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,27 @@ module Intercom
1717
client.base_url.must_equal('https://api.intercom.io')
1818
end
1919

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+
2041
it 'should raise on nil credentials' do
2142
proc { Client.new(app_id: nil, api_key: nil) }.must_raise MisconfiguredClientError
2243
end

0 commit comments

Comments
 (0)