Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new functionality to HelpScout::Threads (create, update, get) #13

Merged
merged 7 commits into from
Jul 19, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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: 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
31 changes: 30 additions & 1 deletion lib/help_scout/thread.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,30 @@
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)
)
end
prsimp marked this conversation as resolved.
Show resolved Hide resolved

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)
threads.find { |thread| thread.id == thread_id }
prsimp marked this conversation as resolved.
Show resolved Hide resolved
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 +50,7 @@ def list_path(conversation_id)
created_at
opened_at
attachments
conversation_id
].freeze

attr_accessor(*BASIC_ATTRIBUTES)
Expand All @@ -46,5 +65,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
39 changes: 39 additions & 0 deletions spec/integration/thread_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

RSpec.describe HelpScout::Thread do
let(:conversation_id) { ENV.fetch('TEST_CONVERSATION_ID') }
let(:thread_id) { ENV.fetch('TEST_THREAD_ID') }

describe '.get' do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels like our getable shared example could be expanded to handle multiple args, but since I think this is the first instance of it, I'm cool with saving that for later.

it "returns a #{described_class} given a conversation_id and thread_id" do
thread = described_class.get(conversation_id, thread_id)

expect(thread).to be_a(described_class)
end
end

describe '.list' do
subject { described_class.list(conversation_id) }
Expand All @@ -11,4 +20,34 @@
expect(subject).to all(be_a(described_class))
end
end

describe '.create' do
subject { described_class.create(conversation_id, thread_type, params) }
let(:thread_type) { 'notes' }
let(:params) do
{
text: 'Hello, note!'
}
end

it 'creates a new thread on a given conversation' do
expect(subject.success?).to eq(true)
prsimp marked this conversation as resolved.
Show resolved Hide resolved
end
end

describe '#update' do
it "updates a given thread's text on a conversation" do
thread = described_class.get(conversation_id, thread_id)
original_body = thread.body
update_params = {
op: 'replace',
path: '/text',
value: original_body.reverse
}

expect(thread.update(*update_params.values)).to be true
new_body = described_class.get(conversation_id, thread_id).body
expect(new_body).to eq(update_params[:value])
end
end
end