intercom 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +0 -8
  3. data/Gemfile +3 -0
  4. data/README.md +208 -52
  5. data/changes.txt +3 -0
  6. data/intercom.gemspec +2 -2
  7. data/lib/ext/hash.rb +18 -0
  8. data/lib/intercom.rb +38 -43
  9. data/lib/intercom/api_operations/count.rb +16 -0
  10. data/lib/intercom/api_operations/delete.rb +15 -0
  11. data/lib/intercom/api_operations/find.rb +22 -0
  12. data/lib/intercom/api_operations/find_all.rb +33 -0
  13. data/lib/intercom/api_operations/list.rb +17 -0
  14. data/lib/intercom/api_operations/load.rb +15 -0
  15. data/lib/intercom/api_operations/save.rb +44 -0
  16. data/lib/intercom/collection_proxy.rb +66 -0
  17. data/lib/intercom/company.rb +29 -0
  18. data/lib/intercom/conversation.rb +15 -0
  19. data/lib/intercom/count.rb +21 -0
  20. data/lib/intercom/errors.rb +52 -0
  21. data/lib/intercom/event.rb +4 -101
  22. data/lib/intercom/extended_api_operations/reply.rb +16 -0
  23. data/lib/intercom/extended_api_operations/tags.rb +14 -0
  24. data/lib/intercom/extended_api_operations/users.rb +17 -0
  25. data/lib/intercom/generic_handlers/base_handler.rb +22 -0
  26. data/lib/intercom/generic_handlers/count.rb +59 -0
  27. data/lib/intercom/generic_handlers/tag.rb +71 -0
  28. data/lib/intercom/generic_handlers/tag_find_all.rb +47 -0
  29. data/lib/intercom/lib/dynamic_accessors.rb +59 -0
  30. data/lib/intercom/lib/dynamic_accessors_on_method_missing.rb +53 -0
  31. data/lib/intercom/lib/flat_store.rb +31 -0
  32. data/lib/intercom/lib/typed_json_deserializer.rb +52 -0
  33. data/lib/intercom/message.rb +9 -0
  34. data/lib/intercom/note.rb +14 -42
  35. data/lib/intercom/request.rb +40 -4
  36. data/lib/intercom/segment.rb +14 -0
  37. data/lib/intercom/tag.rb +19 -78
  38. data/lib/intercom/traits/api_resource.rb +120 -0
  39. data/lib/intercom/traits/dirty_tracking.rb +33 -0
  40. data/lib/intercom/traits/generic_handler_binding.rb +29 -0
  41. data/lib/intercom/traits/incrementable_attributes.rb +23 -0
  42. data/lib/intercom/user.rb +25 -361
  43. data/lib/intercom/utils.rb +50 -0
  44. data/lib/intercom/version.rb +1 -1
  45. data/spec/spec_helper.rb +64 -33
  46. data/spec/unit/intercom/collection_proxy_spec.rb +34 -0
  47. data/spec/unit/intercom/event_spec.rb +25 -0
  48. data/spec/unit/intercom/{flat_store_spec.rb → lib/flat_store_spec.rb} +7 -7
  49. data/spec/unit/intercom/note_spec.rb +5 -4
  50. data/spec/unit/intercom/tag_spec.rb +3 -3
  51. data/spec/unit/intercom/traits/api_resource_spec.rb +79 -0
  52. data/spec/unit/intercom/user_spec.rb +101 -119
  53. data/spec/unit/intercom_spec.rb +7 -7
  54. metadata +50 -26
  55. data/lib/intercom/flat_store.rb +0 -27
  56. data/lib/intercom/hashable_object.rb +0 -22
  57. data/lib/intercom/impression.rb +0 -63
  58. data/lib/intercom/message_thread.rb +0 -189
  59. data/lib/intercom/requires_parameters.rb +0 -10
  60. data/lib/intercom/social_profile.rb +0 -24
  61. data/lib/intercom/unix_timestamp_unwrapper.rb +0 -12
  62. data/lib/intercom/user_collection_proxy.rb +0 -52
  63. data/lib/intercom/user_resource.rb +0 -82
  64. data/spec/integration/fixtures/v1-user.json +0 -45
  65. data/spec/integration/fixtures/v1-users-impression.json +0 -3
  66. data/spec/integration/fixtures/v1-users-message_thread.json +0 -44
  67. data/spec/integration/fixtures/v1-users-message_threads.json +0 -46
  68. data/spec/integration/fixtures/v1-users-note.json +0 -49
  69. data/spec/integration/fixtures/v1-users.json +0 -144
  70. data/spec/integration/intercom_api_integration_spec.rb +0 -134
  71. data/spec/unit/intercom/impression_spec.rb +0 -18
  72. data/spec/unit/intercom/message_thread_spec.rb +0 -74
  73. data/spec/unit/intercom/user_collection_proxy_spec.rb +0 -46
  74. data/spec/unit/intercom/user_event_spec.rb +0 -83
  75. data/spec/unit/intercom/user_resource_spec.rb +0 -13
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b43f8adfa70b2667927c1f3f374e2eee67c2d9db
4
- data.tar.gz: 558e8898118669cd4b917a9bdf7b6fa4f88a9052
3
+ metadata.gz: 19d0a3f10338e77b6c5f8e60e17ee6de1114a340
4
+ data.tar.gz: f6727eea4a0bcc322852e3b0ea412a02e287f3e1
5
5
  SHA512:
