faraday-conductivity 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- YzFiZDYzNzFhODliZjNjNTQ3YTlmMWE0MzY5YTkzYzFiYTEyOTJmMg==
5
- data.tar.gz: !binary |-
6
- MDMzZTJkODg4ZDEzNjk1OTEzMjkxMDA5YTRlYzQ4Y2FhNGE1N2VkZQ==
7
- !binary "U0hBNTEy":
8
- metadata.gz: !binary |-
9
- NGQ5MzA4NmUzYjZlZWU4OTlkZmNlOGM0ODMyMmNlZjE0ZDk3ZDQ4MzM5Yjk3
10
- MWFjNzE4ZTliYjNmZGNiMDdmYjgyYjUwZmE3YjI3ZDdiNWVhMGM4NzZiZDYz
11
- NzY4OTZhOWE4ZDEwN2VjYzQ0NzliMGU2MzRmYjJjM2ViMTgxZDM=
12
- data.tar.gz: !binary |-
13
- Mzc2YjZjODg1NDNmOWIxODc1MjQ2ODMwZjc4YWNlODRkNWJhNzljMWMxMGI5
14
- MjQ1NTBlZmY3ZWIwODc4YmJiMDNhZDE3MGZjMWE2NWQwZGE4MDQ0ZDUxODRj
15
- YzFkNDQwZGEyYWNmMDAzMWQ4NDljYjFiODllZDRjNGM5YjMzYTI=
2
+ SHA1:
3
+ metadata.gz: c0e0b4ab8e7ebe14c7b3ac66223ac8249e457555
4
+ data.tar.gz: 9de5e1b2b2cb788589612ffdb35b656c538bdda8
5
+ SHA512:
6
+ metadata.gz: 72d31c8c4ccb84c8ed76bbe98db9e94dff8c2e39c412ebac07a305cb16d232c3c233faf070e09de23868ad7b8295bb77afbd30c2dc1220c96da9f4f551980e92
7
+ data.tar.gz: 0b47e2a12d797d83d873c60ef4c0fe45b872a442b40343bc64e9a17ded073ac642e84e470277cf32d2dc465517270254a2beca32f459ae4149a4d6ae4776d371
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Faraday::Conductivity
1
+ # Faraday::Conductivity [![Build Status](https://travis-ci.org/yourkarma/faraday-conductivity.png?branch=master)](https://travis-ci.org/yourkarma/faraday-conductivity)
2
2
 
3
3
  Extra Faraday Middleware! Geared towards a service oriented architecture.
4
4
 
@@ -10,6 +10,7 @@ These middlewares are currently included:
10
10
  * **request_id**, passes along the `X-Request-Id` header to track API request
11
11
  back to the source.
12
12
  * **mimetype**, allows you to specify the `Accept` header for API versioning.
13
+ * **selective_errors**, raise errors only on the statuses that you specify.
13
14
 
14
15
  Further information:
15
16
 
@@ -45,6 +46,9 @@ connection = Faraday.new(url: "http://widgets.yourapp.com") do |faraday|
45
46
  # provided by faraday_middleware
46
47
  faraday.response :json, content_type: /\bjson$/
47
48
 
49
+ # provided by this gem
50
+ faraday.response :selective_errors, on: 425..599, except: 402..499
51
+
48
52
  faraday.adapter Faraday.default_adapter
49
53
 
50
54
  end
@@ -169,6 +173,26 @@ end
169
173
  You can use the repeater together with the `raise_error` middleware to also
170
174
  retry after getting 404s and other succeeded requests, but failed statuses.
171
175
 
176
+ ### Selective Errors
177
+
178
+ The default `:raise_error` middleware raises errors for every http status above
179
+ 400. However, status codes like 404 or 422 might not be an actual exceptional
180
+ condition. This middleware allows you to specify which status codes you do and
181
+ do not want to raise an error for.
182
+
183
+ You can pass in an array or a range to the `:on` argument. This will default to
184
+ 400...600. You can specify exceptions with the `:except` argument.
185
+
186
+ ``` ruby
187
+ connection = Faraday.new(url: "http://widgets.yourapp.com") do |faraday|
188
+ faraday.response :selective_errors, on: (400...600), except: [404, 409, 410, 412, 422]
189
+ end
190
+ ```
191
+
192
+ The errors raised will be the same as Faraday, namely
193
+ `Faraday::Error::ResourceNotFound` for 404 errors,
194
+ `Faraday::Error::ConnectionFailed` for 407 and `Faraday::Error::ClientError` for
195
+ the rest. These all inherit from `Faraday::Error`.
172
196
 
173
197
  ## Contributing
174
198
 
@@ -8,14 +8,16 @@ require "faraday/conductivity/request_id"
8
8
  require "faraday/conductivity/request_id_filter"
9
9
  require "faraday/conductivity/user_agent"
10
10
  require "faraday/conductivity/repeater"
11
+ require "faraday/conductivity/selective_errors"
11
12
 
12
13
  module Faraday
