Skip to content

Commit fae9d35

Browse files
authored
Release v4.1.0 (#531)
* Articles endpoint SDK support * Collections Endpoint Support (#523) * Sections Endpoint Support (#524) * Added Collections endpoint SDK support * Added test coverage * Added dirty_tracking support for nested objects (#525) * Added support to check for equality of two resources (#527) * Updated README and logged changes for v4.1.0 (#526)
1 parent 276b5b8 commit fae9d35

16 files changed

+488
-5
lines changed

README.md

+101-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ intercom = Intercom::Client.new(token: 'my_token')
3636

3737
```ruby
3838
# With a versioned app:
39-
intercom = Intercom::Client.new(token: 'my_token', api_version: '2.0')
39+
intercom = Intercom::Client.new(token: 'my_token', api_version: '2.1')
4040
```
4141

4242
If you are building a third party application you can get your access_tokens by [setting-up-oauth](https://developers.intercom.io/page/setting-up-oauth) for Intercom.
@@ -61,6 +61,9 @@ Resources this API supports:
6161
https://api.intercom.io/counts
6262
https://api.intercom.io/subscriptions
6363
https://api.intercom.io/jobs
64+
https://api.intercom.io/articles
65+
https://api.intercom.io/help_center/collections
66+
https://api.intercom.io/help_center/sections
6467

6568

6669
### Examples
@@ -525,6 +528,103 @@ intercom.subscriptions.delete(subscription)
525528
intercom.subscriptions.all
526529
```
527530

531+
#### Articles
532+
```ruby
533+
# Create an article
534+
article = intercom.articles.create(title: "New Article", author_id: "123456")
535+
536+
# Create an article with translations
537+
article = intercom.articles.create(title: "New Article",
538+
author_id: "123456",
539+
translated_content: {fr: {title: "Nouvel Article"}, es: {title: "Nuevo artículo"}})
540+
541+
# Fetch an article
542+
intercom.articles.find(id: "123456")
543+
544+
# List all articles
545+
articles = intercom.articles.all
546+
articles.each { |article| p article.title }
547+
548+
# Update an article
549+
article.title = "Article Updated!"
550+
intercom.articles.save(article)
551+
552+
# Update an article's existing translation
553+
article.translated_content.en.title = "English Updated!"
554+
intercom.articles.save(article)
555+
556+
# Update an article by adding a new translation
557+
article.translated_content.es = {title: "Artículo en español"}
558+
intercom.articles.save(article)
559+
560+
# Delete an article
561+
intercom.articles.delete(article)
562+
```
563+
564+
#### Collections
565+
```ruby
566+
# Create a collection
567+
collection = intercom.collections.create(name: "New Collection")
568+
569+
# Create a collection with translations
570+
collection = intercom.collections.create(name: "New Collection",
571+
translated_content: {fr: {name: "Nouvelle collection"}, es: {name: "Nueva colección"}})
572+
573+
# Fetch a collection
574+
intercom.collections.find(id: "123456")
575+
576+
# List all collections
577+
collections = intercom.collections.all
578+
collections.each { |collection| p collection.name }
579+
580+
# Update a collection
581+
collection.name = "Collection updated!"
582+
intercom.collections.save(collection)
583+
584+
# Update a collection's existing translation
585+
collection.translated_content.en.name = "English Updated!"
586+
intercom.collections.save(collection)
587+
588+
# Update a collection by adding a new translation
589+
collection.translated_content.es = {name: "Colección en español", description: "Descripción en español"}
590+
intercom.collections.save(collection)
591+
592+
# Delete an collection
593+
intercom.collections.delete(collection)
594+
```
595+
596+
#### Sections
597+
```ruby
598+
# Create a section
599+
section = intercom.sections.create(name: "New Section", parent_id: "123456")
600+
601+
# Create a section with translations
602+
section = intercom.sections.create(name: "New Section",
603+
translated_content: {fr: {name: "Nouvelle section"}, es: {name: "Nueva sección"}})
604+
605+
# Fetch a section
606+
intercom.sections.find(id: "123456")
607+
608+
# List all sections
609+
sections = intercom.sections.all
610+
sections.each { |section| p section.name }
611+
612+
# Update a section
613+
section.name = "Section updated!"
614+
intercom.sections.save(section)
615+
616+
# Update a section's existing translation
617+
section.translated_content.en.name = "English Updated!"
618+
intercom.collections.save(section)
619+
620+
# Update a section by adding a new translation
621+
section.translated_content.es = {name: "Sección en español"}
622+
intercom.collections.save(section)
623+
624+
# Delete an section
625+
intercom.sections.delete(section)
626+
```
627+
528628
### Errors
529629

530630
There are different styles for error handling - some people prefer exceptions; some prefer nil and check; some prefer error objects/codes. Balancing these preferences alongside our wish to provide an idiomatic gem has brought us to use the current mechanism of throwing specific exceptions. Our approach in the client is to propagate errors and signal our failure loudly so that erroneous data does not get propagated through our customers' systems - in other words, if you see a `Intercom::ServiceUnavailableError` you know where the problem is.

lib/intercom.rb

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
require 'intercom/version'
44
require 'intercom/service/admin'
5+
require 'intercom/service/article'
6+
require 'intercom/service/collection'
57
require 'intercom/service/company'
68
require 'intercom/service/contact'
79
require 'intercom/service/conversation'
@@ -12,6 +14,7 @@
1214
require 'intercom/service/job'
1315
require 'intercom/service/subscription'
1416
require 'intercom/service/segment'
17+
require 'intercom/service/section'
1518
require 'intercom/service/tag'
1619
require 'intercom/service/team'
1720
require 'intercom/service/visitor'
@@ -24,16 +27,19 @@
2427
require 'intercom/user'
2528
require 'intercom/lead'
2629
require 'intercom/count'
30+
require 'intercom/collection'
2731
require 'intercom/company'
2832
require 'intercom/service/data_attribute'
2933
require 'intercom/note'
3034
require 'intercom/job'
3135
require 'intercom/tag'
3236
require 'intercom/segment'
37+
require 'intercom/section'
3338
require 'intercom/event'
3439
require 'intercom/conversation'
3540
require 'intercom/message'
3641
require 'intercom/admin'
42+
require 'intercom/article'
3743
require 'intercom/request'
3844
require 'intercom/subscription'
3945
require 'intercom/team'

lib/intercom/article.rb

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

lib/intercom/client.rb

+12
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ def admins
4848
Intercom::Service::Admin.new(self)
4949
end
5050

51+
def articles
52+
Intercom::Service::Article.new(self)
53+
end
54+
5155
def companies
5256
Intercom::Service::Company.new(self)
5357
end
@@ -84,6 +88,10 @@ def segments
8488
Intercom::Service::Segment.new(self)
8589
end
8690

91+
def sections
92+
Intercom::Service::Section.new(self)
93+
end
94+
8795
def tags
8896
Intercom::Service::Tag.new(self)
8997
end
@@ -108,6 +116,10 @@ def data_attributes
108116
Intercom::Service::DataAttribute.new(self)
109117
end
110118

119+
def collections
120+
Intercom::Service::Collection.new(self)
121+
end
122+
111123
def get(path, params)
112124
execute_request Intercom::Request.get(path, params)
113125
end

lib/intercom/collection.rb

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

lib/intercom/section.rb

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require 'intercom/api_operations/list'
2+
require 'intercom/api_operations/find'
3+
require 'intercom/api_operations/save'
4+
require 'intercom/api_operations/delete'
5+
6+
module Intercom
7+
module Service
8+
class Section < BaseService
9+
include ApiOperations::List
10+
include ApiOperations::Find
11+
include ApiOperations::Save
12+
include ApiOperations::Delete
13+
14+
def collection_class
15+
Intercom::Section
16+
end
17+
18+
def collection_name
19+
'help_center/sections'
20+
end
21+
end
22+
end
23+
end

lib/intercom/service/article.rb

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require 'intercom/service/base_service'
2+
require 'intercom/api_operations/find'
3+
require 'intercom/api_operations/list'
4+
require 'intercom/api_operations/delete'
5+
require 'intercom/api_operations/save'
6+
7+
module Intercom
8+
module Service
9+
class Article < BaseService
10+
include ApiOperations::Find
11+
include ApiOperations::List
12+
include ApiOperations::Delete
13+
include ApiOperations::Save
14+
15+
def collection_class
16+
Intercom::Article
17+
end
18+
end
19+
end
20+
end

lib/intercom/service/collection.rb

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require 'intercom/service/base_service'
2+
require 'intercom/api_operations/list'
3+
require 'intercom/api_operations/find'
4+
require 'intercom/api_operations/delete'
5+
require 'intercom/api_operations/save'
6+
7+
module Intercom
8+
module Service
9+
class Collection < BaseService
10+
include ApiOperations::List
11+
include ApiOperations::Find
12+
include ApiOperations::Delete
13+
include ApiOperations::Save
14+
15+
def collection_class
16+
Intercom::Collection
17+
end
18+
19+
def collection_name
20+
"help_center/collections"
21+
end
22+
end
23+
end
24+
end

lib/intercom/service/section.rb

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

lib/intercom/traits/api_resource.rb

+16-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ def initialize(attributes = {})
1717
from_hash(attributes)
1818
end
1919

20+
def ==(other)
21+
self.class == other.class && to_json == other.to_json
22+
end
23+
2024
def from_response(response)
2125
from_hash(response)
2226
reset_changed_fields!
@@ -37,10 +41,21 @@ def to_hash
3741
end
3842
end
3943

44+
def to_json(*args)
45+
instance_variables_excluding_dirty_tracking_field.each_with_object({}) do |variable, hash|
46+
next if variable == :@client
47+
48+
value = instance_variable_get(variable)
49+
hash[variable.to_s.delete('@')] = value.respond_to?(:to_json) ? value.to_json(*args) : value
50+
end
51+
end
52+
4053
def to_submittable_hash
4154
submittable_hash = {}
4255
to_hash.each do |attribute, value|
43-
submittable_hash[attribute] = value if submittable_attribute?(attribute, value)
56+
next unless submittable_attribute?(attribute, value)
57+
58+
submittable_hash[attribute] = value.respond_to?(:to_submittable_hash) ? value.to_submittable_hash : value
4459
end
4560
submittable_hash
4661
end

lib/intercom/traits/dirty_tracking.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,14 @@ def mark_field_as_changed!(field_name)
2222

2323
def field_changed?(field_name)
2424
@changed_fields ||= Set.new
25-
@changed_fields.include?(field_name.to_s)
25+
field = instance_variable_get("@#{field_name}")
26+
if field.respond_to?(:field_changed?)
27+
field.to_hash.any? do |attribute, _|
28+
field.field_changed?(attribute)
29+
end
30+
else
31+
@changed_fields.include?(field_name.to_s)
32+
end
2633
end
2734

2835
def instance_variables_excluding_dirty_tracking_field

0 commit comments

Comments
 (0)