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
102 changes: 101 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ intercom = Intercom::Client.new(token: 'my_token')

```ruby
# With a versioned app:
intercom = Intercom::Client.new(token: 'my_token', api_version: '2.0')
intercom = Intercom::Client.new(token: 'my_token', api_version: '2.1')
```

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.
Expand All @@ -61,6 +61,9 @@ Resources this API supports:
https://api.intercom.io/counts
https://api.intercom.io/subscriptions
https://api.intercom.io/jobs
https://api.intercom.io/articles
https://api.intercom.io/help_center/collections
https://api.intercom.io/help_center/sections


### Examples
Expand Down Expand Up @@ -525,6 +528,103 @@ intercom.subscriptions.delete(subscription)
intercom.subscriptions.all
```

#### Articles
```ruby
# Create an article
article = intercom.articles.create(title: "New Article", author_id: "123456")

# Create an article with translations
article = intercom.articles.create(title: "New Article",
author_id: "123456",
translated_content: {fr: {title: "Nouvel Article"}, es: {title: "Nuevo artículo"}})

# Fetch an article
intercom.articles.find(id: "123456")

# List all articles
articles = intercom.articles.all
articles.each { |article| p article.title }

# Update an article
article.title = "Article Updated!"
intercom.articles.save(article)

# Update an article's existing translation
article.translated_content.en.title = "English Updated!"
intercom.articles.save(article)

# Update an article by adding a new translation
article.translated_content.es = {title: "Artículo en español"}
intercom.articles.save(article)

# Delete an article
intercom.articles.delete(article)
```

#### Collections
```ruby
# Create a collection
collection = intercom.collections.create(name: "New Collection")

# Create a collection with translations
collection = intercom.collections.create(name: "New Collection",
translated_content: {fr: {name: "Nouvelle collection"}, es: {name: "Nueva colección"}})

# Fetch a collection
intercom.collections.find(id: "123456")

# List all collections
collections = intercom.collections.all
collections.each { |collection| p collection.name }

# Update a collection
collection.name = "Collection updated!"
intercom.collections.save(collection)

# Update a collection's existing translation
collection.translated_content.en.name = "English Updated!"
intercom.collections.save(collection)

# Update a collection by adding a new translation
collection.translated_content.es = {name: "Colección en español", description: "Descripción en español"}
intercom.collections.save(collection)

# Delete an collection
intercom.collections.delete(collection)
```

#### Sections
```ruby
# Create a section
section = intercom.sections.create(name: "New Section", parent_id: "123456")

# Create a section with translations
section = intercom.sections.create(name: "New Section",
translated_content: {fr: {name: "Nouvelle section"}, es: {name: "Nueva sección"}})

# Fetch a section
intercom.sections.find(id: "123456")

# List all sections
sections = intercom.sections.all
sections.each { |section| p section.name }

# Update a section
section.name = "Section updated!"
intercom.sections.save(section)

# Update a section's existing translation
section.translated_content.en.name = "English Updated!"
intercom.collections.save(section)

# Update a section by adding a new translation
section.translated_content.es = {name: "Sección en español"}
intercom.collections.save(section)