6
- metadata.gz: 0aa42bb76c0280b504c943b9b407ed34ad46141064df6f144e0d8513127464535a8fe6b836f3a7092daf48fddc02a5c898a9504a8d12ee14f5ad913ec754ebf2
7
- data.tar.gz: 02864eb97a2ca575e1580f2b65b3f1d5b18b46d2440118fd9c7912bfd7a6db1276d8768bea07b21f0a5ee3238d0d6b66cbca5c3b7b41fb697c6a73c18899f388
6
+ metadata.gz: 873daa77201675582588235753bf341518ffb3df63eb994fb5e64aac6b43cad2b3e99c13ab865b0506f242402ba0bd87e534d641796924a6cb0eb972e27880a2
7
+ data.tar.gz: e3bd531c5f11afe27a1df6e0efc9289edf20e34a2b8625821a8635637845574da24994b955b3622634d10fef03332a6e0b74e20d2ee74f580a5a249547cf8fe3
data/.travis.yml CHANGED
@@ -1,11 +1,3 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.8.7
4
- - ree
5
- - 1.9.2
6
- - 1.9.3
7
3
  - 2.0.0
8
- - jruby-18mode
9
- - jruby-19mode
10
- - rbx-18mode
11
- - rbx-19mode
data/Gemfile CHANGED
@@ -1,9 +1,12 @@
1
1
  source "http://rubygems.org"
2
+ ruby "2.0.0"
2
3
 
3
4
  gemspec
4
5
 
5
6
  group :development, :test do
6
7
  gem 'fakeweb', "~> 1.3"
8
+ # gem 'm'
9
+ # gem 'pry-byebug'
7
10
  platforms :jruby do
8
11
  gem 'json-jruby'
9
12
  gem 'jruby-openssl'
