Skip to content

Commit 319f3e3

Browse files
authored
Merge pull request #593 from intercom/seanhealy/opedSubscription
Add opted out and in subscription types
2 parents b6ab09c + 36aa8c4 commit 319f3e3

File tree

9 files changed

+76
-3
lines changed

9 files changed

+76
-3
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,16 @@ contact.remove_company(id: company.id)
165165

166166
# List companies for a contact
167167
contact.companies.each {|c| p c.name}
168+
169+
# attach a subscription_types on a contact
170+
contact.create_subscription_types(id: '1')
171+
172+
# List subscription_types for a contact
173+
contact.subscription_types.each {|n| p n.body}
174+
175+
# Remove subscription_types
176+
contact.remove_subscription_type({ "id": subscription.id })
177+
168178
```
169179

170180
#### Visitors
@@ -596,6 +606,19 @@ intercom.subscriptions.delete(subscription)
596606
intercom.subscriptions.all
597607
```
598608

609+
610+
#### Subscription Types
611+
612+
Subscribe to events in Intercom to receive webhooks.
613+
614+
```ruby
615+
616+
# fetch a subscription
617+
intercom.subscription_types.find(id: "1")
618+
619+
intercom.subscription_types.all
620+
```
621+
599622
#### Articles
600623

601624
```ruby

lib/intercom.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
require 'intercom/service/message'
1313
require 'intercom/service/note'
1414
require 'intercom/service/job'
15+
require 'intercom/service/subscription_type'
1516
require 'intercom/service/subscription'
1617
require 'intercom/service/segment'
1718
require 'intercom/service/section'
@@ -44,6 +45,7 @@
4445
require 'intercom/article'
4546
require 'intercom/request'
4647
require 'intercom/subscription'
48+
require 'intercom/subscription_type'
4749
require 'intercom/team'
4850
require 'intercom/errors'
4951
require 'intercom/visitor'

lib/intercom/api_operations/nested_resource.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ def nested_resource_methods(resource,
1313
raise ArgumentError, 'operations array required' if operations.nil?
1414

1515
resource_url_method = :"#{resource_plural}_url"
16-
1716
resource_name = Utils.resource_class_to_collection_name(self)
18-
1917
define_method(resource_url_method.to_sym) do |id, nested_id = nil|
2018
url = "/#{resource_name}/#{id}/#{path}"
2119
url += "/#{nested_id}" unless nested_id.nil?

lib/intercom/client.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ def subscriptions
8484
Intercom::Service::Subscription.new(self)
8585
end
8686

87+
def subscription_types
88+
Intercom::Service::SubscriptionType.new(self)
89+
end
90+
8791
def segments
8892
Intercom::Service::Segment.new(self)
8993
end

lib/intercom/contact.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Contact
1212

1313
nested_resource_methods :tag, operations: %i[add delete list]
1414
nested_resource_methods :note, operations: %i[create list]
15+
nested_resource_methods :subscription_type, operations: %i[create delete list]
1516
nested_resource_methods :company, operations: %i[add delete list]
1617
nested_resource_methods :segment, operations: %i[list]
1718

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require 'intercom/api_operations/list'
2+
require 'intercom/api_operations/find_all'
3+
require 'intercom/api_operations/find'
4+
5+
module Intercom
6+
module Service
7+
class SubscriptionType < BaseService
8+
include ApiOperations::List
9+
include ApiOperations::Find
10+
include ApiOperations::FindAll
11+
include ApiOperations::Delete
12+
13+
def collection_class
14+
Intercom::SubscriptionType
15+
end
16+
end
17+
end
18+
end

lib/intercom/subscription_type.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
require 'intercom/traits/api_resource'
3+
4+
module Intercom
5+
class SubscriptionType
6+
include Traits::ApiResource
7+
8+
def self.collection_proxy_class
9+
Intercom::BaseCollectionProxy
10+
end
11+
end
12+
end

lib/intercom/utils.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ def constantize(camel_cased_word)
2525
constant
2626
end
2727

28+
def camelize(snake_cased_word)
29+
snake_cased_word.split(/_/).map(&:capitalize).join
30+
end
31+
2832
def resource_class_to_singular_name(resource_class)
2933
resource_name = resource_class.to_s.split('::')[-1]
3034
resource_name = maybe_underscore_name(resource_name)
@@ -40,7 +44,7 @@ def resource_class_to_collection_name(resource_class)
4044
end
4145

4246
def constantize_resource_name(resource_name)
43-
class_name = Utils.singularize(resource_name.capitalize)
47+
class_name = camelize Utils.singularize(resource_name.capitalize)
4448
define_lightweight_class(class_name) unless Intercom.const_defined?(class_name, false)
4549
namespaced_class_name = "Intercom::#{class_name}"
4650
constantize namespaced_class_name

spec/unit/intercom/contact_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@
282282
let(:contact) { Intercom::Contact.new(id: '1', client: client) }
283283
let(:contact_no_tags) { Intercom::Contact.new(id: '2', client: client, tags: []) }
284284
let(:company) { Intercom::Company.new(id: '1') }
285+
let(:subscription) { Intercom::Subscription.new(id: '1', client: client) }
285286
let(:tag) { Intercom::Tag.new(id: '1') }
286287
let(:note) { Intercom::Note.new(body: "<p>Text for the note</p>") }
287288

@@ -344,6 +345,11 @@
344345
contact.add_tag({ "id": tag.id })
345346
end
346347

348+
it 'removes a subscription to a contact' do
349+
client.expects(:delete).with("/contacts/1/subscription_types/#{subscription.id}", "id": subscription.id).returns(subscription.to_hash)
350+
contact.remove_subscription_type({ "id": subscription.id })
351+
end
352+
347353
it 'removes a tag from a contact' do
348354
client.expects(:delete).with("/contacts/1/tags/#{tag.id}", "id": tag.id ).returns(tag.to_hash)
349355
contact.remove_tag({ "id": tag.id })
@@ -393,6 +399,11 @@
393399
contact.create_note({body: note.body})
394400
end
395401

402+
it 'adds a subscription to a contact' do
403+
client.expects(:post).with('/contacts/1/subscription_types', "id": subscription.id).returns(subscription.to_hash)
404+
contact.create_subscription_type({ "id": subscription.id })
405+
end
406+
396407
it 'adds a tag to a contact' do
397408
client.expects(:post).with('/contacts/1/tags', "id": tag.id).returns(tag.to_hash)
398409
contact.add_tag({ "id": tag.id })

0 commit comments

Comments
 (0)