reviewed 0.1.30 → 0.1.31

Sign up to get free protection for your applications and to get access to all the features.
@@ -58,10 +58,26 @@ module Reviewed
58
58
  private
59
59
 
60
60
  def perform(method, path, params={})
61
- self.connection.send(method.to_sym, path, params) do |request|
62
- request.params.merge!(self.request_params)
63
- request.headers['X-Reviewed-Authorization'] ||= self.api_key
61
+ begin
62
+ response = self.connection.send(method.to_sym, path, params) do |request|
63
+ request.params.merge!(self.request_params)
64
+ request.headers['X-Reviewed-Authorization'] ||= self.api_key
65
+ end
66
+ rescue => e
67
+ if e.class < Faraday::Error::ClientError
68
+ message = <<-EOS.gsub(/^[ ]*/, '')
69
+ API Error. method: #{method}. path: #{path}. params: #{params.to_s}. api_key: #{self.api_key}
70
+ Original exception message:
71
+ #{e.message}
72
+ EOS
73
+ new_exception = Reviewed::ApiError.new(msg: message)
74
+ new_exception.set_backtrace(e.backtrace)
75
+ raise new_exception
76
+ else
77
+ raise
78
+ end
64
79
  end
80
+ response
65
81
  end
66
82
  end
67
83
  end
@@ -1,4 +1,4 @@
1
1
  module Reviewed
2
- VERSION = "0.1.30"
2
+ VERSION = "0.1.31"
3
3
  API_VERSION = 'v1'
4
4
  end
data/lib/reviewed.rb CHANGED
@@ -30,12 +30,13 @@ module Reviewed
30
30
  class BaseError < StandardError
31
31
  attr_accessor :url
32
32
 
33
- def initialize(opts)
33
+ def initialize(opts={})
34
34
  @url = opts[:url]
35
35
  super(opts[:msg])
36
36
  end
37
37
  end
38
38
  class ConfigurationError < Reviewed::BaseError; end
39
39
  class ResourceNotFound < Reviewed::BaseError; end
40
+ class ApiError < Reviewed::BaseError; end
40
41
  end
41
42
 
@@ -7,12 +7,22 @@ describe Faraday::Errors do
7
7
  stub.get('/products/123') {[ 404, {}, { message: 'Record Not Found' } ]}
8
8
  end
9
9
 
10
+ stubs2 = Faraday::Adapter::Test::Stubs.new do |stub|
11
+ stub.get('/products/123') {[ 500, {}, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"> <style type="text/css"> html, body, iframe { margin: 0; padding: 0; height: 100%; } iframe { display: block; width: 100%; border: none; } </style> <title>Application Error</title></head> </head> <body> <iframe src="//s3.amazonaws.com/heroku_pages/error.html"> <p>Application Error</p> </iframe> </body> </html>' ]}
12
+ end
13
+
10
14
  test = Faraday.new do |builder|
11
15
  builder.response :errors
12
16
  builder.response :json
13
17
  builder.adapter :test, stubs
14
18
  end
15
19
 
20
+ test2 = Faraday.new do |builder|
21
+ builder.response :errors
22
+ builder.response :json
23
+ builder.adapter :test, stubs2
24
+ end
25
+
16
26
  it 'should raise a ResourceNotFound error' do
17
27
  expect {
18
28
  test.get('/products/123')
@@ -22,6 +32,25 @@ describe Faraday::Errors do
22
32
  e.message.should eql('Not Found')
23
33
  }
24
34
  end
35
+
36
+ context 'faraday catch-all' do
37
+
38
+ it 'raises an ApiError error' do
39
+ client = Reviewed::Client.new
40
+ client.stub!(:connection).and_return(test2)
41
+ expect {
42
+ client.send(:perform, :get, '/products/123')
43
+ }.to raise_error(Reviewed::ApiError)
44
+ end
45
+
46
+ it 'passes other Reviewed errors through' do
47
+ client = Reviewed::Client.new
48
+ client.stub!(:connection).and_raise(Reviewed::ConfigurationError.new)
49
+ expect {
50
+ client.send(:perform, :get, '/products/123')
51
+ }.to raise_error(Reviewed::ConfigurationError)
52
+ end
53
+ end
25
54
  end
26
55
 
27
56
  describe 'no error' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reviewed
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.30
4
+ version: 0.1.31
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-01-15 00:00:00.000000000 Z
13
+ date: 2013-02-22 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -296,7 +296,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
296
296
  version: '0'
297
297
  requirements: []
298
298
  rubyforge_project:
299
- rubygems_version: 1.8.24
299
+ rubygems_version: 1.8.23
300
300
  signing_key:
301
301
  specification_version: 3
302
302
  summary: A Ruby Gem for Accessing the Reviewed.com API