From 07411ee7dab8c5c6d74f24cd752479694ddf3f8c Mon Sep 17 00:00:00 2001 From: Robert Ingrum Date: Wed, 17 May 2017 08:50:42 -0600 Subject: [PATCH 1/2] Adding reply endpoint --- lib/hipchat/api_version.rb | 11 +++++++ lib/hipchat/client.rb | 7 +---- lib/hipchat/room.rb | 10 ++++++ lib/hipchat/user.rb | 2 +- spec/hipchat_api_v2_spec.rb | 34 +++++++++++++++++++-- spec/support/shared_contexts_for_hipchat.rb | 10 ++++++ 6 files changed, 65 insertions(+), 9 deletions(-) diff --git a/lib/hipchat/api_version.rb b/lib/hipchat/api_version.rb index 4178cda..b018ffc 100644 --- a/lib/hipchat/api_version.rb +++ b/lib/hipchat/api_version.rb @@ -186,6 +186,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' => { diff --git a/lib/hipchat/client.rb b/lib/hipchat/client.rb index 1bf5b6b..0737165 100644 --- a/lib/hipchat/client.rb +++ b/lib/hipchat/client.rb @@ -96,12 +96,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| diff --git a/lib/hipchat/room.rb b/lib/hipchat/room.rb index df24c96..94e81ad 100644 --- a/lib/hipchat/room.rb +++ b/lib/hipchat/room.rb @@ -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'" diff --git a/lib/hipchat/user.rb b/lib/hipchat/user.rb index 6c19a5b..c7891bb 100644 --- a/lib/hipchat/user.rb +++ b/lib/hipchat/user.rb @@ -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 # diff --git a/spec/hipchat_api_v2_spec.rb b/spec/hipchat_api_v2_spec.rb index 2e485b6..75820e7 100644 --- a/spec/hipchat_api_v2_spec.rb +++ b/spec/hipchat_api_v2_spec.rb @@ -29,7 +29,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) @@ -64,7 +64,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) @@ -205,6 +205,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" diff --git a/spec/support/shared_contexts_for_hipchat.rb b/spec/support/shared_contexts_for_hipchat.rb index 01857c2..4a73d82 100644 --- a/spec/support/shared_contexts_for_hipchat.rb +++ b/spec/support/shared_contexts_for_hipchat.rb @@ -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"}, From 291d32530610cf672185a0ca09775e610a003971 Mon Sep 17 00:00:00 2001 From: Robert Ingrum Date: Wed, 17 May 2017 08:55:37 -0600 Subject: [PATCH 2/2] Making compatible with old ruby version --- spec/support/shared_contexts_for_hipchat.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/support/shared_contexts_for_hipchat.rb b/spec/support/shared_contexts_for_hipchat.rb index 4a73d82..0c47de5 100644 --- a/spec/support/shared_contexts_for_hipchat.rb +++ b/spec/support/shared_contexts_for_hipchat.rb @@ -128,8 +128,8 @@ def mock_successful_reply(parent_message_id, message) .with(query: { auth_token: 'blah' }, body: { parent_message_id: parent_message_id, message: message }, - headers: { 'Accept': 'application/json', - 'Content-Type': 'application/json' }) + headers: { 'Accept' => 'application/json', + 'Content-Type' => 'application/json' }) .to_return(status: 200, body: '', headers: {}) end