13
14
  module Conductivity
14
15
  end
15
- register_middleware :middleware, :extended_logging => Faraday::Conductivity::ExtendedLogging
16
- register_middleware :request, :user_agent => Faraday::Conductivity::UserAgent
17
- register_middleware :request, :request_id => Faraday::Conductivity::RequestId
18
- register_middleware :request, :mimetype => Faraday::Conductivity::Mimetype
19
- register_middleware :middleware, :repeater => Faraday::Conductivity::Repeater
16
+ register_middleware :middleware, :extended_logging => Faraday::Conductivity::ExtendedLogging
17
+ register_middleware :request, :user_agent => Faraday::Conductivity::UserAgent
18
+ register_middleware :request, :request_id => Faraday::Conductivity::RequestId
19
+ register_middleware :request, :mimetype => Faraday::Conductivity::Mimetype
20
+ register_middleware :middleware, :repeater => Faraday::Conductivity::Repeater
21
+ register_middleware :response, :selective_errors => Faraday::Conductivity::SelectiveErrors
20
22
  end
21
23
 
@@ -0,0 +1,52 @@
1
+ module Faraday
2
+ module Conductivity
3
+ class SelectiveErrors < Faraday::Middleware
4
+
5
+ ClientErrorStatuses = 400...600
6
+
7
+ def initialize(app, options = {})
8
+ @app = app
9
+ @on = options.fetch(:on) { ClientErrorStatuses }
10
+ @except = options.fetch(:except) { [] }
11
+ end
12
+
13
+ def call(env)
14
+ # capture request_body because not accessible afterwards
15
+ request_body = env[:body]
16
+ @app.call(env).on_complete do
17
+ if should_raise_error?(env[:status])
18
+ raise_error(env, request_body)
19
+ end
20
+ end
21
+ end
22
+
23
+ def should_raise_error?(status)
24
+ @on.include?(status) && !@except.include?(status)
25
+ end
26
+
27
+ def raise_error(env, request_body)
28
+ case env[:status]
29
+ when 404
30
+ raise Faraday::Error::ResourceNotFound, response_values(env, request_body)
31
+ when 407
32
+ # mimic the behavior that we get with proxy requests with HTTPS
33
+ raise Faraday::Error::ConnectionFailed, %{407 "Proxy Authentication Required "}
34
+ else
35
+ raise Faraday::Error::ClientError, response_values(env, request_body)
36
+ end
37
+ end
38
+
39
+ def response_values(env, request_body)
40
+ {
41
+ :url => env[:url],
42
+ :status => env[:status],
43
+ :request_body => request_body,
44
+ :request_headers => env[:request_headers],
45
+ :response_headers => env[:response_headers],
46
+ :response_body => env[:body],
47
+ }
48
+ end
49
+
50
+ end
51
+ end
52
+ end
@@ -1,5 +1,5 @@
1
1
  module Faraday
2
2
  module Conductivity
3
- VERSION = "0.1.1"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -5,6 +5,7 @@ describe Faraday::Conductivity::Repeater do
5
5
  let(:connection) {
6
6
  Faraday.new(url: $service_double_url) { |faraday|
7
7
  faraday.use :repeater, mode: :rapid, retries: 6
8
+ faraday.response :raise_error
8
9
  faraday.adapter Faraday.default_adapter
9
10
  }
10
11
  }
@@ -14,16 +15,28 @@ describe Faraday::Conductivity::Repeater do
14
15
  end
15
16
 
16
17
  it "gives up after a number of retries" do
17
- expect { get_with_max(9) }.to raise_error(Faraday::Error::TimeoutError)
18
+ expect { get_with_max(20) }.to raise_error(Faraday::Error::TimeoutError)
19
+ end
20
+
21
+ class MyPattern
22
+
23
+ def initialize
24
+ @waited = []
25
+ end
26
+
27
+ attr_reader :waited
28
+
29
+ def wait(x)
30
+ waited << x
31
+ end
32
+
18
33
  end
19
34
 
20
35
  it "waits according to a pattern" do
21
- pattern = double :pattern
36
+ pattern = MyPattern.new
22
37
  Faraday::Conductivity::Repeater::Pattern.should_receive(:new).and_return(pattern)
23
- pattern.should_receive(:wait).with(1).ordered
24
- pattern.should_receive(:wait).with(2).ordered
25
- pattern.should_receive(:wait).with(3).ordered
26
- get_with_max(3)
38
+ get_with_max(6)
39
+ pattern.waited.should eq pattern.waited.sort
27
40
  end
28
41
 
