Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Using bundler:
intercom = Intercom::Client.new(app_id: 'my_app_id', api_key: 'my_api_key')
```

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

### Resources

Expand All @@ -41,6 +41,7 @@ Resources this API supports:
https://api.intercom.io/users
https://api.intercom.io/contacts
https://api.intercom.io/companies
https://api.intercom.io/counts
https://api.intercom.io/tags
https://api.intercom.io/notes
https://api.intercom.io/segments
Expand Down Expand Up @@ -313,6 +314,16 @@ contacts = intercom.contacts.find_all(email: "some_contact@example.com")
intercom.contacts.convert(contact, user)
```

### Counts

```ruby
# App-wide counts
intercom.counts.for_app

# Users in segment counts
intercom.counts.for_type(type: 'user', count: 'segment')
```

### Subscriptions

Subscribe to events in Intercom to receive webhooks.
Expand Down
2 changes: 2 additions & 0 deletions lib/intercom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'intercom/service/company'
require 'intercom/service/contact'
require 'intercom/service/conversation'
require 'intercom/service/count'
require 'intercom/service/event'
require 'intercom/service/message'
require 'intercom/service/note'
Expand All @@ -13,6 +14,7 @@
require 'intercom/options'
require 'intercom/client'
require "intercom/contact"
require "intercom/count"
require "intercom/user"
require "intercom/company"
require "intercom/note"
Expand Down
4 changes: 4 additions & 0 deletions lib/intercom/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def conversations
Intercom::Service::Conversation.new(self)
end

def counts
Intercom::Service::Counts.new(self)
end

def events
Intercom::Service::Event.new(self)
end
Expand Down
7 changes: 7 additions & 0 deletions lib/intercom/count.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'intercom/traits/api_resource'

module Intercom
class Count
include Traits::ApiResource
end
end
22 changes: 22 additions & 0 deletions lib/intercom/service/count.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'intercom/service/base_service'
require 'intercom/api_operations/find'

module Intercom
module Service
class Counts < BaseService
include ApiOperations::Find

def collection_class
Intercom::Count
end

def for_app
find({})
end

def for_type(type:, count: nil)
find(type: type, count: count)
end
end
end
end
43 changes: 43 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,49 @@ def test_subscription
"notes"=>[]}}
end

def test_app_count
{
"type" => "count.hash",
"company" => {
"count" => 8
},
"segment" => {
"count" => 47
},
"tag" => {
"count" => 341
},
"user" => {
"count" => 12239
}
}
end

def test_segment_count
{
"type" => "count",
"user" => {
"segment" => [
{
"Active" => 1
},
{
"New" => 0
},
{
"VIP" => 0
},
{
"Slipping Away" => 0
},
{
"segment 1" => 1
}
]
}
}
end

def error_on_modify_frozen
RUBY_VERSION =~ /1.8/ ? TypeError : RuntimeError
end
Expand Down
17 changes: 17 additions & 0 deletions spec/unit/intercom/count_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require "spec_helper"

describe "Intercom::Count" do
let (:client) { Intercom::Client.new(app_id: 'app_id', api_key: 'api_key') }

it 'should get app wide counts' do
client.expects(:get).with("/counts", {}).returns(test_app_count)
counts = client.counts.for_app
counts.tag['count'].must_equal(341)
end

it 'should get type counts' do
client.expects(:get).with("/counts", {type: 'user', count: 'segment'}).returns(test_segment_count)
counts = client.counts.for_type(type: 'user', count: 'segment')
counts.user['segment'][4]["segment 1"].must_equal(1)
end
end