Skip to content

Commit

Permalink
Add new functionality to HelpScout::Threads (create, update, get) (#13)
Browse files Browse the repository at this point in the history
* Add new functionality to threads (create, update, get)

* Update README to include Thread examples

* Returns true on Thread creation

* Converts thread_id argument to integer for accurate comparison

* Adds cassettes for Thread.create, Thread.get, and Thread#update

* Adds CHANGELOG entry
  • Loading branch information
jjustinwhite authored and prsimp committed Jul 19, 2019
1 parent b951ca2 commit 0a3b933
Show file tree
Hide file tree
Showing 7 changed files with 528 additions and 3 deletions.
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
### master (unreleased)

Enhancements:

* Adds support for `HelpScout::Thread.create`, `HelpScout::Thread.get`, and
`HelpScout::Thread#update` (Justin White, [#13](https://github.com/taxjar/help_scout-sdk/pull/13))

### 1.1.0 / 2019-06-20

### 1.0.2 / 2019-06-07

### 1.0.1 / 2019-06-07

### 1.0.0 / 2019-06-05

Breaking Changes:

* Drops support for the Mailbox 1.0 API ([#2](https://github.com/taxjar/help_scout-sdk/pull/2))

Enhancements:

* Updated to support the Mailbox 2.0 API ([#2](https://github.com/taxjar/help_scout-sdk/pull/2))
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ And then execute:
| :------------------------- | :--: | :--: | :-----: | :----: | :-----: |
| Attachment ||||||
| Conversations ||||||
| Conversation::Threads ||||||
| Conversation::ThreadSource ||||||
| Conversation::Threads ||||||
| Customers ||||||
| Notes ||||||
| Mailboxes ||||||
Expand Down Expand Up @@ -86,6 +85,19 @@ mailbox.fields
mailbox.folders
```

### Threads

[Documentation Link](https://developer.helpscout.com/mailbox-api/endpoints/conversations/threads/list/)

```ruby
conversation = HelpScout::Conversation.list.first
new_thread = HelpScout::Thread.create(conversation.id, "notes", { text: 'Hello, world!' })
threads = HelpScout::Thread.list(conversation.id)
latest_thread = threads.first
latest_thread.update("replace", "/text", "Updating a threads text.")
modified_thread = HelpScout::Thread.get(conversation.id, latest_thread.id)
```

### Users

[Documentation Link](https://developer.helpscout.com/mailbox-api/endpoints/users/list/)
Expand Down
34 changes: 33 additions & 1 deletion lib/help_scout/thread.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,33 @@
module HelpScout
class Thread < HelpScout::Base
class << self
def create(conversation_id, thread_type, params)
HelpScout.api.post(
create_path(conversation_id, thread_type),
HelpScout::Util.camelize_keys(params)
)
true
end

def list(conversation_id, page: nil)
HelpScout.api.get(list_path(conversation_id), page: page).embedded_list.map { |details| new details }
HelpScout.api.get(
list_path(conversation_id), page: page
).embedded_list.map { |details| new details.merge(conversation_id: conversation_id) }
end

def get(conversation_id, thread_id)
threads = list(conversation_id)
thread_id = thread_id.to_i

threads.find { |thread| thread.id == thread_id }
end

private

def create_path(conversation_id, thread_type)
"conversations/#{conversation_id}/#{thread_type}"
end

def list_path(conversation_id)
"conversations/#{conversation_id}/threads"
end
Expand All @@ -32,6 +53,7 @@ def list_path(conversation_id)
created_at
opened_at
attachments
conversation_id
].freeze

attr_accessor(*BASIC_ATTRIBUTES)
Expand All @@ -46,5 +68,15 @@ def initialize(params)

@hrefs = HelpScout::Util.map_links(params.fetch(:_links, []))
end

def conversation
@_conversation ||= HelpScout::Conversation.get(conversation_id)
end

def update(operation, path, value = nil)
update_path = "conversations/#{conversation_id}/threads/#{id}"
HelpScout.api.patch(update_path, op: operation, path: path, value: value)
true
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0a3b933

Please sign in to comment.