Skip to content

Commit e1a322a

Browse files
committed
Merge pull request #160 from intercom/jo/counts-api
counts api
2 parents 810bf1f + 86a42dc commit e1a322a

File tree

7 files changed

+107
-1
lines changed

7 files changed

+107
-1
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Using bundler:
3232
intercom = Intercom::Client.new(app_id: 'my_app_id', api_key: 'my_api_key')
3333
```
3434

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).
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).
3636

3737
### Resources
3838

@@ -41,6 +41,7 @@ Resources this API supports:
4141
https://api.intercom.io/users
4242
https://api.intercom.io/contacts
4343
https://api.intercom.io/companies
44+
https://api.intercom.io/counts
4445
https://api.intercom.io/tags
4546
https://api.intercom.io/notes
4647
https://api.intercom.io/segments
@@ -313,6 +314,16 @@ contacts = intercom.contacts.find_all(email: "[email protected]")
313314
intercom.contacts.convert(contact, user)
314315
```
315316

317+
### Counts
318+
319+
```ruby
320+
# App-wide counts
321+
intercom.counts.for_app
322+
323+
# Users in segment counts
324+
intercom.counts.for_type(type: 'user', count: 'segment')
325+
```
326+
316327
### Subscriptions
317328

318329
Subscribe to events in Intercom to receive webhooks.

lib/intercom.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'intercom/service/company'
44
require 'intercom/service/contact'
55
require 'intercom/service/conversation'
6+
require 'intercom/service/count'
67
require 'intercom/service/event'
78
require 'intercom/service/message'
89
require 'intercom/service/note'
@@ -13,6 +14,7 @@
1314
require 'intercom/options'
1415
require 'intercom/client'
1516
require "intercom/contact"
17+
require "intercom/count"
1618
require "intercom/user"
1719
require "intercom/company"
1820
require "intercom/note"

lib/intercom/client.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ def conversations
3636
Intercom::Service::Conversation.new(self)
3737
end
3838

39+
def counts
40+
Intercom::Service::Counts.new(self)
41+
end
42+
3943
def events
4044
Intercom::Service::Event.new(self)
4145
end

lib/intercom/count.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require 'intercom/traits/api_resource'
2+
3+
module Intercom
4+
class Count
5+
include Traits::ApiResource
6+
end
7+
end

lib/intercom/service/count.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require 'intercom/service/base_service'
2+
require 'intercom/api_operations/find'
3+
4+
module Intercom
5+
module Service
6+
class Counts < BaseService
7+
include ApiOperations::Find
8+
9+
def collection_class
10+
Intercom::Count
11+
end
12+
13+
def for_app
14+
find({})
15+
end
16+
17+
def for_type(type:, count: nil)
18+
find(type: type, count: count)
19+
end
20+
end
21+
end
22+
end

spec/spec_helper.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,49 @@ def test_subscription
405405
"notes"=>[]}}
406406
end
407407

408+
def test_app_count
409+
{
410+
"type" => "count.hash",
411+
"company" => {
412+
"count" => 8
413+
},
414+
"segment" => {
415+
"count" => 47
416+
},
417+
"tag" => {
418+
"count" => 341
419+
},
420+
"user" => {
421+
"count" => 12239
422+
}
423+
}
424+
end
425+
426+
def test_segment_count
427+
{
428+
"type" => "count",
429+
"user" => {
430+
"segment" => [
431+
{
432+
"Active" => 1
433+
},
434+
{
435+
"New" => 0
436+
},
437+
{
438+
"VIP" => 0
439+
},
440+
{
441+
"Slipping Away" => 0
442+
},
443+
{
444+
"segment 1" => 1
445+
}
446+
]
447+
}
448+
}
449+
end
450+
408451
def error_on_modify_frozen
409452
RUBY_VERSION =~ /1.8/ ? TypeError : RuntimeError
410453
end

spec/unit/intercom/count_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require "spec_helper"
2+
3+
describe "Intercom::Count" do
4+
let (:client) { Intercom::Client.new(app_id: 'app_id', api_key: 'api_key') }
5+
6+
it 'should get app wide counts' do
7+
client.expects(:get).with("/counts", {}).returns(test_app_count)
8+
counts = client.counts.for_app
9+
counts.tag['count'].must_equal(341)
10+
end
11+
12+
it 'should get type counts' do
13+
client.expects(:get).with("/counts", {type: 'user', count: 'segment'}).returns(test_segment_count)
14+
counts = client.counts.for_type(type: 'user', count: 'segment')
15+
counts.user['segment'][4]["segment 1"].must_equal(1)
16+
end
17+
end

0 commit comments

Comments
 (0)