Skip to content
Browse files

Error handling on requests.

  • Loading branch information...
1 parent 48d94d3 commit 9434b7ad70d41f875cd0307bf519ea49f79b6e8d @kevintuhumury committed
View
3 lib/cameraplus.rb
@@ -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"
View
26 lib/cameraplus/api/request.rb
@@ -5,10 +5,8 @@ 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 = {})
@@ -16,14 +14,30 @@ def initialize(url, options = {})
@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
View
7 lib/cameraplus/exceptions.rb
@@ -0,0 +1,7 @@
+module Cameraplus
+ class InvalidArgumentError < StandardError
+ end
+
+ class InvalidResponseError < StandardError
+ end
+end
View
18 spec/cameraplus/api/request_spec.rb
@@ -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" }
@@ -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
View
56 spec/vcr_cassettes/api_request_invalid.yml
@@ -0,0 +1,56 @@
+---
+http_interactions:
+- request:
+ method: get
+ uri: http://camerapl.us/non-existing-page?
+ body:
+ encoding: US-ASCII
+ string: ''
+ headers: {}
+ response:
+ status:
+ code: 404
+ message: Not Found
+ headers:
+ Cache-Control:
+ - no-store, no-cache, must-revalidate, post-check=0, pre-check=0
+ Content-Type:
+ - text/html; charset=UTF-8
+ Date:
+ - Sun, 25 Mar 2012 16:47:40 GMT
+ Expires:
+ - Thu, 19 Nov 1981 08:52:00 GMT
+ Pragma:
+ - no-cache
+ Server:
+ - Apache/2.2.21 (Amazon)
+ Set-Cookie:
+ - PHPSESSID=7v53q5k9m16h8qrghjdp5mj1g3; expires=Fri, 21-Sep-2012 16:47:40 GMT;
+ path=/; domain=.campl.us
+ - PHPSESSID=7v53q5k9m16h8qrghjdp5mj1g3; expires=Fri, 21-Sep-2012 16:47:40 GMT;
+ path=/; domain=.campl.us
+ X-Powered-By:
+ - PHP/5.3.10
+ Content-Length:
+ - '956'
+ Connection:
+ - keep-alive
+ body:
+ encoding: US-ASCII
+ string: ! "<!DOCTYPE html> \n<html lang=\"en\"> \n<head> \n\t<meta charset=\"UTF-8\"
+ /> \n\t<title>Camera+ ~ Error</title> \n\t<meta name = \"viewport\" content
+ = \"width = device-width\"> \n\t<style type=\"text/css\">@import url('/static/error.css?14');
+ </style> \n<head> \n<body class=\"error\"> \n\t<div class=\"container\"> \n\t\t<a
+ href=\"/\"><div class=\"findOutMore\"></div><h1>Camera+</h1></a> \n \n\t\t<div
+ class=\"box1\"> \n\t\t<div class=\"box2\"> \n\t\t<div id=\"contentArea\">
+ \n\t\t<div class=\"box3\"> \n\t\t\n\t\t\t<div class=\"errorbox\">\n\t\t\t\t<div
+ class=\"icon error404page\"></div>\n\t\t\t\t<div class=\"message\">Page not
+ found</div>\n\t\t\t\t<div class=\"message2\">Sorry, the requested page could
+ not be found</div>\n\t\t\t</div>\n\t\t\t<div class=\"login\"></div>\n\t\t\n\t\t</div>
+ \n\t\t</div>\t\n\t\t</div>\t\n\t\t</div> \n\t</div> \n\t\n\t<script type=\"text/javascript\"
+ src=\"http://include.reinvigorate.net/re_.js\"></script> \n\t<script type=\"text/javascript\">
+ \n\ttry {\n\t\treinvigorate.track(\"995iv-68r19xp73x\");\n\t} catch(err) {}\n\t</script>
+ \n</body> \n</html> \n"
+ http_version:
+ recorded_at: Sun, 25 Mar 2012 16:47:40 GMT
+recorded_with: VCR 2.0.0
View
17 spec/vcr_cassettes/request_api.yml → spec/vcr_cassettes/api_request_valid.yml
@@ -17,7 +17,7 @@ http_interactions:
Content-Type:
- application/json
Date:
- - Fri, 16 Mar 2012 22:06:28 GMT
+ - Sun, 25 Mar 2012 16:47:39 GMT
Expires:
- Thu, 19 Nov 1981 08:52:00 GMT
Pragma:
@@ -25,9 +25,9 @@ http_interactions:
Server:
- Apache/2.2.21 (Amazon)
Set-Cookie:
- - PHPSESSID=oggop8sa3kshmj1d16cocibc37; expires=Wed, 12-Sep-2012 22:06:28 GMT;
+ - PHPSESSID=pt07keqsoc5jhhc7tv79eqpg42; expires=Fri, 21-Sep-2012 16:47:39 GMT;
path=/; domain=.campl.us
- - PHPSESSID=oggop8sa3kshmj1d16cocibc37; expires=Wed, 12-Sep-2012 22:06:28 GMT;
+ - PHPSESSID=pt07keqsoc5jhhc7tv79eqpg42; expires=Fri, 21-Sep-2012 16:47:39 GMT;
path=/; domain=.campl.us
X-Powered-By:
- PHP/5.3.10
@@ -38,9 +38,14 @@ http_interactions:
body:
encoding: US-ASCII
string: ! '{"user":{"userid":6978642,"username":"mostlylisa","realname":"Lisa
- Bettany","avatar":"http:\/\/a0.twimg.com\/profile_images\/1767370289\/284161_10150711631895637_674215636_19658534_6246798_n_normal.jpeg","pages":120,"pictures":874},"pages":[{"url":"http:\/\/campl.us\/iao4","tweettext":"Carnival
+ Bettany","avatar":"http:\/\/a0.twimg.com\/profile_images\/1767370289\/284161_10150711631895637_674215636_19658534_6246798_n_normal.jpeg","pages":122,"pictures":876},"pages":[{"url":"http:\/\/campl.us\/iiQb","tweettext":"@bbcrenshaw
+ On average I take 5-15 shots of each scene to make sure my composition & focus
+ are perfect. I took 10 here.","tweetid":"182208844892606464","timestamp":"2012-03-20
+ 20:55:48","views":167,"comments":0,"location":null,"locationname":null,"images":[{"800px":"http:\/\/pics.campl.us\/f\/d\/de7421b027101574dc519796067bd3ac.jpg","120px":"http:\/\/pics.campl.us\/t\/d\/de7421b027101574dc519796067bd3ac.jpg","480px":"http:\/\/pics.campl.us\/iphone\/d\/de7421b027101574dc519796067bd3ac.jpg","fullwidth":533,"fullheight":800,"location":""}]},{"url":"http:\/\/campl.us\/iiPj","tweettext":"Back
+ in the Big Apple!","tweetid":"182204831094673408","timestamp":"2012-03-20
+ 20:39:51","views":1408,"comments":2,"location":null,"locationname":null,"images":[{"800px":"http:\/\/pics.campl.us\/f\/1\/15e03b8d68880348c0542e4944d14641.jpg","120px":"http:\/\/pics.campl.us\/t\/1\/15e03b8d68880348c0542e4944d14641.jpg","480px":"http:\/\/pics.campl.us\/iphone\/1\/15e03b8d68880348c0542e4944d14641.jpg","fullwidth":600,"fullheight":800,"location":""}]},{"url":"http:\/\/campl.us\/iao4","tweettext":"Carnival
in Venice, a travel photographer''s dream shoot!","tweetid":"180064216093437954","timestamp":"2012-03-14
- 22:53:49","views":1888,"comments":1,"location":"45.43383498680353 12.34226474539595","locationname":"Venice,
+ 22:53:49","views":2428,"comments":1,"location":"45.43383498680353 12.34226474539595","locationname":"Venice,
Venice","images":[{"800px":"http:\/\/pics.campl.us\/f\/0\/0b497f01791c851db1a17f81e0621a5c.jpg","120px":"http:\/\/pics.campl.us\/t\/0\/0b497f01791c851db1a17f81e0621a5c.jpg","480px":"http:\/\/pics.campl.us\/iphone\/0\/0b497f01791c851db1a17f81e0621a5c.jpg","fullwidth":800,"fullheight":590,"location":"45.43383498680353
12.34226474539595"},{"800px":"http:\/\/pics.campl.us\/f\/a\/a2fb3bfe849f16f5420f4ca8348f33a0.jpg","120px":"http:\/\/pics.campl.us\/t\/a\/a2fb3bfe849f16f5420f4ca8348f33a0.jpg","480px":"http:\/\/pics.campl.us\/iphone\/a\/a2fb3bfe849f16f5420f4ca8348f33a0.jpg","fullwidth":800,"fullheight":601,"location":"45.43305496689438
12.33965336346285"},{"800px":"http:\/\/pics.campl.us\/f\/e\/e40e677a67af931391d1b0c2fdebc6f9.jpg","120px":"http:\/\/pics.campl.us\/t\/e\/e40e677a67af931391d1b0c2fdebc6f9.jpg","480px":"http:\/\/pics.campl.us\/iphone\/e\/e40e677a67af931391d1b0c2fdebc6f9.jpg","fullwidth":800,"fullheight":600,"location":"45.43375707701355
@@ -69,5 +74,5 @@ http_interactions:
12.34029692598837"},{"800px":"http:\/\/pics.campl.us\/f\/e\/e8170f8e597d23a45eafba6be863607c.jpg","120px":"http:\/\/pics.campl.us\/t\/e\/e8170f8e597d23a45eafba6be863607c.jpg","480px":"http:\/\/pics.campl.us\/iphone\/e\/e8170f8e597d23a45eafba6be863607c.jpg","fullwidth":655,"fullheight":800,"location":""},{"800px":"http:\/\/pics.campl.us\/f\/c\/c783e4c9dd68ec7dc2cc6aa390c2df81.jpg","120px":"http:\/\/pics.campl.us\/t\/c\/c783e4c9dd68ec7dc2cc6aa390c2df81.jpg","480px":"http:\/\/pics.campl.us\/iphone\/c\/c783e4c9dd68ec7dc2cc6aa390c2df81.jpg","fullwidth":600,"fullheight":800,"location":"45.45624578150769
12.35432020530416"}]}],"next":"http:\/\/camerapl.us\/user\/mostlylisa:pages?continue=iao4"}'
http_version:
- recorded_at: Fri, 16 Mar 2012 22:06:30 GMT
+ recorded_at: Sun, 25 Mar 2012 16:47:39 GMT
recorded_with: VCR 2.0.0

0 comments on commit 9434b7a

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