geoloqi 0.9.27 → 0.9.28

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  module Geoloqi
2
2
  class Config
3
- attr_accessor :client_id, :client_secret, :redirect_uri, :adapter, :logger, :use_hashie_mash, :throw_exceptions
3
+ attr_accessor :client_id, :client_secret, :redirect_uri, :adapter, :logger, :use_hashie_mash, :throw_exceptions, :use_dynamic_exceptions
4
4
  def initialize(opts={})
5
5
  self.use_hashie_mash ||= false
6
6
  self.throw_exceptions ||= true
@@ -45,7 +45,17 @@ module Geoloqi
45
45
  begin
46
46
  response = execute meth, path, query, headers
47
47
  hash = JSON.parse response.body
48
- raise ApiError.new(response.status, hash['error'], hash['error_description']) if hash.is_a?(Hash) && hash['error'] && @config.throw_exceptions
48
+
49
+ if hash.is_a?(Hash) && hash['error'] && @config.throw_exceptions
50
+ if @config.use_dynamic_exceptions && !hash['error'].nil? && !hash['error'].empty?
51
+ exception_class_name = hash['error'].split('_').collect {|w| w.capitalize}.join+'Error'
52
+ Geoloqi.const_set exception_class_name, Class.new(Geoloqi::ApiError) unless Geoloqi.const_defined? exception_class_name
53
+ raise_class = Geoloqi.const_get exception_class_name
54
+ else
55
+ raise_class = ApiError
56
+ end
57
+ raise raise_class.new(response.status, hash['error'], hash['error_description'])
58
+ end
49
59
  rescue Geoloqi::ApiError
50
60
  raise Error.new('Unable to procure fresh access token from API on second attempt') if retry_attempt > 0
51
61
  if hash['error'] == 'expired_token' && !(hash['error_description'] =~ /The auth code expired/)
@@ -1,5 +1,5 @@
1
1
  module Geoloqi
2
2
  def self.version
3
- '0.9.27'
3
+ '0.9.28'
4
4
  end
5
5
  end
@@ -21,6 +21,28 @@ describe Geoloqi::Session do
21
21
  end
22
22
  end
23
23
 
24
+ describe 'custom exceptions scheme' do
25
+ before do
26
+ @session = Geoloqi::Session.new :access_token => 'access_token1234', :config => {:use_dynamic_exceptions => true}
27
+ end
28
+
29
+ it 'should throw api error exception with custom name' do
30
+ stub_request(:get, api_url('specialerror')).
31
+ with(:headers => auth_headers).
32
+ to_return(:status => 404, :body => {'error' => 'not_found'}.to_json)
33
+
34
+ expect { rescuing {@session.get('specialerror')}.class == Geoloqi::NotFoundError }
35
+ end
36
+
37
+ it 'should throw api error exception without custom name if empty' do
38
+ stub_request(:get, api_url('specialerror')).
39
+ with(:headers => auth_headers).
40
+ to_return(:status => 404, :body => {'error' => ''}.to_json)
41
+
42
+ expect { rescuing {@session.get('specialerror')}.class == Geoloqi::ApiError }
43
+ end
44
+ end
45
+
24
46
  describe 'with access token and throw exceptions false' do
25
47
  before do
26
48
  @session = Geoloqi::Session.new :access_token => 'access_token1234', :config => {:throw_exceptions => false}
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: geoloqi
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.9.27
5
+ version: 0.9.28
6
6
  platform: ruby
7
7
  authors:
8
8
  - Kyle Drake
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2011-08-29 00:00:00 -07:00
14
+ date: 2011-10-06 00:00:00 -07:00
15
15
  default_executable:
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency