diff --git a/lib/fitgem/client.rb b/lib/fitgem/client.rb index 14fe63c..786f0b1 100644 --- a/lib/fitgem/client.rb +++ b/lib/fitgem/client.rb @@ -214,7 +214,7 @@ def access_token end def get(path, headers={}) - extract_response_body raw_get(path, headers) + extract_response_body_and_headers raw_get(path, headers) end def raw_get(path, headers={}) @@ -225,7 +225,7 @@ def raw_get(path, headers={}) end def post(path, body='', headers={}) - extract_response_body raw_post(path, body, headers) + extract_response_body_and_headers raw_post(path, body, headers) end def raw_post(path, body='', headers={}) @@ -236,7 +236,7 @@ def raw_post(path, body='', headers={}) end def delete(path, headers={}) - extract_response_body raw_delete(path, headers) + extract_response_body_and_headers raw_delete(path, headers) end def raw_delete(path, headers={}) @@ -246,6 +246,12 @@ def raw_delete(path, headers={}) access_token.delete(uri, headers) end + def extract_response_body_and_headers(response) + body = extract_response_body(response) + headers = extract_response_headers(response) + body.merge('http_headers' => headers) + end + def extract_response_body(response) return {} if response.nil? @@ -253,5 +259,15 @@ def extract_response_body(response) response.body.nil? ? {} : JSON.parse(response.body) end + + def extract_response_headers(resp) + result = {} + + if !resp.nil? + resp.each_header { |key, value| result[key] = value } + end + + result + end end end diff --git a/lib/fitgem/notifications.rb b/lib/fitgem/notifications.rb index ea6360f..dc58d5b 100644 --- a/lib/fitgem/notifications.rb +++ b/lib/fitgem/notifications.rb @@ -28,7 +28,7 @@ def subscriptions(opts) # @since v0.4.0 def create_subscription(opts) resp = raw_post make_subscription_url(opts.merge({:use_subscription_id => true})), EMPTY_BODY, make_headers(opts) - [resp.code, extract_response_body(resp)] + [resp.code, extract_response_body_and_headers(resp)] end # Removes a notification subscription @@ -49,7 +49,7 @@ def create_subscription(opts) # @since v0.4.0 def remove_subscription(opts) resp = raw_delete make_subscription_url(opts.merge({:use_subscription_id => true})), make_headers(opts) - [resp.code, extract_response_body(resp)] + [resp.code, extract_response_body_and_headers(resp)] end protected diff --git a/spec/fitgem_client_spec.rb b/spec/fitgem_client_spec.rb index a0edce8..1e01fbb 100644 --- a/spec/fitgem_client_spec.rb +++ b/spec/fitgem_client_spec.rb @@ -6,7 +6,11 @@ :consumer_key => '12345', :consumer_secret => '67890' }) } - let(:response) { double :body => {:foo => :bar}.to_json, :code => 200 } + let(:response) { + double :body => {:foo => :bar}.to_json, + :code => 200, + :headers => {'header_name_1' => 'header_value_1'} + } let(:consumer) { double 'Consumer' } before :each do @@ -15,10 +19,17 @@ :authorize_url => "https://www.fitbit.com/oauth/authorize", :proxy => nil}).and_return(consumer) allow(OAuth::AccessToken).to receive(:new).and_return(access_token) + + allow(client).to receive(:extract_response_headers).with(response). + and_return(response.headers) end - it 'returns JSON from the request' do - expect(client.user_info).to eq({'foo' => 'bar'}) + it 'returns JSON body and headers from the request' do + expected_result = { + 'foo' => 'bar', + 'http_headers' => { 'header_name_1' => 'header_value_1' } + } + expect(client.user_info).to eq(expected_result) end it 'raises a service unavailable exception when the status is 503' do diff --git a/spec/fitgem_notifications_spec.rb b/spec/fitgem_notifications_spec.rb index 710e9b0..6d62934 100644 --- a/spec/fitgem_notifications_spec.rb +++ b/spec/fitgem_notifications_spec.rb @@ -50,7 +50,7 @@ it "calls #extract_response_body to get the JSON body" do opts = { :subscriber_id => "5555", :type => :all, :subscription_id => "320", :use_subscription_id => true } - expect(@client).to receive(:extract_response_body) + expect(@client).to receive(:extract_response_body_and_headers) @client.create_subscription(opts) end @@ -81,7 +81,7 @@ it "calls #extract_response_body to get the JSON body" do opts = { :subscriber_id => "5555", :type => :all, :subscription_id => "320", :use_subscription_id => true } - expect(@client).to receive(:extract_response_body) + expect(@client).to receive(:extract_response_body_and_headers) @client.remove_subscription(opts) end