Skip to content

Commit

Permalink
Error handling on requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevintuhumury committed Mar 25, 2012
1 parent 48d94d3 commit 9434b7a
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 18 deletions.
3 changes: 2 additions & 1 deletion lib/cameraplus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
require "cameraplus/api/page"

require "cameraplus/user"
require "cameraplus/page_metadata"
require "cameraplus/page"
require "cameraplus/page_metadata"
require "cameraplus/photo"
require "cameraplus/photo_exif"
require "cameraplus/photo_recipe"
require "cameraplus/comment"
require "cameraplus/exceptions"
require "cameraplus/version"
26 changes: 20 additions & 6 deletions lib/cameraplus/api/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,39 @@ class Request

base_uri "http://camerapl.us"

format :json

def self.call(url, options = {})
new(url, options).response
new(url, options).parsed_response
end

def initialize(url, options = {})
@url = url
@options = options
end

def response
request.parsed_response
def parsed_response
if code == 200
@response.parsed_response
else
raise InvalidResponseError, message
end
end

private

def request
self.class.get @url, query: @options
@response ||= self.class.get @url, query: @options
end

def response
request.response
end

def code
response.code.to_i
end

def message
"#{@response.message} (#{@response.code}): #{@response.body}\n#{@response.inspect}"
end

end
Expand Down
7 changes: 7 additions & 0 deletions lib/cameraplus/exceptions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Cameraplus
class InvalidArgumentError < StandardError
end

class InvalidResponseError < StandardError
end
end
18 changes: 13 additions & 5 deletions spec/cameraplus/api/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,11 @@
subject.base_uri.should eq "http://camerapl.us"
end

it "should know it's response format" do
subject.format.should eq :json
end

end

context "connecting with the api" do

use_vcr_cassette :request_api
use_vcr_cassette :api_request_valid

let(:response) { subject.call "/user/mostlylisa:pages" }

Expand All @@ -36,4 +32,16 @@

end

context "connecting with an invalid api call" do

use_vcr_cassette :api_request_invalid

let(:response) { subject.call "/non-existing-page" }

it "should raise an error" do
expect { response }.to raise_error Cameraplus::InvalidResponseError
end

end

end
56 changes: 56 additions & 0 deletions spec/vcr_cassettes/api_request_invalid.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9434b7a

Please sign in to comment.