Skip to content

Commit

Permalink
Implements exception throwing when making requests
Browse files Browse the repository at this point in the history
  • Loading branch information
inf0rmer committed Dec 29, 2014
1 parent a4861d1 commit 7395c2b
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 24 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,26 @@ users_endpoint = api.users(55)
users_endpoint.extension = :xml
```

### Handling Exceptions

Blanket will raise exceptions for HTTP errors encountered while making requests. Exception subclasses are raised for well known errors (404, 500, etc.) but for other status codes a default `Blanket::Exception` will be raised instead.

```ruby
begin
api.thingamajig.get
rescue Blanket::ResourceNotFound => e
e.code
# => 404

e.message
# => "404: Resource Not Found"

# The HTTP body, ie. the error message sent by the server
e.body
# => "Could not find this resource!"
end
```

## Contributing

1. Fork it ( https://github.com/[my-github-username]/blanket/fork )
Expand Down
7 changes: 4 additions & 3 deletions lib/blanket.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "blanket/version"
require "blanket/response"
require "blanket/wrapper"
require_relative "blanket/version"
require_relative "blanket/response"
require_relative "blanket/exception"
require_relative "blanket/wrapper"
require 'httparty'

module Blanket
Expand Down
16 changes: 15 additions & 1 deletion lib/blanket/exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,27 @@ def message
@message || self.class.name
end

def inspect
"#{message}: #{body}"
end

def to_s
inspect
end
end

# We will a create an exception for each status code, see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
module Exceptions
# Map http status codes to the corresponding exception class
EXCEPTIONS_MAP = {}
end

STATUSES.each_pair do |code, message|
klass = Class.new(Exception) do
send(:define_method, :message) {"#{code ? "#{code} " : ''}#{message}"}
end

const_set message.delete(' \-\''), klass
klass_constant = const_set message.delete(' \-\''), klass
Exceptions::EXCEPTIONS_MAP[code] = klass_constant
end
end
9 changes: 6 additions & 3 deletions lib/blanket/wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,12 @@ def request(method, id=nil, options={})
headers: headers
}.reject { |k, v| v.nil? || v.empty? })

body = (response.respond_to? :body) ? response.body : nil

(body.is_a? Array) ? body.map(Response.new) : Response.new(body)
if response.code <= 400
body = (response.respond_to? :body) ? response.body : nil
(body.is_a? Array) ? body.map(Response.new) : Response.new(body)
else
raise Blanket::Exceptions::EXCEPTIONS_MAP[response.code].new(response)
end
end

def merged_headers(headers)
Expand Down
65 changes: 48 additions & 17 deletions spec/blanket/wrapper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
end

describe 'Making Requests' do
before :each do
allow(HTTParty).to receive(:get)
end

it 'resets after performing a request' do
allow(HTTParty).to receive(:get) { StubbedResponse.new }

api.users.get()
api.videos.get()

Expand All @@ -20,19 +18,40 @@

describe "Response" do
before :each do
stub_request(:get, "http://api.example.org/users").to_return(:body => '{"title": "Something"}')
stub_request(:get, "http://api.example.org/users")
.to_return(:body => '{"title": "Something"}')
end

let :response do
api.users.get()
api.users.get
end

it "returns a Blanket::Response instance" do
expect(response).to be_kind_of(Blanket::Response)
end
end

describe "Exceptions" do
let :api do
Blanket::wrap("http://api.example.org")
end

describe "500" do
before :each do
stub_request(:get, "http://api.example.org/users").to_return(:status => 500, :body => "You've been met with a terrible fate, haven't you?")
end

it "raises a Blanket::InternalServerError exception" do
expect { api.users.get }.to raise_exception(Blanket::InternalServerError)
end
end
end

describe 'Resource identification' do
before do
allow(HTTParty).to receive(:get) { StubbedResponse.new }
end

context 'When using a resource method' do
it 'allows identifying a resource' do
api.users(55).get()
Expand All @@ -57,6 +76,10 @@
end

describe 'Headers' do
before :each do
allow(HTTParty).to receive(:get) { StubbedResponse.new }
end

it 'allows sending headers in a request' do
api.users(55).get(headers: {foo: 'bar'})

Expand All @@ -72,6 +95,10 @@
end

describe 'Parameters' do
before :each do
allow(HTTParty).to receive(:get) { StubbedResponse.new }
end

it 'allows sending parameters in a request' do
api.users(55).get(params: {foo: 'bar'})

Expand All @@ -80,7 +107,11 @@
end

describe 'URL Extension' do
it 'allows setting an extension for a request', :wip => true do
before :each do
allow(HTTParty).to receive(:get) { StubbedResponse.new }
end

it 'allows setting an extension for a request' do
users_endpoint = api.users(55)
users_endpoint.extension = :json
users_endpoint.get
Expand All @@ -98,8 +129,8 @@
end

describe '#get' do
before :each do
allow(HTTParty).to receive(:get)
before do
allow(HTTParty).to receive(:get) { StubbedResponse.new }
end

it 'GETs a resource' do
Expand All @@ -110,8 +141,8 @@
end

describe '#post' do
before :each do
allow(HTTParty).to receive(:post)
before do
allow(HTTParty).to receive(:post) { StubbedResponse.new }
end

it 'POSTs a resource' do
Expand All @@ -122,8 +153,8 @@
end

describe '#put' do
before :each do
allow(HTTParty).to receive(:put)
before do
allow(HTTParty).to receive(:put) { StubbedResponse.new }
end

it 'PUTs a resource' do
Expand All @@ -134,8 +165,8 @@
end

describe '#patch' do
before :each do
allow(HTTParty).to receive(:patch)
before do
allow(HTTParty).to receive(:patch) { StubbedResponse.new }
end

it 'PATCHes a resource' do
Expand All @@ -146,8 +177,8 @@
end

describe '#delete' do
before :each do
allow(HTTParty).to receive(:delete)
before do
allow(HTTParty).to receive(:delete) { StubbedResponse.new }
end

it 'DELETEs a resource' do
Expand Down
6 changes: 6 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
require 'coveralls'
Coveralls.wear!

class StubbedResponse
def code
200
end
end

require 'pry'
require 'webmock/rspec'
require 'blanket'

0 comments on commit 7395c2b

Please sign in to comment.