breacan 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e0984b1ef88360f43d44cc08a769c1b65269cfa8
4
- data.tar.gz: f01791668c85b71f6a426a582b9dba47722f76c7
3
+ metadata.gz: 866f53e65dc0fa0ae9c7a169826f9526f1ca7e1d
4
+ data.tar.gz: 2373e47e6cc067ef3066a120d216e6701c365005
5
5
  SHA512:
6
- metadata.gz: ef74fddb9c0d0fadb39263ca232c772cd904055ee5041bd9b1d4d67cd1e49968d89233e37f6de433ec5182a447f974a7ee6c950959c118bbd5273c3b9b1aa916
7
- data.tar.gz: c034707c3abd69a10780c52dc9d77966ce794c3508ad66d9d1f376d35e1bcc1f9122c37ec6a5f7b3151e3ba3c2b7367154eeb3b378d4a128a4b7bf99c855bdd8
6
+ metadata.gz: 9274790635979a54746753d15cb54a51b776a2be37dd9176dad7419971e74d2e5209353b5589a8d7e10b259ccd506d54719e74bccc55f03acdfcca27ac086f0a
7
+ data.tar.gz: 943f4c3d3a5ebb6a2385133ea40e2762b22c650431e19f488f211b1cea834152b40ab41664a8b6995e53c2df6a801fc8ecca5cc9f7f59735b79320f520141908
data/lib/breacan/error.rb CHANGED
@@ -1,26 +1,31 @@
1
1
  module Breacan
2
2
  class Error < StandardError
3
- def self.from_body(body)
4
- if klass = case body[:error]
5
- when 400 then Breacan::BadRequest
6
- when 401 then Breacan::Unauthorized
7
- when 403 then Breacan::Forbidden
8
- when 404 then Breacan::NotFound
9
- when 406 then Breacan::NotAcceptable
10
- when 409 then Breacan::Conflict
11
- when 415 then Breacan::UnsupportedMediaType
12
- when 422 then Breacan::UnprocessableEntity
13
- when 400..499 then Breacan::ClientError
14
- when 500 then Breacan::InternalServerError
15
- when 501 then Breacan::NotImplemented
16
- when 502 then Breacan::BadGateway
17
- when 503 then Breacan::ServiceUnavailable
18
- when 500..599 then Breacan::ServerError
19
- end
3
+ def self.from_response(response)
4
+ status = response[:status].to_i
5
+ if klass = case status
6
+ when 400 then Breacan::BadRequest
7
+ when 401 then Breacan::Unauthorized
8
+ when 403 then Breacan::Forbidden
9
+ when 404 then Breacan::NotFound
10
+ when 406 then Breacan::NotAcceptable
11
+ when 409 then Breacan::Conflict
12
+ when 415 then Breacan::UnsupportedMediaType
13
+ when 422 then Breacan::UnprocessableEntity
14
+ when 400..499 then Breacan::ClientError
15
+ when 500 then Breacan::InternalServerError
16
+ when 501 then Breacan::NotImplemented
17
+ when 502 then Breacan::BadGateway
18
+ when 503 then Breacan::ServiceUnavailable
19
+ when 500..599 then Breacan::ServerError
20
+ end
20
21
  klass.new(response)
21
22
  end
22
23
  end
23
24
 
25
+ def self.from_body(response, body)
26
+ Breacan::BadRequest.new(response) unless body[:ok]
27
+ end
28
+
24
29
  def initialize(response = nil)
25
30
  @response = response
26
31
  super(build_error_message)
@@ -77,7 +82,7 @@ module Breacan
77
82
  end
78
83
 
79
84
  def redact_url(url_string)
80
- %w[client_secret access_token].each do |token|
85
+ %w[token client_secret access_token].each do |token|
81
86
  url_string.gsub!(/#{token}=\S+/, "#{token}=(redacted)") if url_string.include? token
82
87
  end
83
88
  url_string
@@ -1,6 +1,5 @@
1
1
  require 'faraday'
2
2
  require 'breacan/error'
3
- require 'json'
4
3
 
5
4
  module Breacan
6
5
  module Response
@@ -8,16 +7,20 @@ module Breacan
8
7
  private
9
8
 
10
9
  def on_complete(res)
11
- if error = Breacan::Error.from_body(res)
10
+ if error = Breacan::Error.from_response(res)
12
11
  raise error
13
12
  end
14
13
 
15
14
  return if res[:body].empty?
16
15
 
17
- body = JSON.load(res[:body])
18
- if body['ok']
16
+ body = Sawyer::Agent.serializer.decode(res[:body])
17
+ if body[:ok]
19
18
  res[:body].gsub!('"ok":true,', '')
20
19
  end
20
+
21
+ if error = Breacan::Error.from_body(res, body)
22
+ raise error
23
+ end
21
24
  end
22
25
  end
23
26
 
@@ -1,3 +1,3 @@
1
1
  module Breacan
2
- VERSION = '0.3.0'
2
+ VERSION = '0.4.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: breacan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - linyows
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-17 00:00:00.000000000 Z
11
+ date: 2015-12-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sawyer