Skip to content
Browse files

Handle unsuccessful responses in Request.get.

  • Loading branch information...
1 parent fba1747 commit d4c9e9e663a9baf2f5d2b19e8c73c52760abe2dc @tristandunn tristandunn committed May 28, 2011
Showing with 15 additions and 3 deletions.
  1. +8 −1 lib/bartender/request.rb
  2. +7 −2 spec/bartender/request_spec.rb
View
9 lib/bartender/request.rb
@@ -18,7 +18,14 @@ def self.get(path, options = {})
options[:token] ||= token
end
- JSON.parse(request.get(uri(path, options)).body)
+ response = request.get(uri(path, options))
+
+ case response.code.to_i
+ when 200
+ JSON.parse(response.body)
+ else
+ false
+ end
end
def self.post(path, attributes = {}, options = {})
View
9 spec/bartender/request_spec.rb
@@ -38,7 +38,7 @@
describe Bartender::Request, ".get" do
let(:request) { stub("Request", :get => response) }
- let(:response) { stub("Response", :body => "{}") }
+ let(:response) { stub("Response", :body => "{}", :code => 200) }
let(:public_token) { "a1b2c3" }
before do
@@ -68,9 +68,14 @@
request.should have_received(:get).with("/")
end
- it "returns the parsed JSON response body" do
+ it "returns the parsed JSON response body for a successful response" do
Bartender::Request.get("/").should == {}
end
+
+ it "returns false for all other responses" do
+ response.stubs(:code => "401")
+ Bartender::Request.get("/").should == false
+ end
end
describe Bartender::Request, ".post" do

0 comments on commit d4c9e9e

Please sign in to comment.
Something went wrong with that request. Please try again.