Skip to content

Commit

Permalink
Merge pull request #196 from bonusly/master
Browse files Browse the repository at this point in the history
Added the reply endpoint
  • Loading branch information
rberrelleza authored May 18, 2017
2 parents d44086e + 291d325 commit 4a28188
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 9 deletions.
11 changes: 11 additions & 0 deletions lib/hipchat/api_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,17 @@ def send_config
}[version]
end

def reply_config
{
'v2' => {
:url => URI::escape("/#{room_id}/reply"),
:method => :post,
:query_params => { },
:body_format => :to_json
}
}[version]
end

def send_file_config
{
'v2' => {
Expand Down
7 changes: 1 addition & 6 deletions lib/hipchat/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,7 @@ def setup_proxy(proxy_url)
end

def _rooms
response = self.class.get(@api.rooms_config[:url],
:query => {
:auth_token => @token
},
:headers => @api.headers
)
response = self.class.get(@api.rooms_config[:url], query: { auth_token: @token }, headers: @api.headers)

ErrorHandler.response_code_to_exception_for :room, nil, response
response[@api.rooms_config[:data_key]].map do |r|
Expand Down
10 changes: 10 additions & 0 deletions lib/hipchat/room.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ def send(from, message, options_or_notify = {})
true
end

def reply(parent_message_id, message)
query_params = { auth_token: @token }.merge(@api.reply_config[:query_params])
body = { message: message, parent_message_id: parent_message_id }.send(@api.reply_config[:body_format])

response = self.class.post(@api.reply_config[:url], query: query_params, body: body, headers: @api.headers)

ErrorHandler.response_code_to_exception_for :room, 'all', response
response.parsed_response
end

def share_link(from, message, link)
if from.length > 20
raise UsernameTooLong, "Username #{from} is `#{from.length} characters long. Limit is 20'"
Expand Down
2 changes: 1 addition & 1 deletion lib/hipchat/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def view
)

ErrorHandler.response_code_to_exception_for :user, user_id, response
User.new(@token, response.merge(:api_version => @api.version))
User.new(@token, response.merge(:api_version => @api.version, :server_url => server_url))
end

#
Expand Down
34 changes: 32 additions & 2 deletions spec/hipchat_api_v2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
expect(subject.rooms.first.history).to be_truthy
end

it "fails when the room doen't exist" do
it "fails when the room doesn't exist" do
allow(room.class).to receive(:get).with(anything, anything).and_return(OpenStruct.new(:code => 404))

expect { room.history }.to raise_error(HipChat::UnknownRoom)
Expand Down Expand Up @@ -120,7 +120,7 @@
expect(subject.rooms.first.statistics).to be_truthy
end

it "fails when the room doen't exist" do
it "fails when the room doesn't exist" do
allow(room.class).to receive(:get).with(anything, anything).and_return(OpenStruct.new(:code => 404))

expect { room.statistics }.to raise_error(HipChat::UnknownRoom)
Expand Down Expand Up @@ -261,6 +261,36 @@
end
end

describe '#reply' do
include_context 'HipChatV2'
let(:parent_id) { '100000' }
let(:message) { 'Hello world' }

it 'successfully' do
mock_successful_reply parent_id, message

expect(room.reply(parent_id, message))
end

it "but fails when the parent_id doesn't exist" do
allow(room.class).to receive(:post).and_return(OpenStruct.new(:code => 404))

expect { room.reply parent_id, message }.to raise_error(HipChat::UnknownRoom)
end

it "but fails when we're not allowed to do so" do
allow(room.class).to receive(:post).and_return(OpenStruct.new(:code => 401))

expect { room.reply parent_id, message }.to raise_error(HipChat::Unauthorized)
end

it 'but fails if we get an unknown response code' do
allow(room.class).to receive(:post).and_return(OpenStruct.new(:code => 403))

expect { room.reply parent_id, message }.to raise_error(HipChat::Unauthorized)
end
end

describe '#share_link' do
let(:link) { "http://i.imgur.com/cZ6GDFY.jpg" }
include_context "HipChatV2"
Expand Down
10 changes: 10 additions & 0 deletions spec/support/shared_contexts_for_hipchat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ def mock_successful_send(from, message, options={})
'Content-Type' => 'application/json'}).to_return(:status => 200, :body => "", :headers => {})
end

def mock_successful_reply(parent_message_id, message)
stub_request(:post, 'https://api.hipchat.com/v2/room/Hipchat/reply?auth_token=blah')
.with(query: { auth_token: 'blah' },
body: { parent_message_id: parent_message_id,
message: message },
headers: { 'Accept' => 'application/json',
'Content-Type' => 'application/json' })
.to_return(status: 200, body: '', headers: {})
end

def mock_successful_link_share(from, message, link)
stub_request(:post, "https://api.hipchat.com/v2/room/Hipchat/share/link").with(
:query => {:auth_token => "blah"},
Expand Down

0 comments on commit 4a28188

Please sign in to comment.