data/README.md CHANGED
@@ -8,13 +8,16 @@ Ruby bindings for the Intercom API (https://api.intercom.io).
8
8
 
9
9
  For generating Intercom javascript script tags for Rails, please see https://github.com/intercom/intercom-rails
10
10
 
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
+
11
14
  ## Installation
12
15
 
13
16
  gem install intercom
14
17
 
15
18
  Using bundler:
16
19
 
17
- gem 'intercom'
20
+ gem 'intercom', "~> 2.0"
18
21
 
19
22
  ## Basic Usage
20
23
 
@@ -22,104 +25,249 @@ Using bundler:
22
25
 
23
26
  ```ruby
24
27
  Intercom.app_id = "my_app_id"
25
- Intercom.api_key = "my-super-crazy-api-key"
28
+ Intercom.app_api_key = "my-super-crazy-api-key"
26
29
  ```
27
30
 
28
31
  ### Resources
29
32
 
30
- The API supports:
33
+ Resources this API supports:
31
34
 
32
- POST,PUT,GET https://api.intercom.io/v1/users
33
- POST,PUT,GET https://api.intercom.io/v1/users/messages
34
- POST https://api.intercom.io/v1/users/impressions
35
- POST https://api.intercom.io/v1/users/notes
35
+ https://api.intercom.io/users
36
+ https://api.intercom.io/companies
37
+ https://api.intercom.io/tags
38
+ https://api.intercom.io/notes
39
+ https://api.intercom.io/segments
40
+ https://api.intercom.io/events
41
+ https://api.intercom.io/conversations
42
+ https://api.intercom.io/messages
43
+ https://api.intercom.io/counts
36
44
 
37
45
  ### Examples
38
46
 
39
47
  #### Users
40
48
 
41
49
  ```ruby
42
- user = Intercom::User.find_by_email("bob@example.com")
43
- user.custom_data["average_monthly_spend"] = 1234.56
44
- user.save
45
- user = Intercom::User.find_by_user_id("1")
50
+ # Find user by email
51
+ user = Intercom::User.find(:email => "bob@example.com")
52
+ # Find user by user_id
53
+ user = Intercom::User.find(:user_id => "1")
54
+ # Find user by id
55
+ user = Intercom::User.find(:id => "1")
56
+ # Create a user
46
57
  user = Intercom::User.create(:email => "bob@example.com", :name => "Bob Smith")
47
- user = Intercom::User.new(params)
48
- user.save
49
- Intercom::User.all.count
50
- Intercom::User.all.each {|user| puts %Q(#{user.email} - #{user.custom_data["average_monthly_spend"]}) }
58
+ # Update custom_attributes for a user
59
+ user.custom_attributes["average_monthly_spend"] = 1234.56; user.save
60
+ # Perform incrementing
61
+ user.increment('karma'); user.save
62
+ # Iterate over all users
63
+ Intercom::User.all.each {|user| puts %Q(#{user.email} - #{user.custom_attributes["average_monthly_spend"]}) }
51
64
  Intercom::User.all.map {|user| user.email }
52
65
  ```
53
66
 
54
67
  #### Companies
55
68
  ```ruby
56
- user = Intercom::User.find_by_email("bob@example.com")
57
- user.company = {:id => 6, :name => "Intercom"}
58
- user.companies = [{:id => 6, :name => "Intercom"}, {:id => 9, :name => "Test Company"}]
69
+ # Add a user to one or more companies
70
+ user = Intercom::User.find(:email => "bob@example.com")
71
+ user.companies = [{:company_id => 6, :name => "Intercom"}, {:company_id => 9, :name => "Test Company"}]; user.save
72
+ # You can also pass custom attributes within a company as you do this
73
+ user.companies = [{:id => 6, :name => "Intercom", :custom_attributes => {:referral_source => "Google"} } ]; user.save
74
+ # Find a company by company_id
75
+ company = Intercom::Company.find(:company_id => "44")
76
+ # Find a company by name
77
+ company = Intercom::Company.find(:name => "Some company")
78
+ # Find a company by id
79
+ company = Intercom::Company.find(:id => "41e66f0313708347cb0000d0")
80
+ # Update a company
81
+ company.name = 'Updated company name'; company.save
82
+ # Iterate over all companies
83
+ Intercom::Company.all.each {|company| puts %Q(#{company.name} - #{company.custom_attributes["referral_source"]}) }
84
+ Intercom::Company.all.map {|company| company.name }
85
+ # Get a list of users in a company
86
+ company.users
59
87
  ```
60
88
 
61
- You can also pass custom data within a company:
89
+ #### Tags
90
+ ```ruby
91
+ # Tag users
92
+ tag = Intercom::Tag.tag_users('blue', ["42ea2f1b93891f6a99000427"])
93
+ # Untag users
94
+ Intercom::Tag.untag_users('blue', ["42ea2f1b93891f6a99000427"])
95
+ # Iterate over all tags
96
+ Intercom::Tag.all.each {|tag| "#{tag.id} - #{tag.name}" }
97
+ Intercom::Tag.all.map {|tag| tag.name }
98
+ # Iterate over all tags for user
99
+ Intercom::Tag.find_all_for_user(:id => '53357ddc3c776629e0000029')
100
+ Intercom::Tag.find_all_for_user(:email => 'declan+declan@intercom.io')
101
+ Intercom::Tag.find_all_for_user(:user_id => '3')
102
+ # Tag companies
103
+ tag = Intercom::Tag.tag_companies('red', ["42ea2f1b93891f6a99000427"])
104
+ # Untag companies
105
+ Intercom::Tag.untag_users('blue', ["42ea2f1b93891f6a99000427"])
106
+ # Iterate over all tags for company
107
+ Intercom::Tag.find_all_for_company(:id => '43357e2c3c77661e25000026')
108
+ Intercom::Tag.find_all_for_company(:company_id => '6')
109
+ ```
62
110
 
111
+ #### Segments
63
112
  ```ruby
64
- user.company = {:id => 6, :name => "Intercom", :referral_source => "Google"}
113
+ # Find a segment
114
+ segment = Intercom::Segment.find(:id => segment_id)
115
+ # Update a segment
116
+ segment.name = 'Updated name'; segment.save
117
+ # Iterate over all segments
118
+ Intercom::Segment.all.each {|segment| puts "id: #{segment.id} name: #{segment.name}"}
65
119
  ```
66
120
 
67
- #### Message Threads
121
+ #### Notes
68
122
  ```ruby
69
- Intercom::MessageThread.create(:email => "bob@example.com", :body => "Example message from bob@example.com to your application on Intercom.")
70
- Intercom::MessageThread.find(:email => "bob@example.com", :thread_id => 123)
71
- Intercom::MessageThread.find_all(:email => "bob@example.com")
72
- Intercom::MessageThread.mark_as_read(:email => "bob@example.com", :thread_id => 123)
123
+ # Find a note by id
124
+ note = Intercom::Note.find(:id => note)
125
+ # Create a note for a user
126
+ note = Intercom::Note.create(:body => "<p>Text for the note</p>", :email => 'joe@example.com')
127
+ # Iterate over all notes for a user via their email address
128
+ Intercom::Note.find_all(:email => 'joe@example.com').each {|note| puts note.body}
129
+ # Iterate over all notes for a user via their user_id
130
+ Intercom::Note.find_all(:user_id => '123').each {|note| puts note.body}
73
131
  ```
74
132
 
75
- #### Impressions
133
+ #### Conversations
76
134
  ```ruby
77
- Intercom::Impression.create(:email => "bob@example.com", :location => "/path/in/my/app", :user_ip => "1.2.3.4", :user_agent => "my-savage-iphone-app-0.1"
135
+ # FINDING CONVERSATIONS FOR AN ADMIN
136
+ # Iterate over all conversations (open and closed) assigned to an admin
137
+ Intercom::Conversation.find_all(:type => 'admin', :id => '7').each do {|convo| ... }
138
+ # Iterate over all open conversations assigned to an admin
139
+ Intercom::Conversation.find_all(:type => 'admin', :id => 7, :open => true).each do {|convo| ... }
140
+ # Iterate over closed conversations assigned to an admin
141
+ Intercom::Conversation.find_all(:type => 'admin', :id => 7, :open => false).each do {|convo| ... }
142
+ # Iterate over closed conversations for assigned an admin, before a certain moment in time
143
+ Intercom::Conversation.find_all(:type => 'admin', :id => 7, :open => false, :before => 1374844930).each do {|convo| ... }
144
+
145
+ # FINDING CONVERSATIONS FOR A USER
146
+ # Iterate over all conversations (read + unread, correct) with a user based on the users email
147
+ Intercom::Conversation.find_all(:email => 'joe@example.com', :type => 'user').each do {|convo| ... }
148
+ # Iterate over through all conversations (read + unread) with a user based on the users email
149
+ Intercom::Conversation.find_all(:email => 'joe@example.com', :type => 'user', :unread => false).each do {|convo| ... }
150
+ # Iterate over all unread conversations with a user based on the users email
151
+ Intercom::Conversation.find_all(:email => 'joe@example.com', :type => 'user', :unread => true).each do {|convo| ... }
152
+
153
+ # FINDING A SINGLE CONVERSATION
154
+ conversation = Intercom::Conversation.find(:id => '1')
155
+
156
+ # INTERACTING WITH THE PARTS OF A CONVERSATION
157
+ # Getting the subject of a part (only applies to email-based conversations)
158
+ conversation.rendered_message.subject
159
+ # Get the part_type of the first part
160
+ conversation.conversation_parts[0].part_type
161
+ # Get the body of the second part
162
+ conversation.conversation_parts[1].body
163
+
164
+ # REPLYING TO CONVERSATIONS
165
+ # User (identified by email) replies with a comment
166
+ conversation.reply(:type => 'user', :email => 'joe@example.com', :message_type => 'comment', :body => 'foo')
167
+ # Admin (identified by email) replies with a comment
168
+ conversation.reply(:type => 'admin', :email => 'bob@example.com', :message_type => 'comment', :body => 'bar')
78
169
  ```
79
170
 
80
- #### Notes
171
+ #### Counts
81
172
  ```ruby
82
- Intercom::Note.create(:email => "bob@example.com", :body => "This is the text of the note")
173
+ # Get Conversation per Admin
174
+ conversation_counts_for_each_admin = Intercom::Count.conversation_counts_for_each_admin
175
+ conversation_counts_for_each_admin.each{|count| puts "Admin: #{count.name} (id: #{count.id}) Open: #{count.open} Closed: #{count.closed}" }
176
+ # Get User Tag Count Object
177
+ Intercom::Count.user_counts_for_each_tag
178
+ # Get User Segment Count Object
179
+ Intercom::Count.user_counts_for_each_segment
180
+ # Get Company Segment Count Object
181
+ Intercom::Count.company_counts_for_each_segment
182
+ # Get Company Tag Count Object
183
+ Intercom::Count.company_counts_for_each_tag
184
+ # Get Company User Count Object
185
+ Intercom::Count.company_counts_for_each_user
186
+ # Get total count of companies, users, segments or tags across app
187
+ Intercom::Company.count
188
+ Intercom::User.count
189
+ Intercom::Segment.count
190
+ Intercom::Tag.count
83
191
  ```
84
192
 
85
- #### Events
86
- The simplest way to create an event is directly on the user
193
+ #### Full loading of and embedded entity
87
194
  ```ruby
88
- user = Intercom::User.find_by_email("bob@example.com")
89
- user.track_event("invited-friend")
195
+ # Given a converation with a partial user, load the full user. This can be done for any entity
196
+ conversation.user.load
90
197
  ```
91
198
 
92
- For more control create events through Intercom::Event
199
+ #### Sending messages
93
200
  ```ruby
94
- Intercom::Event.create(:event_name => "invited-friend", :user => user)
95
201
 
96
- # With an explicit event creation date
97
- Intercom::Event.create(:event_name => "invited-friend", :user => user, :created_at => 1391691571)
202
+ # InApp message from admin to user
203
+ Intercom::Message.create({
204
+ :message_type => 'inapp',
205
+ :body => "What's up :)",
206
+ :from => {
207
+ :type => 'admin',
208
+ :id => "1234"
209
+ },
210
+ :to => {
211
+ :type => :user,
212
+ :id => "5678"
213
+ }
214
+ })
98
215
 
99
- # With metadata
100
- Intercom::Event.create(:event_name => "invited-friend", :user => user,
101
- metadata => {
102
- :invitee_email => 'pi@example.org',
103
- :invite_code => 'ADDAFRIEND'
216
+ # Email message from admin to user
217
+ Intercom::Message.create({
218
+ :message_type => 'email',
219
+ :subject => 'Hey there',
220
+ :body => "What's up :)",
221
+ :template => "plain", # or "personal",
222
+ :from => {
223
+ :type => "admin",
224
+ :id => "1234"
225
+ },
226
+ :to => {
227
+ :type => "user",
228
+ :id => "536e564f316c83104c000020"
229
+ }
230
+ })
231
+
232
+ # Message from a user
233
+ Intercom::Message.create({
234
+ :from => {
235
+ :type => "user",
236
+ :id => "536e564f316c83104c000020"
237
+ },
238
+ :body => "halp"
239
+ })
240
+ ```
241
+
242
+ #### Events
243
+ ```ruby
244
+ Intercom::Event.create(
245
+ :event_name => "invited-friend", :created_at => Time.now.to_i,
246
+ :email => user.email,
247
+ :metadata => {
248
+ "invitee_email" => "pi@example.org",
249
+ :invite_code => "ADDAFRIEND",
250
+ "found_date" => 12909364407
104
251
  }
105
252
  )
106
253
  ```
107
254
 
108
- Metadata Objects support a few simple types that Intercom can present on your behalf
255
+ Metadata Objects support a few simple types that Intercom can present on your behalf
109
256
 
110
257
  ```ruby
111
- Intercom::Event.create(:event_name => "placed-order", :user => current_user,
112
- metadata => {
258
+ Intercom::Event.create(:event_name => "placed-order", :email => current_user.email,
259
+ :created_at => 1403001013
260
+ :metadata => {
113
261
  :order_date => Time.now.to_i,
114
- :stripe_invoice => 'inv_3434343434',
115
- :order_number => {
116
- :value => '3434-3434',
117
- :url => 'https://example.org/orders/3434-3434'
262
+ :stripe_invoice => 'inv_3434343434',
263
+ :order_number => {
264
+ :value => '3434-3434',
265
+ :url => 'https://example.org/orders/3434-3434'
118
266
  },
119
- price: {
267
+ price: {
120
268
  :currency => 'usd',
121
- :amount => 2999
122
- }
269
+ :amount => 2999
270
+ }
123
271
  }
124
272
  )
125
273
  ```
@@ -131,9 +279,17 @@ The metadata key values in the example are treated as follows-
131
279
  - price: An Amount in US Dollars (value contains 'amount' and 'currency' keys)
132
280
 
133
281
  ### Errors
282
+ 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 it's `http_code` method.
283
+
284
+ The list of different error subclasses are listed below. As they all inherit off Intercom::Error you can choose to rescue Intercom::Error or
285
+ else rescue the more specific error subclass.
286
+
134
287
  ```ruby
135
288
  Intercom::AuthenticationError
136
289
  Intercom::ServerError
137
290
  Intercom::ServiceUnavailableError
138
291
  Intercom::ResourceNotFound
292
+ Intercom::BadRequestError
293
+ Intercom::RateLimitExceeded
294
+ Intercom::AttributeNotSetError # Raised when you try to call a getter that does not exist on an object
139
295
  ```
data/changes.txt CHANGED
@@ -1,3 +1,6 @@
1
+ 2.0.0
2
+ - Bumped to 2.0.0.
3
+
1
4
  1.0.0
2
5
  - Bumped to a 1.0.0 version. No functional changes.
3
6
 
data/intercom.gemspec CHANGED
@@ -6,8 +6,8 @@ require "intercom/version"
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "intercom"
8
8
  spec.version = Intercom::VERSION
9
- spec.authors = ["Ben McRedmond", "Ciaran Lee", "Darragh Curran", "Jeff Gardner", "Kyle Daigle", "Declan McGrath", "Jamie Osler"]
10
- spec.email = ["ben@intercom.io", "ciaran@intercom.io", "darragh@intercom.io", "jeff@intercom.io", "kyle@digitalworkbox.com", "declan@intercom.io", "jamie@intercom.io"]
9
+ spec.authors = ["Ben McRedmond", "Ciaran Lee", "Darragh Curran", "Jeff Gardner", "Kyle Daigle", "Declan McGrath", "Jamie Osler", "Bob Long"]
10
+ spec.email = ["ben@intercom.io", "ciaran@intercom.io", "darragh@intercom.io", "jeff@intercom.io", "kyle@digitalworkbox.com", "declan@intercom.io", "jamie@intercom.io", "bob@intercom.io"]
11
11
  spec.homepage = "https://www.intercom.io"
12
12
  spec.summary = %q{Ruby bindings for the Intercom API}
13
13
  spec.description = %Q{Intercom (https://www.intercom.io) is a customer relationship management and messaging tool for web app owners. This library wraps the api provided by Intercom. See http://docs.intercom.io/api for more details. }
data/lib/ext/hash.rb ADDED
@@ -0,0 +1,18 @@
1
+ class Hash
2
+ # Return a hash that includes everything but the given keys.
3
+ def except(*keys)
4
+ dup.except!(*keys)
5
+ end
6
+
7
+ # Replaces the hash without the given keys.
8
+ def except!(*keys)
9
+ keys.each { |key| delete(key) }
10
+ self
11
+ end
12
+
13
+ # Return a hash that includes only the given keys.
14
+ def slice(*keys)
15
+ keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true)
16
+ keys.each_with_object(self.class.new) { |k, hash| hash[k] = self[k] if has_key?(k) }
17
+ end
18
+ end
data/lib/intercom.rb CHANGED
@@ -1,12 +1,16 @@
1
1
  require "intercom/version"
2
- require "intercom/user_resource"
3
2
  require "intercom/user"
4
- require "intercom/message_thread"
5
- require "intercom/impression"
3
+ require "intercom/company"
6
4
  require "intercom/note"
7
5
  require "intercom/tag"
8
- require "intercom/request"
6
+ require "intercom/segment"
9
7
  require "intercom/event"
8
+ require "intercom/conversation"
9
+ require "intercom/message"
10
+ require "intercom/count"
11
+ require "intercom/request"
12
+ require "intercom/utils"
13
+ require "intercom/errors"
10
14
  require "json"
11
15
 
12
16
  ##
@@ -17,23 +21,19 @@ require "json"
17
21
  # == Basic Usage
18
22
  # === Configure Intercom with your access credentials
19
23
  # Intercom.app_id = "my_app_id"
20
- # Intercom.api_key = "my_api_key"
24
+ # Intercom.app_api_key = "my_api_key"
21
25
  # === Make requests to the API
22
26
  # Intercom::User.find(:email => "bob@example.com")
23
27
  #
24
28
  module Intercom
25
29
  @hostname = "api.intercom.io"
26
30
  @protocol = "https"
31
+
27
32
  @endpoints = nil
28
33
  @current_endpoint = nil
29
34
  @app_id = nil
30
- @api_key = nil
35
+ @app_api_key = nil
31
36
 
32
- ##
33
- # Set the id of the application you want to interact with.
34
- # When logged into your intercom console, the app_id is in the url after /apps (eg https://www.intercom.io/apps/<app-id>)
35
- # @param [String] app_id
36
- # @return [String]
37
37
  def self.app_id=(app_id)
38
38
  @app_id = app_id
39
39
  end
@@ -42,27 +42,42 @@ module Intercom
42
42
  @app_id
43
43
  end
44
44
 
45
- ##
46
- # Set the api key to gain access to your application data.
47
- # When logged into your intercom console, you can view/create api keys in the settings menu
48
- # @param [String] api_key
49
- # @return [String]
50
- def self.api_key=(api_key)
51
- @api_key = api_key
45
+ def self.app_api_key=(app_api_key)
46
+ @app_api_key = app_api_key
47
+ end
48
+ def self.app_api_key
49
+ @app_api_key
52
50
  end
53
51
 
54
- def self.api_key
55
- @api_key
52
+ # This method is obsolete and used to warn of backwards incompatible changes on upgrading
53
+ def self.api_key=(val)
54
+ raise ArgumentError, "#{compatibility_warning_text} #{compatibility_workaround_text} #{related_docs_text}"
56
55
  end
57
56
 
58
57
  private
59
58
 
60
59
  def self.target_base_url
61
- raise ArgumentError, "You must set both Intercom.app_id and Intercom.api_key to use this client. See https://github.com/intercom/intercom-ruby for usage examples." if [@app_id, @api_key].any?(&:nil?)
62
- basic_auth_part = "#{@app_id}:#{@api_key}@"
60
+ raise ArgumentError, "#{configuration_required_text} #{related_docs_text}" if [@app_id, @app_api_key].any?(&:nil?)
61
+ basic_auth_part = "#{@app_id}:#{@app_api_key}@"
63
62
  current_endpoint.gsub(/(https?:\/\/)(.*)/, "\\1#{basic_auth_part}\\2")
64
63
  end
65
64
 
65
+ def self.related_docs_text
66
+ "See https://github.com/intercom/intercom-ruby for usage examples."
67
+ end
68
+
69
+ def self.compatibility_warning_text
70
+ "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. "
71
+ end
72
+
73
+ def self.compatibility_workaround_text
74
+ "To get rid of this error please set Intercom.app_api_key and don't set Intercom.api_key."
75
+ end
76
+
77
+ def self.configuration_required_text
78
+ "You must set both Intercom.app_id and Intercom.app_api_key to use this client."
79
+ end
80
+
66
81
  def self.send_request_to_path(request)
67
82
  request.execute(target_base_url)
68
83
  rescue Intercom::ServiceUnavailableError => e
@@ -144,24 +159,4 @@ module Intercom
144
159
  @endpoints || ["#{@protocol}://#{hostname}"]
145
160
  end
146
161
 
147
- # Raised when the credentials you provide don't match a valid account on Intercom.
148
- # Check that you have set <b>Intercom.app_id=</b> and <b>Intercom.api_key=</b> correctly.
149
- class AuthenticationError < StandardError;
150
- end
151
-
152
- # Raised when something does wrong on within the Intercom API service.
153
- class ServerError < StandardError;
154
- end
155
-
156
- # Raised when we have bad gateway errors.
157
- class BadGatewayError < StandardError;
158
- end
159
-
160
- # Raised when we reach socket connect timeout
161
- class ServiceUnavailableError < StandardError;
162
- end
163
-
164
- # Raised when requesting resources on behalf of a user that doesn't exist in your application on Intercom.
165
- class ResourceNotFound < StandardError;
166
- end
167
- end
162
+ end