Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
22 changes: 19 additions & 3 deletions lib/fitgem/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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={})
Expand All @@ -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={})
Expand All @@ -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={})
Expand All @@ -246,12 +246,28 @@ 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?

raise ServiceUnavailableError if response.code == '503'

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
4 changes: 2 additions & 2 deletions lib/fitgem/notifications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
17 changes: 14 additions & 3 deletions spec/fitgem_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/fitgem_notifications_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down