intercom 2.5.4 → 3.0.0b1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +0 -2
- data/README.md +97 -118
- data/changes.txt +3 -0
- data/intercom.gemspec +1 -1
- data/lib/intercom.rb +13 -153
- data/lib/intercom/admin.rb +0 -2
- data/lib/intercom/api_operations/convert.rb +5 -5
- data/lib/intercom/api_operations/delete.rb +4 -6
- data/lib/intercom/api_operations/find.rb +9 -15
- data/lib/intercom/api_operations/find_all.rb +16 -21
- data/lib/intercom/api_operations/list.rb +4 -10
- data/lib/intercom/api_operations/load.rb +6 -6
- data/lib/intercom/api_operations/save.rb +23 -28
- data/lib/intercom/client.rb +95 -0
- data/lib/intercom/{collection_proxy.rb → client_collection_proxy.rb} +5 -8
- data/lib/intercom/company.rb +0 -16
- data/lib/intercom/contact.rb +0 -11
- data/lib/intercom/conversation.rb +0 -10
- data/lib/intercom/event.rb +0 -2
- data/lib/intercom/extended_api_operations/segments.rb +13 -0
- data/lib/intercom/extended_api_operations/tags.rb +4 -5
- data/lib/intercom/extended_api_operations/users.rb +3 -5
- data/lib/intercom/message.rb +0 -2
- data/lib/intercom/note.rb +1 -10
- data/lib/intercom/options.rb +11 -0
- data/lib/intercom/request.rb +9 -5
- data/lib/intercom/segment.rb +0 -7
- data/lib/intercom/service/admin.rb +14 -0
- data/lib/intercom/service/base_service.rb +21 -0
- data/lib/intercom/service/company.rb +28 -0
- data/lib/intercom/service/contact.rb +22 -0
- data/lib/intercom/service/conversation.rb +31 -0
- data/lib/intercom/service/event.rb +14 -0
- data/lib/intercom/service/message.rb +14 -0
- data/lib/intercom/service/note.rb +22 -0
- data/lib/intercom/service/segment.rb +16 -0
- data/lib/intercom/service/subscription.rb +21 -0
- data/lib/intercom/service/tag.rb +30 -0
- data/lib/intercom/service/user.rb +28 -0
- data/lib/intercom/subscription.rb +0 -8
- data/lib/intercom/tag.rb +0 -16
- data/lib/intercom/user.rb +0 -16
- data/lib/intercom/utils.rb +0 -1
- data/lib/intercom/version.rb +1 -1
- data/spec/spec_helper.rb +117 -0
- data/spec/unit/intercom/admin_spec.rb +11 -4
- data/spec/unit/intercom/client_collection_proxy_spec.rb +35 -0
- data/spec/unit/intercom/client_spec.rb +21 -0
- data/spec/unit/intercom/company_spec.rb +24 -11
- data/spec/unit/intercom/contact_spec.rb +7 -5
- data/spec/unit/intercom/conversation_spec.rb +28 -0
- data/spec/unit/intercom/event_spec.rb +6 -5
- data/spec/unit/intercom/message_spec.rb +9 -8
- data/spec/unit/intercom/note_spec.rb +9 -2
- data/spec/unit/intercom/segment_spec.rb +12 -0
- data/spec/unit/intercom/subscription_spec.rb +7 -5
- data/spec/unit/intercom/tag_spec.rb +13 -7
- data/spec/unit/intercom/user_spec.rb +41 -33
- data/spec/unit/intercom_spec.rb +0 -83
- metadata +30 -20
- data/lib/intercom/api_operations/count.rb +0 -16
- data/lib/intercom/count.rb +0 -21
- data/lib/intercom/extended_api_operations/reply.rb +0 -16
- data/lib/intercom/generic_handlers/base_handler.rb +0 -22
- data/lib/intercom/generic_handlers/count.rb +0 -59
- data/lib/intercom/generic_handlers/tag.rb +0 -71
- data/lib/intercom/generic_handlers/tag_find_all.rb +0 -47
- data/lib/intercom/notification.rb +0 -20
- data/lib/intercom/traits/generic_handler_binding.rb +0 -29
- data/spec/unit/intercom/collection_proxy_spec.rb +0 -34
- data/spec/unit/intercom/notification_spec.rb +0 -68
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b15f68c468eb6d078694a471e95551e20e709b0e
|
4
|
+
data.tar.gz: 36819fd55b2cf627ec08006431cc64bb23094fe3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0603c5eaad0d8bd02c285cad70cfd0356492bad00d4119d10ed317aeb2817d7c5466aad6796481e9a4fc07133e07928263cd39f22381faca31857afd089e056f
|
7
|
+
data.tar.gz: d7aec86d14f294f9bfa1b1537c4079cd93e775d6abb54a982c0e7ea0415f22f439eae50dc546d7af2a1c36bb98a79829b323907c4c6de31e49e40a41e46daedc
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -9,9 +9,12 @@ Ruby bindings for the Intercom API (https://api.intercom.io).
|
|
9
9
|
For generating Intercom javascript script tags for Rails, please see https://github.com/intercom/intercom-rails
|
10
10
|
|
11
11
|
## Upgrading information
|
12
|
-
Version 2 of intercom-ruby is not backwards compatible with previous versions. Be sure to test this new version before deploying to production. One other change you will need to make as part of the upgrade is to set `Intercom.app_api_key` and not set `Intercom.api_key` (you can continue to use your existing API key).
|
13
12
|
|
14
|
-
|
13
|
+
Version 3 of intercom-ruby is not backwards compatible with previous versions.
|
14
|
+
|
15
|
+
Version 3 moves away from a global setup approach to the use of an Intercom Client.
|
16
|
+
|
17
|
+
This version of the gem is compatible with `Ruby 2.1` and above.
|
15
18
|
|
16
19
|
## Installation
|
17
20
|
|
@@ -19,24 +22,24 @@ This version of the gem is compatible with `Ruby 2.1`, `Ruby 2.0` & `Ruby 1.9.3`
|
|
19
22
|
|
20
23
|
Using bundler:
|
21
24
|
|
22
|
-
gem 'intercom', "~>
|
25
|
+
gem 'intercom', "~> 3.0.0b1"
|
23
26
|
|
24
27
|
## Basic Usage
|
25
28
|
|
26
|
-
### Configure your
|
29
|
+
### Configure your client
|
27
30
|
|
28
31
|
```ruby
|
29
|
-
Intercom.app_id
|
30
|
-
Intercom.app_api_key = "my-super-crazy-api-key"
|
32
|
+
intercom = Intercom::Client.new(app_id: 'my_app_id', api_key: 'my_api_key')
|
31
33
|
```
|
32
34
|
|
33
|
-
You can get your `app_id` from the URL when you're logged into Intercom (it's the alphanumeric just after `/apps/`) and your API key from the API keys settings page (under your app settings in Intercom).
|
35
|
+
You can get your `app_id` from the URL when you're logged into Intercom (it's the alphanumeric just after `/apps/`) and your API key from the API keys integration settings page (under your app settings - integrations in Intercom).
|
34
36
|
|
35
37
|
### Resources
|
36
38
|
|
37
39
|
Resources this API supports:
|
38
40
|
|
39
41
|
https://api.intercom.io/users
|
42
|
+
https://api.intercom.io/contacts
|
40
43
|
https://api.intercom.io/companies
|
41
44
|
https://api.intercom.io/tags
|
42
45
|
https://api.intercom.io/notes
|
@@ -44,129 +47,120 @@ Resources this API supports:
|
|
44
47
|
https://api.intercom.io/events
|
45
48
|
https://api.intercom.io/conversations
|
46
49
|
https://api.intercom.io/messages
|
47
|
-
https://api.intercom.io/counts
|
48
50
|
https://api.intercom.io/subscriptions
|
49
51
|
|
50
|
-
Additionally, the library can handle incoming webhooks from Intercom and convert to `Intercom::` models.
|
51
|
-
|
52
52
|
### Examples
|
53
53
|
|
54
54
|
#### Users
|
55
55
|
|
56
56
|
```ruby
|
57
57
|
# Find user by email
|
58
|
-
user =
|
58
|
+
user = intercom.users.find(:email => "bob@example.com")
|
59
59
|
# Find user by user_id
|
60
|
-
user =
|
60
|
+
user = intercom.users.find(:user_id => "1")
|
61
61
|
# Find user by id
|
62
|
-
user =
|
62
|
+
user = intercom.users.find(:id => "1")
|
63
63
|
# Create a user
|
64
|
-
user =
|
64
|
+
user = intercom.users.create(:email => "bob@example.com", :name => "Bob Smith", :signed_up_at => Time.now.to_i)
|
65
65
|
# Delete a user
|
66
|
-
deleted_user =
|
66
|
+
deleted_user = intercom.users.find(:id => "1").delete
|
67
67
|
# Update custom_attributes for a user
|
68
|
-
user.custom_attributes["average_monthly_spend"] = 1234.56
|
68
|
+
user.custom_attributes["average_monthly_spend"] = 1234.56
|
69
|
+
intercom.users.save(user)
|
69
70
|
# Perform incrementing
|
70
|
-
user.increment('karma')
|
71
|
+
user.increment('karma')
|
72
|
+
intercom.users.save(user)
|
71
73
|
# Iterate over all users
|
72
|
-
|
73
|
-
|
74
|
+
intercom.users.all.each {|user| puts %Q(#{user.email} - #{user.custom_attributes["average_monthly_spend"]}) }
|
75
|
+
intercom.users.all.map {|user| user.email }
|
74
76
|
```
|
75
77
|
|
76
78
|
#### Admins
|
77
79
|
```ruby
|
78
80
|
# Iterate over all admins
|
79
|
-
|
81
|
+
intercom.admins.all.each {|admin| puts admin.email }
|
80
82
|
```
|
81
83
|
|
82
84
|
#### Companies
|
83
85
|
```ruby
|
84
86
|
# Add a user to one or more companies
|
85
|
-
user =
|
86
|
-
user.companies = [{:company_id => 6, :name => "Intercom"}, {:company_id => 9, :name => "Test Company"}]
|
87
|
+
user = intercom.users.find(:email => "bob@example.com")
|
88
|
+
user.companies = [{:company_id => 6, :name => "Intercom"}, {:company_id => 9, :name => "Test Company"}]
|
89
|
+
intercom.users.save(user)
|
87
90
|
# You can also pass custom attributes within a company as you do this
|
88
|
-
user.companies = [{:id => 6, :name => "Intercom", :custom_attributes => {:referral_source => "Google"} } ]
|
91
|
+
user.companies = [{:id => 6, :name => "Intercom", :custom_attributes => {:referral_source => "Google"} } ]
|
92
|
+
intercom.users.save(user)
|
89
93
|
# Find a company by company_id
|
90
|
-
company =
|
94
|
+
company = intercom.companies.find(:company_id => "44")
|
91
95
|
# Find a company by name
|
92
|
-
company =
|
96
|
+
company = intercom.companies.find(:name => "Some company")
|
93
97
|
# Find a company by id
|
94
|
-
company =
|
98
|
+
company = intercom.companies.find(:id => "41e66f0313708347cb0000d0")
|
95
99
|
# Update a company
|
96
|
-
company.name = 'Updated company name'
|
100
|
+
company.name = 'Updated company name'
|
101
|
+
intercom.companies.save(company)
|
97
102
|
# Iterate over all companies
|
98
|
-
|
99
|
-
|
103
|
+
intercom.companies.all.each {|company| puts %Q(#{company.name} - #{company.custom_attributes["referral_source"]}) }
|
104
|
+
intercom.companies.all.map {|company| company.name }
|
100
105
|
# Get a list of users in a company
|
101
|
-
company.
|
106
|
+
intercom.companies.users(company.id)
|
102
107
|
```
|
103
108
|
|
104
109
|
#### Tags
|
105
110
|
```ruby
|
106
111
|
# Tag users
|
107
|
-
tag =
|
112
|
+
tag = intercom.tags.tag(name: 'blue', users: [{email: "test1@example.com"}])
|
108
113
|
# Untag users
|
109
|
-
|
114
|
+
intercom.tags.untag_users('blue', users: [{user_id: "42ea2f1b93891f6a99000427"}])
|
110
115
|
# Iterate over all tags
|
111
|
-
|
112
|
-
|
113
|
-
# Iterate over all tags for user
|
114
|
-
Intercom::Tag.find_all_for_user(:id => '53357ddc3c776629e0000029')
|
115
|
-
Intercom::Tag.find_all_for_user(:email => 'declan+declan@intercom.io')
|
116
|
-
Intercom::Tag.find_all_for_user(:user_id => '3')
|
116
|
+
intercom.tags.all.each {|tag| "#{tag.id} - #{tag.name}" }
|
117
|
+
intercom.tags.all.map {|tag| tag.name }
|
117
118
|
# Tag companies
|
118
|
-
tag =
|
119
|
-
# Untag companies
|
120
|
-
Intercom::Tag.untag_companies('blue', ["42ea2f1b93891f6a99000427"])
|
121
|
-
# Iterate over all tags for company
|
122
|
-
Intercom::Tag.find_all_for_company(:id => '43357e2c3c77661e25000026')
|
123
|
-
Intercom::Tag.find_all_for_company(:company_id => '6')
|
119
|
+
tag = intercom.tags.tag(name: 'blue', companies: [{id: "42ea2f1b93891f6a99000427"}])
|
124
120
|
```
|
125
121
|
|
126
122
|
#### Segments
|
127
123
|
```ruby
|
128
124
|
# Find a segment
|
129
|
-
segment =
|
130
|
-
# Update a segment
|
131
|
-
segment.name = 'Updated name'; segment.save
|
125
|
+
segment = intercom.segments.find(:id => segment_id)
|
132
126
|
# Iterate over all segments
|
133
|
-
|
127
|
+
intercom.segments.all.each {|segment| puts "id: #{segment.id} name: #{segment.name}"}
|
134
128
|
```
|
135
129
|
|
136
130
|
#### Notes
|
137
131
|
```ruby
|
138
132
|
# Find a note by id
|
139
|
-
note =
|
133
|
+
note = intercom.notes.find(:id => note)
|
140
134
|
# Create a note for a user
|
141
|
-
note =
|
135
|
+
note = intercom.notes.create(:body => "<p>Text for the note</p>", :email => 'joe@example.com')
|
142
136
|
# Iterate over all notes for a user via their email address
|
143
|
-
|
137
|
+
intercom.notes.find_all(:email => 'joe@example.com').each {|note| puts note.body}
|
144
138
|
# Iterate over all notes for a user via their user_id
|
145
|
-
|
139
|
+
intercom.notes.find_all(:user_id => '123').each {|note| puts note.body}
|
146
140
|
```
|
147
141
|
|
148
142
|
#### Conversations
|
149
143
|
```ruby
|
150
144
|
# FINDING CONVERSATIONS FOR AN ADMIN
|
151
145
|
# Iterate over all conversations (open and closed) assigned to an admin
|
152
|
-
|
146
|
+
intercom.conversations.find_all(:type => 'admin', :id => '7').each do {|convo| ... }
|
153
147
|
# Iterate over all open conversations assigned to an admin
|
154
|
-
|
148
|
+
intercom.conversations.find_all(:type => 'admin', :id => 7, :open => true).each do {|convo| ... }
|
155
149
|
# Iterate over closed conversations assigned to an admin
|
156
|
-
|
150
|
+
intercom.conversations.find_all(:type => 'admin', :id => 7, :open => false).each do {|convo| ... }
|
157
151
|
# Iterate over closed conversations for assigned an admin, before a certain moment in time
|
158
|
-
|
152
|
+
intercom.conversations.find_all(:type => 'admin', :id => 7, :open => false, :before => 1374844930).each do {|convo| ... }
|
159
153
|
|
160
154
|
# FINDING CONVERSATIONS FOR A USER
|
161
155
|
# Iterate over all conversations (read + unread, correct) with a user based on the users email
|
162
|
-
|
156
|
+
intercom.conversations.find_all(:email => 'joe@example.com', :type => 'user').each do {|convo| ... }
|
163
157
|
# Iterate over through all conversations (read + unread) with a user based on the users email
|
164
|
-
|
158
|
+
intercom.conversations.find_all(:email => 'joe@example.com', :type => 'user', :unread => false).each do {|convo| ... }
|
165
159
|
# Iterate over all unread conversations with a user based on the users email
|
166
|
-
|
160
|
+
intercom.conversations.find_all(:email => 'joe@example.com', :type => 'user', :unread => true).each do {|convo| ... }
|
167
161
|
|
168
162
|
# FINDING A SINGLE CONVERSATION
|
169
|
-
conversation =
|
163
|
+
conversation = intercom.conversations.find(:id => '1')
|
170
164
|
|
171
165
|
# INTERACTING WITH THE PARTS OF A CONVERSATION
|
172
166
|
# Getting the subject of a part (only applies to email-based conversations)
|
@@ -178,49 +172,26 @@ conversation.conversation_parts[1].body
|
|
178
172
|
|
179
173
|
# REPLYING TO CONVERSATIONS
|
180
174
|
# User (identified by email) replies with a comment
|
181
|
-
|
175
|
+
intercom.conversations.reply(:id => conversation.id, :type => 'user', :email => 'joe@example.com', :message_type => 'comment', :body => 'foo')
|
182
176
|
# Admin (identified by email) replies with a comment
|
183
|
-
|
177
|
+
intercom.conversations.reply(:id => conversation.id, :type => 'admin', :email => 'bob@example.com', :message_type => 'comment', :body => 'bar')
|
184
178
|
|
185
179
|
# MARKING A CONVERSATION AS READ
|
186
|
-
conversation.
|
187
|
-
conversation.save
|
188
|
-
```
|
189
|
-
|
190
|
-
#### Counts
|
191
|
-
```ruby
|
192
|
-
# Get Conversation per Admin
|
193
|
-
conversation_counts_for_each_admin = Intercom::Count.conversation_counts_for_each_admin
|
194
|
-
conversation_counts_for_each_admin.each{|count| puts "Admin: #{count.name} (id: #{count.id}) Open: #{count.open} Closed: #{count.closed}" }
|
195
|
-
# Get User Tag Count Object
|
196
|
-
Intercom::Count.user_counts_for_each_tag
|
197
|
-
# Get User Segment Count Object
|
198
|
-
Intercom::Count.user_counts_for_each_segment
|
199
|
-
# Get Company Segment Count Object
|
200
|
-
Intercom::Count.company_counts_for_each_segment
|
201
|
-
# Get Company Tag Count Object
|
202
|
-
Intercom::Count.company_counts_for_each_tag
|
203
|
-
# Get Company User Count Object
|
204
|
-
Intercom::Count.company_counts_for_each_user
|
205
|
-
# Get total count of companies, users, segments or tags across app
|
206
|
-
Intercom::Company.count
|
207
|
-
Intercom::User.count
|
208
|
-
Intercom::Segment.count
|
209
|
-
Intercom::Tag.count
|
180
|
+
intercom.conversations.mark_read(conversation.id)
|
210
181
|
```
|
211
182
|
|
212
183
|
#### Full loading of an embedded entity
|
213
184
|
```ruby
|
214
185
|
# Given a conversation with a partial user, load the full user. This can be
|
215
186
|
# done for any entity
|
216
|
-
conversation.user
|
187
|
+
intercom.users.load(conversation.user)
|
217
188
|
```
|
218
189
|
|
219
190
|
#### Sending messages
|
220
191
|
```ruby
|
221
192
|
|
222
193
|
# InApp message from admin to user
|
223
|
-
|
194
|
+
intercom.messages.create({
|
224
195
|
:message_type => 'inapp',
|
225
196
|
:body => "What's up :)",
|
226
197
|
:from => {
|
@@ -234,7 +205,7 @@ Intercom::Message.create({
|
|
234
205
|
})
|
235
206
|
|
236
207
|
# Email message from admin to user
|
237
|
-
|
208
|
+
intercom.messages.create({
|
238
209
|
:message_type => 'email',
|
239
210
|
:subject => 'Hey there',
|
240
211
|
:body => "What's up :)",
|
@@ -250,18 +221,41 @@ Intercom::Message.create({
|
|
250
221
|
})
|
251
222
|
|
252
223
|
# Message from a user
|
253
|
-
|
224
|
+
intercom.messages.create({
|
254
225
|
:from => {
|
255
226
|
:type => "user",
|
256
227
|
:id => "536e564f316c83104c000020"
|
257
228
|
},
|
258
229
|
:body => "halp"
|
259
230
|
})
|
231
|
+
|
232
|
+
# Message from admin to contact
|
233
|
+
|
234
|
+
intercom.messages.create({
|
235
|
+
:body => "How can I help :)",
|
236
|
+
:from => {
|
237
|
+
:type => "admin",
|
238
|
+
:id => "1234"
|
239
|
+
},
|
240
|
+
:to => {
|
241
|
+
:type => "contact",
|
242
|
+
:id => "536e5643as316c83104c400671"
|
243
|
+
}
|
244
|
+
})
|
245
|
+
|
246
|
+
# Message from a contact
|
247
|
+
intercom.messages.create({
|
248
|
+
:from => {
|
249
|
+
:type => "contact",
|
250
|
+
:id => "536e5643as316c83104c400671"
|
251
|
+
},
|
252
|
+
:body => "halp"
|
253
|
+
})
|
260
254
|
```
|
261
255
|
|
262
256
|
#### Events
|
263
257
|
```ruby
|
264
|
-
|
258
|
+
intercom.events.create(
|
265
259
|
:event_name => "invited-friend", :created_at => Time.now.to_i,
|
266
260
|
:email => user.email,
|
267
261
|
:metadata => {
|
@@ -275,7 +269,7 @@ Intercom::Event.create(
|
|
275
269
|
Metadata Objects support a few simple types that Intercom can present on your behalf
|
276
270
|
|
277
271
|
```ruby
|
278
|
-
|
272
|
+
intercom.events.create(:event_name => "placed-order", :email => current_user.email,
|
279
273
|
:created_at => 1403001013,
|
280
274
|
:metadata => {
|
281
275
|
:order_date => Time.now.to_i,
|
@@ -306,14 +300,17 @@ The metadata key values in the example are treated as follows-
|
|
306
300
|
|
307
301
|
```ruby
|
308
302
|
# Create a contact
|
309
|
-
contact =
|
303
|
+
contact = intercom.contacts.create(email: "some_contact@example.com")
|
310
304
|
|
311
305
|
# Update a contact
|
312
306
|
contact.custom_attributes['foo'] = 'bar'
|
313
|
-
|
307
|
+
intercom.contacts.save(contact)
|
314
308
|
|
315
309
|
# Find contacts by email
|
316
|
-
contacts =
|
310
|
+
contacts = intercom.contacts.find_all(email: "some_contact@example.com")
|
311
|
+
|
312
|
+
# Convert a contact into a user
|
313
|
+
intercom.contacts.convert(contact, user)
|
317
314
|
```
|
318
315
|
|
319
316
|
### Subscriptions
|
@@ -322,33 +319,15 @@ Subscribe to events in Intercom to receive webhooks.
|
|
322
319
|
|
323
320
|
```ruby
|
324
321
|
# create a subscription
|
325
|
-
|
322
|
+
intercom.subscriptions.create(:url => "http://example.com", :topics => ["user.created"])
|
326
323
|
|
327
324
|
# fetch a subscription
|
328
|
-
|
325
|
+
intercom.subscriptions.find(:id => "nsub_123456789")
|
329
326
|
|
330
327
|
# list subscriptions
|
331
|
-
|
332
|
-
```
|
333
|
-
|
334
|
-
### Webhooks
|
335
|
-
|
336
|
-
```ruby
|
337
|
-
# create a payload from the notification hash (from json).
|
338
|
-
payload = Intercom::Notification.new(notification_hash)
|
339
|
-
|
340
|
-
payload.type
|
341
|
-
# => 'user.created'
|
342
|
-
|
343
|
-
payload.model_type
|
344
|
-
# => Intercom::User
|
345
|
-
|
346
|
-
user = payload.model
|
347
|
-
# => Instance of Intercom::User
|
328
|
+
intercom.subscriptions.all
|
348
329
|
```
|
349
330
|
|
350
|
-
Note that models generated from webhook notifications might differ slightly from models directly acquired via the API. If this presents a problem, calling `payload.load` will load the model from the API using the `id` field.
|
351
|
-
|
352
331
|
### Errors
|
353
332
|
You do not need to deal with the HTTP response from an API call directly. If there is an unsuccessful response then an error that is a subclass of Intercom:Error will be raised. If desired, you can get at the http_code of an Intercom::Error via its `http_code` method.
|
354
333
|
|
@@ -370,9 +349,9 @@ Intercom::HttpError # Raised when response object is unexpectedly nil
|
|
370
349
|
|
371
350
|
### Rate Limiting
|
372
351
|
|
373
|
-
Calling `
|
352
|
+
Calling your client's `rate_limit_details` returns a Hash that contains details about your app's current rate limit.
|
374
353
|
|
375
354
|
```ruby
|
376
|
-
|
355
|
+
intercom.rate_limit_details
|
377
356
|
#=> {:limit=>180, :remaining=>179, :reset_at=>2014-10-07 14:58:00 +0100}
|
378
357
|
```
|
data/changes.txt
CHANGED
data/intercom.gemspec
CHANGED
data/lib/intercom.rb
CHANGED
@@ -1,4 +1,17 @@
|
|
1
1
|
require "intercom/version"
|
2
|
+
require 'intercom/service/admin'
|
3
|
+
require 'intercom/service/company'
|
4
|
+
require 'intercom/service/contact'
|
5
|
+
require 'intercom/service/conversation'
|
6
|
+
require 'intercom/service/event'
|
7
|
+
require 'intercom/service/message'
|
8
|
+
require 'intercom/service/note'
|
9
|
+
require 'intercom/service/subscription'
|
10
|
+
require 'intercom/service/segment'
|
11
|
+
require 'intercom/service/tag'
|
12
|
+
require 'intercom/service/user'
|
13
|
+
require 'intercom/options'
|
14
|
+
require 'intercom/client'
|
2
15
|
require "intercom/contact"
|
3
16
|
require "intercom/user"
|
4
17
|
require "intercom/company"
|
@@ -9,10 +22,8 @@ require "intercom/event"
|
|
9
22
|
require "intercom/conversation"
|
10
23
|
require "intercom/message"
|
11
24
|
require "intercom/admin"
|
12
|
-
require "intercom/count"
|
13
25
|
require "intercom/request"
|
14
26
|
require "intercom/subscription"
|
15
|
-
require "intercom/notification"
|
16
27
|
require "intercom/utils"
|
17
28
|
require "intercom/errors"
|
18
29
|
require "json"
|
@@ -21,156 +32,5 @@ require "json"
|
|
21
32
|
# Intercom is a customer relationship management and messaging tool for web app owners
|
22
33
|
#
|
23
34
|
# This library provides ruby bindings for the Intercom API (https://api.intercom.io)
|
24
|
-
#
|
25
|
-
# == Basic Usage
|
26
|
-
# === Configure Intercom with your access credentials
|
27
|
-
# Intercom.app_id = "my_app_id"
|
28
|
-
# Intercom.app_api_key = "my_api_key"
|
29
|
-
# === Make requests to the API
|
30
|
-
# Intercom::User.find(:email => "bob@example.com")
|
31
|
-
#
|
32
35
|
module Intercom
|
33
|
-
@hostname = "api.intercom.io"
|
34
|
-
@protocol = "https"
|
35
|
-
|
36
|
-
@endpoints = nil
|
37
|
-
@current_endpoint = nil
|
38
|
-
@app_id = nil
|
39
|
-
@app_api_key = nil
|
40
|
-
@rate_limit_details = {}
|
41
|
-
|
42
|
-
def self.app_id=(app_id)
|
43
|
-
@app_id = app_id
|
44
|
-
end
|
45
|
-
|
46
|
-
def self.app_id
|
47
|
-
@app_id
|
48
|
-
end
|
49
|
-
|
50
|
-
def self.app_api_key=(app_api_key)
|
51
|
-
@app_api_key = app_api_key
|
52
|
-
end
|
53
|
-
|
54
|
-
def self.app_api_key
|
55
|
-
@app_api_key
|
56
|
-
end
|
57
|
-
|
58
|
-
def self.rate_limit_details=(rate_limit_details)
|
59
|
-
@rate_limit_details = rate_limit_details
|
60
|
-
end
|
61
|
-
|
62
|
-
def self.rate_limit_details
|
63
|
-
@rate_limit_details
|
64
|
-
end
|
65
|
-
|
66
|
-
# This method is obsolete and used to warn of backwards incompatible changes on upgrading
|
67
|
-
def self.api_key=(val)
|
68
|
-
raise ArgumentError, "#{compatibility_warning_text} #{compatibility_workaround_text} #{related_docs_text}"
|
69
|
-
end
|
70
|
-
|
71
|
-
private
|
72
|
-
|
73
|
-
def self.target_base_url
|
74
|
-
raise ArgumentError, "#{configuration_required_text} #{related_docs_text}" if [@app_id, @app_api_key].any?(&:nil?)
|
75
|
-
basic_auth_part = "#{@app_id}:#{@app_api_key}@"
|
76
|
-
current_endpoint.gsub(/(https?:\/\/)(.*)/, "\\1#{basic_auth_part}\\2")
|
77
|
-
end
|
78
|
-
|
79
|
-
def self.related_docs_text
|
80
|
-
"See https://github.com/intercom/intercom-ruby for usage examples."
|
81
|
-
end
|
82
|
-
|
83
|
-
def self.compatibility_warning_text
|
84
|
-
"It looks like you are upgrading from an older version of the intercom-ruby gem. Please note that this new version (#{Intercom::VERSION}) is not backwards compatible. "
|
85
|
-
end
|
86
|
-
|
87
|
-
def self.compatibility_workaround_text
|
88
|
-
"To get rid of this error please set Intercom.app_api_key and don't set Intercom.api_key."
|
89
|
-
end
|
90
|
-
|
91
|
-
def self.configuration_required_text
|
92
|
-
"You must set both Intercom.app_id and Intercom.app_api_key to use this client."
|
93
|
-
end
|
94
|
-
|
95
|
-
def self.send_request_to_path(request)
|
96
|
-
request.execute(target_base_url)
|
97
|
-
rescue Intercom::ServiceUnavailableError => e
|
98
|
-
if endpoints.length > 1
|
99
|
-
retry_on_alternative_endpoint(request)
|
100
|
-
else
|
101
|
-
raise e
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
def self.retry_on_alternative_endpoint(request)
|
106
|
-
@current_endpoint = alternative_random_endpoint
|
107
|
-
request.execute(target_base_url)
|
108
|
-
end
|
109
|
-
|
110
|
-
def self.current_endpoint
|
111
|
-
return @current_endpoint if @current_endpoint && @endpoint_randomized_at > (Time.now - (60 * 5))
|
112
|
-
@endpoint_randomized_at = Time.now
|
113
|
-
@current_endpoint = random_endpoint
|
114
|
-
end
|
115
|
-
|
116
|
-
def self.random_endpoint
|
117
|
-
endpoints.shuffle.first
|
118
|
-
end
|
119
|
-
|
120
|
-
def self.alternative_random_endpoint
|
121
|
-
(endpoints.shuffle - [@current_endpoint]).first
|
122
|
-
end
|
123
|
-
|
124
|
-
def self.post(path, payload_hash)
|
125
|
-
send_request_to_path(Intercom::Request.post(path, payload_hash))
|
126
|
-
end
|
127
|
-
|
128
|
-
def self.delete(path, payload_hash)
|
129
|
-
send_request_to_path(Intercom::Request.delete(path, payload_hash))
|
130
|
-
end
|
131
|
-
|
132
|
-
def self.put(path, payload_hash)
|
133
|
-
send_request_to_path(Intercom::Request.put(path, payload_hash))
|
134
|
-
end
|
135
|
-
|
136
|
-
def self.get(path, params)
|
137
|
-
send_request_to_path(Intercom::Request.get(path, params))
|
138
|
-
end
|
139
|
-
|
140
|
-
def self.check_required_params(params, path=nil) #nodoc
|
141
|
-
return if path.eql?("users")
|
142
|
-
raise ArgumentError.new("Expected params Hash, got #{params.class}") unless params.is_a?(Hash)
|
143
|
-
raise ArgumentError.new("Either email or user_id must be specified") unless params.keys.any? { |key| %W(email user_id).include?(key.to_s) }
|
144
|
-
end
|
145
|
-
|
146
|
-
def self.protocol #nodoc
|
147
|
-
@protocol
|
148
|
-
end
|
149
|
-
|
150
|
-
def self.protocol=(override) #nodoc
|
151
|
-
@protocol = override
|
152
|
-
end
|
153
|
-
|
154
|
-
def self.hostname #nodoc
|
155
|
-
@hostname
|
156
|
-
end
|
157
|
-
|
158
|
-
def self.hostname=(override) #nodoc
|
159
|
-
@hostname = override
|
160
|
-
end
|
161
|
-
|
162
|
-
def self.endpoint=(endpoint) #nodoc
|
163
|
-
self.endpoints = [endpoint]
|
164
|
-
@current_endpoint = nil
|
165
|
-
end
|
166
|
-
|
167
|
-
def self.endpoints=(endpoints) #nodoc
|
168
|
-
@endpoints = endpoints
|
169
|
-
@current_endpoint = nil
|
170
|
-
end
|
171
|
-
|
172
|
-
def self.endpoints
|
173
|
-
@endpoints || ["#{@protocol}://#{hostname}"]
|
174
|
-
end
|
175
|
-
|
176
36
|
end
|