Skip to content

Commit eebff40

Browse files
committed
Merge pull request #152 from intercom/BL/contacts
Acquire support
2 parents a81d155 + fb8522b commit eebff40

File tree

6 files changed

+95
-5
lines changed

6 files changed

+95
-5
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,22 @@ The metadata key values in the example are treated as follows-
300300

301301
*NB:* This version of the gem reserves the field name `type` in Event data.
302302

303+
### Contacts
304+
305+
`Contacts` represent logged out users of your application.
306+
307+
```ruby
308+
# Create a contact
309+
contact = Intercom::Contact.create(email: "[email protected]")
310+
311+
# Update a contact
312+
contact.custom_attributes['foo'] = 'bar'
313+
contact.save
314+
315+
# Find contacts by email
316+
contacts = Intercom::Contact.find_all(email: "[email protected]")
317+
```
318+
303319
### Subscriptions
304320

305321
Subscribe to events in Intercom to receive webhooks.

lib/intercom.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "intercom/version"
2+
require "intercom/contact"
23
require "intercom/user"
34
require "intercom/company"
45
require "intercom/note"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'intercom/traits/api_resource'
2+
3+
module Intercom
4+
module ApiOperations
5+
module Convert
6+
def convert(user)
7+
from_response(
8+
Intercom.post(
9+
"/contacts/convert",
10+
{
11+
contact: { user_id: user_id },
12+
user: user.identity_hash
13+
}
14+
)
15+
)
16+
end
17+
end
18+
end
19+
end

lib/intercom/api_operations/save.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ module ApiOperations
55
module Save
66

77
module ClassMethods
8-
def create(params)
8+
PARAMS_NOT_PROVIDED = Object.new
9+
def create(params = PARAMS_NOT_PROVIDED)
10+
if self.ancestors.include?(Intercom::Contact) && params == PARAMS_NOT_PROVIDED
11+
params = Hash.new
12+
elsif params == PARAMS_NOT_PROVIDED
13+
raise ArgumentError, '.create requires 1 parameter'
14+
end
15+
916
instance = self.new(params)
1017
instance.mark_fields_as_changed!(params.keys)
1118
instance.save
@@ -26,6 +33,10 @@ def save
2633
from_response(response) if response # may be nil we received back a 202
2734
end
2835

36+
def identity_hash
37+
respond_to?(:identity_vars) ? SliceableHash.new(to_hash).slice(*(identity_vars.map(&:to_s))) : {}
38+
end
39+
2940
private
3041

3142
def id_present?
@@ -35,10 +46,6 @@ def id_present?
3546
def posted_updates?
3647
respond_to?(:update_verb) && update_verb == 'post'
3748
end
38-
39-
def identity_hash
40-
respond_to?(:identity_vars) ? SliceableHash.new(to_hash).slice(*(identity_vars.map(&:to_s))) : {}
41-
end
4249
end
4350
end
4451
end

lib/intercom/contact.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require 'intercom/api_operations/count'
2+
require 'intercom/api_operations/load'
3+
require 'intercom/api_operations/find'
4+
require 'intercom/api_operations/find_all'
5+
require 'intercom/api_operations/save'
6+
require 'intercom/api_operations/convert'
7+
require 'intercom/traits/api_resource'
8+
9+
module Intercom
10+
class Contact
11+
include ApiOperations::Load
12+
include ApiOperations::Find
13+
include ApiOperations::FindAll
14+
include ApiOperations::Save
15+
include ApiOperations::Convert
16+
include Traits::ApiResource
17+
18+
def identity_vars ; [:email, :user_id] ; end
19+
def flat_store_attributes ; [:custom_attributes] ; end
20+
def update_verb; 'put' ; end
21+
end
22+
end

spec/unit/intercom/contact_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require "spec_helper"
2+
3+
describe "Intercom::Contact" do
4+
it 'should not throw ArgumentErrors when there are no parameters' do
5+
Intercom.expects(:post)
6+
Intercom::Contact.create
7+
end
8+
9+
describe 'converting' do
10+
let(:contact) { Intercom::Contact.from_api(user_id: 'contact_id') }
11+
let(:user) { Intercom::User.from_api(id: 'user_id') }
12+
13+
it do
14+
Intercom.expects(:post).with(
15+
"/contacts/convert",
16+
{
17+
contact: { user_id: contact.user_id },
18+
user: user.identity_hash
19+
}
20+
).returns(test_user)
21+
22+
contact.convert(user)
23+
end
24+
end
25+
end

0 commit comments

Comments
 (0)