Compare commits

...

3 Commits

Author SHA1 Message Date
J. Paul Daigle
ccea3a4ea0 Merge pull request #20 from heisler3030/add_error_checking
Add error checking
2014-11-05 10:51:07 -05:00
Hamish Eisler
9c24e3acee add response code 2014-11-04 16:42:10 -08:00
Hamish Eisler
44253dd34e throw errors on HTTP error code 2014-11-04 16:26:00 -08:00
3 changed files with 18 additions and 1 deletions

View File

@ -54,6 +54,7 @@ module BitPay
def request(request_klass, path, params=nil)
request = make_request(request_klass, path, params)
response = @https.request(request)
raise BitPayError, "HTTP Status " + response.code + " with body: " + response.body unless response.kind_of?(Net::HTTPSuccess)
return JSON.parse(response.body)
rescue => e
fail BitPayError, "Bitpay Request Error: #{e}"

View File

@ -1,3 +1,3 @@
module BitPay
VERSION = '0.1.4'
VERSION = '0.1.5'
end

View File

@ -20,6 +20,10 @@ def invoice_response_body
}
end
def unauthorized_key_body
{"error" => {"type" => "unauthorized", "message" => "invalid api key"}}
end
stub_request(:post, "https://KEY:@bitpay.com/api/invoice/create").
with(
:headers => {'User-Agent'=>USER_AGENT, 'Content-Type' => 'application/json'},
@ -31,6 +35,10 @@ stub_request(:get, "https://KEY:@bitpay.com/api/invoice/DGrAEmbsXe9bavBPMJ8kuk")
with(:headers => {'User-Agent'=>USER_AGENT}).
to_return(:body => invoice_response_body.to_json)
stub_request(:get, "https://KEY:@bitpay.com/api/invoice/BADAPIKEY").
with(:headers => {'User-Agent'=>USER_AGENT}).
to_return(:status => 403, :body => unauthorized_key_body.to_json)
describe BitPay::Client do
before do
@client = BitPay::Client.new 'KEY'
@ -51,4 +59,12 @@ describe BitPay::Client do
response['id'].must_equal 'DGrAEmbsXe9bavBPMJ8kuk'
end
end
describe 'bad API Key' do
it 'returns Error' do
assert_raises(BitPay::Client::BitPayError) {
response = @client.get 'invoice/BADAPIKEY'
}
end
end
end