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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,22 @@ The metadata key values in the example are treated as follows-

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

### Contacts

`Contacts` represent logged out users of your application.

```ruby
# Create a contact
contact = Intercom::Contact.create(email: "some_contact@example.com")

# Update a contact
contact.custom_attributes['foo'] = 'bar'
contact.save

# Find contacts by email
contacts = Intercom::Contact.find_all(email: "some_contact@example.com")
```

### Subscriptions

Subscribe to events in Intercom to receive webhooks.
Expand Down
1 change: 1 addition & 0 deletions lib/intercom.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "intercom/version"
require "intercom/contact"
require "intercom/user"
require "intercom/company"
require "intercom/note"
Expand Down
19 changes: 19 additions & 0 deletions lib/intercom/api_operations/convert.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'intercom/traits/api_resource'

module Intercom
module ApiOperations
module Convert
def convert(user)
from_response(
Intercom.post(
"/contacts/convert",
{
contact: { user_id: user_id },
user: user.identity_hash
}
)
)
end
end
end
end
17 changes: 12 additions & 5 deletions lib/intercom/api_operations/save.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ module ApiOperations
module Save

module ClassMethods
def create(params)
PARAMS_NOT_PROVIDED = Object.new
def create(params = PARAMS_NOT_PROVIDED)
if self.ancestors.include?(Intercom::Contact) && params == PARAMS_NOT_PROVIDED
params = Hash.new
elsif params == PARAMS_NOT_PROVIDED
raise ArgumentError, '.create requires 1 parameter'
end

instance = self.new(params)
instance.mark_fields_as_changed!(params.keys)
instance.save
Expand All @@ -26,6 +33,10 @@ def save
from_response(response) if response # may be nil we received back a 202
end

def identity_hash
respond_to?(:identity_vars) ? SliceableHash.new(to_hash).slice(*(identity_vars.map(&:to_s))) : {}
end

private

def id_present?
Expand All @@ -35,10 +46,6 @@ def id_present?
def posted_updates?
respond_to?(:update_verb) && update_verb == 'post'
end

def identity_hash
respond_to?(:identity_vars) ? SliceableHash.new(to_hash).slice(*(identity_vars.map(&:to_s))) : {}
end
end
end
end
22 changes: 22 additions & 0 deletions lib/intercom/contact.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'intercom/api_operations/count'
require 'intercom/api_operations/load'
require 'intercom/api_operations/find'
require 'intercom/api_operations/find_all'
require 'intercom/api_operations/save'
require 'intercom/api_operations/convert'
require 'intercom/traits/api_resource'

module Intercom
class Contact
include ApiOperations::Load
include ApiOperations::Find
include ApiOperations::FindAll
include ApiOperations::Save
include ApiOperations::Convert
include Traits::ApiResource

def identity_vars ; [:email, :user_id] ; end
def flat_store_attributes ; [:custom_attributes] ; end
def update_verb; 'put' ; end
end
end
25 changes: 25 additions & 0 deletions spec/unit/intercom/contact_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require "spec_helper"

describe "Intercom::Contact" do
it 'should not throw ArgumentErrors when there are no parameters' do
Intercom.expects(:post)
Intercom::Contact.create
end

describe 'converting' do
let(:contact) { Intercom::Contact.from_api(user_id: 'contact_id') }
let(:user) { Intercom::User.from_api(id: 'user_id') }

it do
Intercom.expects(:post).with(
"/contacts/convert",
{
contact: { user_id: contact.user_id },
user: user.identity_hash
}
).returns(test_user)

contact.convert(user)
end
end
end