# Delete an section
intercom.sections.delete(section)
```

### Errors

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.
Expand Down
6 changes: 6 additions & 0 deletions lib/intercom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

require 'intercom/version'
require 'intercom/service/admin'
require 'intercom/service/article'
require 'intercom/service/collection'
require 'intercom/service/company'
require 'intercom/service/contact'
require 'intercom/service/conversation'
Expand All @@ -12,6 +14,7 @@
require 'intercom/service/job'
require 'intercom/service/subscription'
require 'intercom/service/segment'
require 'intercom/service/section'
require 'intercom/service/tag'
require 'intercom/service/team'
require 'intercom/service/visitor'
Expand All @@ -24,16 +27,19 @@
require 'intercom/user'
require 'intercom/lead'
require 'intercom/count'
require 'intercom/collection'
require 'intercom/company'
require 'intercom/service/data_attribute'
require 'intercom/note'
require 'intercom/job'
require 'intercom/tag'
require 'intercom/segment'
require 'intercom/section'
require 'intercom/event'
require 'intercom/conversation'
require 'intercom/message'
require 'intercom/admin'
require 'intercom/article'
require 'intercom/request'
require 'intercom/subscription'
require 'intercom/team'
Expand Down
7 changes: 7 additions & 0 deletions lib/intercom/article.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'intercom/traits/api_resource'

module Intercom
class Article
include Traits::ApiResource
end
end
12 changes: 12 additions & 0 deletions lib/intercom/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ def admins
Intercom::Service::Admin.new(self)
end

def articles
Intercom::Service::Article.new(self)
end

def companies
Intercom::Service::Company.new(self)
end
Expand Down Expand Up @@ -84,6 +88,10 @@ def segments
Intercom::Service::Segment.new(self)
end

def sections
Intercom::Service::Section.new(self)
end

def tags
Intercom::Service::Tag.new(self)
end
Expand All @@ -108,6 +116,10 @@ def data_attributes
Intercom::Service::DataAttribute.new(self)
end

def collections
Intercom::Service::Collection.new(self)
end

def get(path, params)
execute_request Intercom::Request.get(path, params)
end
Expand Down
7 changes: 7 additions & 0 deletions lib/intercom/collection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'intercom/traits/api_resource'

module Intercom
class Collection
include Traits::ApiResource
end
end
23 changes: 23 additions & 0 deletions lib/intercom/section.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'intercom/api_operations/list'
require 'intercom/api_operations/find'
require 'intercom/api_operations/save'
require 'intercom/api_operations/delete'

module Intercom
module Service
class Section < BaseService
include ApiOperations::List
include ApiOperations::Find
include ApiOperations::Save
include ApiOperations::Delete

def collection_class
Intercom::Section
end

def collection_name
'help_center/sections'
end
end
end
end
20 changes: 20 additions & 0 deletions lib/intercom/service/article.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'intercom/service/base_service'
require 'intercom/api_operations/find'
require 'intercom/api_operations/list'
require 'intercom/api_operations/delete'
require 'intercom/api_operations/save'

module Intercom
module Service
class Article < BaseService
include ApiOperations::Find
include ApiOperations::List
include ApiOperations::Delete
include ApiOperations::Save

def collection_class
Intercom::Article
end
end
end
end
24 changes: 24 additions & 0 deletions lib/intercom/service/collection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'intercom/service/base_service'
require 'intercom/api_operations/list'
require 'intercom/api_operations/find'
require 'intercom/api_operations/delete'
require 'intercom/api_operations/save'

module Intercom
module Service
class Collection < BaseService
include ApiOperations::List
include ApiOperations::Find
include ApiOperations::Delete
include ApiOperations::Save

def collection_class
Intercom::Collection
end

def collection_name
"help_center/collections"
end
end
end
end
7 changes: 7 additions & 0 deletions lib/intercom/service/section.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'intercom/traits/api_resource'

module Intercom
class Section
include Traits::ApiResource
end
end
17 changes: 16 additions & 1 deletion lib/intercom/traits/api_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def initialize(attributes = {})
from_hash(attributes)
end

def ==(other)
self.class == other.class && to_json == other.to_json
end

def from_response(response)
from_hash(response)
reset_changed_fields!
Expand All @@ -37,10 +41,21 @@ def to_hash
end
end

def to_json(*args)
instance_variables_excluding_dirty_tracking_field.each_with_object({}) do |variable, hash|
next if variable == :@client

value = instance_variable_get(variable)
hash[variable.to_s.delete('@')] = value.respond_to?(:to_json) ? value.to_json(*args) : value
end
end

def to_submittable_hash
submittable_hash = {}
to_hash.each do |attribute, value|
submittable_hash[attribute] = value if submittable_attribute?(attribute, value)
next unless submittable_attribute?(attribute, value)

submittable_hash[attribute] = value.respond_to?(:to_submittable_hash) ? value.to_submittable_hash : value
end
submittable_hash
end
Expand Down
9 changes: 8 additions & 1 deletion lib/intercom/traits/dirty_tracking.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@ def mark_field_as_changed!(field_name)

def field_changed?(field_name)
@changed_fields ||= Set.new
@changed_fields.include?(field_name.to_s)
field = instance_variable_get("@#{field_name}")
if field.respond_to?(:field_changed?)
field.to_hash.any? do |attribute, _|
field.field_changed?(attribute)
end
else
@changed_fields.include?(field_name.to_s)
end
end

def instance_variables_excluding_dirty_tracking_field
Expand Down
Loading