29
42
  it "handles other errors too" do
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ describe Faraday::Conductivity::SelectiveErrors do
4
+
5
+ subject(:request_headers) { response.env[:request_headers] }
6
+
7
+ it "raises an exception if the error is inside the :on argument" do
8
+ apply_selective_errors on: 407..409
9
+ expect { response_with_status(408) }.to raise_error Faraday::Error::ClientError
10
+ end
11
+
12
+ it "won't raise an exception when outside the range" do
13
+ apply_selective_errors on: 407..409
14
+ expect { response_with_status(410) }.not_to raise_error
15
+ end
16
+
17
+ it "won't raise if included in range, but excluded with :except argument" do
18
+ apply_selective_errors on: 403..422, except: [408]
19
+ expect { response_with_status(408) }.not_to raise_error
20
+ end
21
+
22
+ it "raises an exception if included in :on and not included in :except" do
23
+ apply_selective_errors on: 403..422, except: [408]
24
+ expect { response_with_status(409) }.to raise_error Faraday::Error::ClientError
25
+ end
26
+
27
+ it "raises a resource not found error when the actual status is 404" do
28
+ apply_selective_errors on: 403..422, except: [408]
29
+ expect { response_with_status(404) }.to raise_error Faraday::Error::ResourceNotFound
30
+ end
31
+
32
+ it "raises a connection failed on 407" do
33
+ apply_selective_errors on: 403..422, except: [408]
34
+ expect { response_with_status(407) }.to raise_error Faraday::Error::ConnectionFailed
35
+ end
36
+
37
+ def apply_selective_errors(options)
38
+ @options = options
39
+ end
40
+
41
+ def response_with_status(status)
42
+ stubs = create_stubs do |stub|
43
+ stub.get("/test") { |e| [status, {}, "response"] }
44
+ end
45
+ connection = create_connection(stubs) do |faraday|
46
+ faraday.response :selective_errors, @options
47
+ end
48
+ connection.get("/test")
49
+ end
50
+
51
+ end
@@ -6,10 +6,10 @@ module SpecHelper
6
6
  connection.get("/test")
7
7
  end
8
8
 
9
- def create_connection
9
+ def create_connection(the_stubs = stubs)
10
10
  Faraday.new(url: "http://widgets.example.org") do |faraday|
11
11
  yield faraday
12
- faraday.adapter :test, stubs
12
+ faraday.adapter :test, the_stubs
13
13
  end
14
14
  end
15
15
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday-conductivity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - iain
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-03-28 00:00:00.000000000 Z
11
+ date: 2013-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -28,56 +28,56 @@ dependencies:
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ! '>='
31
+ - - '>='
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ! '>='
38
+ - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ! '>='
45
+ - - '>='
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ! '>='
52
+ - - '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: pry
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ! '>='
59
+ - - '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ! '>='
66
+ - - '>='
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: service_double
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ! '>='
73
+ - - '>='
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ! '>='
80
+ - - '>='
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  description: Extra Faraday middleware, geared towards a service oriented architecture.
@@ -89,6 +89,7 @@ extra_rdoc_files: []
89
89
  files:
90
90
  - .gitignore
91
91
  - .rspec
92
+ - .travis.yml
92
93
  - Gemfile
93
94
  - LICENSE.txt
94
95
  - README.md
@@ -100,6 +101,7 @@ files:
100
101
  - lib/faraday/conductivity/repeater.rb
101
102
  - lib/faraday/conductivity/request_id.rb
102
103
  - lib/faraday/conductivity/request_id_filter.rb
104
+ - lib/faraday/conductivity/selective_errors.rb
103
105
  - lib/faraday/conductivity/user_agent.rb
104
106
  - lib/faraday/conductivity/version.rb
105
107
  - spec/fake_server.rb
@@ -107,6 +109,7 @@ files:
107
109
  - spec/middleware/mimetype_spec.rb
108
110
  - spec/middleware/repeater_spec.rb
109
111
  - spec/middleware/request_id_spec.rb
112
+ - spec/middleware/selective_errors_spec.rb
110
113
  - spec/middleware/user_agent_spec.rb
111
114
  - spec/spec_helper.rb
112
115
  homepage: https://github.com/yourkarma/faraday-conductivity
@@ -119,17 +122,17 @@ require_paths:
119
122
  - lib
120
123
  required_ruby_version: !ruby/object:Gem::Requirement
121
124
  requirements:
122
- - - ! '>='
125
+ - - '>='
123
126
  - !ruby/object:Gem::Version
124
127
  version: '0'
125
128
  required_rubygems_version: !ruby/object:Gem::Requirement
126
129
  requirements:
127
- - - ! '>='
130
+ - - '>='
128
131
  - !ruby/object:Gem::Version
129
132
  version: '0'
130
133
  requirements: []
131
134
  rubyforge_project:
132
- rubygems_version: 2.0.3
135
+ rubygems_version: 2.1.0
133
136
  signing_key:
134
137
  specification_version: 4
135
138
  summary: Extra Faraday middleware, geared towards a service oriented architecture.
@@ -139,5 +142,6 @@ test_files:
139
142
  - spec/middleware/mimetype_spec.rb
140
143
  - spec/middleware/repeater_spec.rb
141
144
  - spec/middleware/request_id_spec.rb
145
+ - spec/middleware/selective_errors_spec.rb
142
146
  - spec/middleware/user_agent_spec.rb
143
147
  - spec/spec_helper.rb