faraday-conductivity 0.2.1 → 0.3.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: 6881981ef0909be7b9068c7b807507a5a06ff9af
4
- data.tar.gz: b4839cd37451314cc0f82049300ec3fe3a3899f1
3
+ metadata.gz: 81337bd02893a9e0365427e2f498fc299c9bfd91
4
+ data.tar.gz: b35b5bf0cf8e97c222b7a08d2425f6ffc47210d7
5
5
  SHA512:
6
- metadata.gz: e0d512fbf02229c25e38d1d8306e3853d07a36ba5a095ed1df5470a31a9c87661a7116b842c49477316eadbc303232fe743d45d91329a60ebcb8081b15d7a4c4
7
- data.tar.gz: 701bc930076a97d3dafe8cc11abc3a37a7f77441b3ba101d755cca1d1f85dc3eea5aac1802f6685689cc6a56cce4a97b632ee93a2d26f31af956696b720d75a8
6
+ metadata.gz: ff06c7a6e23d062a62cf24bc02867c9ec23243afcf284dec10bdf4737d97bb2cbfa23befd52907d4b2e590b41205a24398e78572513da26491f8c47d6027e9ed
7
+ data.tar.gz: 88b20bf120f272d9c2e16908bd2bcbc650befac32e64ba6570d3cc103ac3244af517d2c7cd90d33883c6a697f24a174c5da87bd2ebd437b577de2f55e3510069
data/.gitignore CHANGED
@@ -16,3 +16,4 @@ test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
18
  log/
19
+ gemfiles/*.lock
@@ -2,3 +2,7 @@ language: ruby
2
2
  rvm:
3
3
  - 1.9.3
4
4
  - 2.0.0
5
+ - 2.1.1
6
+ gemfile:
7
+ - gemfiles/faraday_0.8.gemfile
8
+ - gemfiles/faraday_0.9.gemfile
@@ -0,0 +1,7 @@
1
+ appraise "faraday-0.8" do
2
+ gem "faraday", "~> 0.8.0"
3
+ end
4
+
5
+ appraise "faraday-0.9" do
6
+ gem "faraday", "~> 0.9.0"
7
+ end
data/README.md CHANGED
@@ -9,7 +9,7 @@ These middlewares are currently included:
9
9
  * **extended_logging**, logs *all* the information of the request.
10
10
  * **request_id**, passes along the `X-Request-Id` header to track API request
11
11
  back to the source.
12
- * **mimetype**, allows you to specify the `Accept` header for API versioning.
12
+ * **request_headers**, allows you to specify default request headers used in each request.
13
13
  * **selective_errors**, raise errors only on the statuses that you specify.
14
14
 
15
15
  Further information:
@@ -38,7 +38,7 @@ connection = Faraday.new(url: "http://widgets.yourapp.com") do |faraday|
38
38
  # provided by this gem
39
39
  faraday.request :user_agent, app: "MarketingSite", version: APP_VERSION
40
40
  faraday.request :request_id
41
- faraday.request :mimetype, accept: "application/vnd.widgets-v2+json"
41
+ faraday.request :request_headers, accept: "application/vnd.widgets-v2+json"
42
42
 
43
43
  # provided by this gem
44
44
  faraday.use :extended_logging, logger: Rails.logger
@@ -192,7 +192,44 @@ end
192
192
  The errors raised will be the same as Faraday, namely
193
193
  `Faraday::Error::ResourceNotFound` for 404 errors,
194
194
  `Faraday::Error::ConnectionFailed` for 407 and `Faraday::Error::ClientError` for
195
- the rest. These all inherit from `Faraday::Error`.
195
+ the rest.
196
+
197
+ If you don't specify the `:on` or `:except` options, it will behave exactly like
198
+ `:raise_error`. The errors are however "enhanced" with extra information about
199
+ the request that normally are lost:
200
+
201
+ ``` ruby
202
+ begin
203
+ do_failing_request_here
204
+ rescue Faraday::Error::ClientError => error
205
+ puts error.request[:url]
206
+ puts error.request[:method]
207
+ puts error.request[:body]
208
+ puts error.request[:headers]
209
+
210
+ puts error.response[:status]
211
+ puts error.response[:body]
212
+ puts error.response[:headers]
213
+ puts error.response_time
214
+ end
215
+ ```
216
+
217
+ ### Request Headers
218
+
219
+ Allows you to set request headers ahead of time, so you don't have to do this
220
+ each time you make a request. You can override it per request of course.
221
+
222
+ Usage:
223
+
224
+ ``` ruby
225
+ connection = Faraday.new(url: "http://widgets.yourapp.com") do |faraday|
226
+ faraday.request :request_headers, accept: "application/json", x_version_number: "10"
227
+ end
228
+ ```
229
+
230
+ ### Mimetype
231
+
232
+ This one is deprecated. Use Request Headers instead.
196
233
 
197
234
  ## Contributing
198
235
 
data/Rakefile CHANGED
@@ -1,3 +1,4 @@
1
+ require "appraisal"
1
2
  require "bundler/gem_tasks"
2
3
  require 'rspec/core/rake_task'
3
4
  RSpec::Core::RakeTask.new(:spec)
@@ -23,4 +23,5 @@ Gem::Specification.new do |gem|
23
23
  gem.add_development_dependency "rspec"
24
24
  gem.add_development_dependency "pry"
25
25
  gem.add_development_dependency "service_double"
26
+ gem.add_development_dependency "appraisal"
26
27
  end
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "faraday", "~> 0.8.0"
6
+
7
+ gemspec :path=>"../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "faraday", "~> 0.9.0"
6
+
7
+ gemspec :path=>"../"
@@ -9,15 +9,18 @@ require "faraday/conductivity/request_id_filter"
9
9
  require "faraday/conductivity/user_agent"
10
10
  require "faraday/conductivity/repeater"
11
11
  require "faraday/conductivity/selective_errors"
12
+ require "faraday/conductivity/request_headers"
12
13
 
13
14
  module Faraday
14
15
  module Conductivity
15
16
  end
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
17
+
18
+ Faraday::Middleware.register_middleware :extended_logging => Faraday::Conductivity::ExtendedLogging
19
+ Faraday::Middleware.register_middleware :repeater => Faraday::Conductivity::Repeater
20
+ Faraday::Request.register_middleware :mimetype => Faraday::Conductivity::Mimetype
21
+ Faraday::Request.register_middleware :request_id => Faraday::Conductivity::RequestId
22
+ Faraday::Request.register_middleware :request_headers => Faraday::Conductivity::RequestHeaders
23
+ Faraday::Request.register_middleware :user_agent => Faraday::Conductivity::UserAgent
24
+ Faraday::Response.register_middleware :selective_errors => Faraday::Conductivity::SelectiveErrors
22
25
  end
23
26
 
@@ -1,5 +1,6 @@
1
1
  module Faraday
2
2
  module Conductivity
3
+ # Deprecated. Use RequestHeaders
3
4
  class Mimetype < Faraday::Middleware
4
5
 
5
6
  def initialize(app, options = {})
@@ -0,0 +1,19 @@
1
+ module Faraday
2
+ module Conductivity
3
+ class RequestHeaders < Faraday::Middleware
4
+
5
+ def initialize(app, headers)
6
+ super(app)
7
+ @headers = headers
8
+ end
9
+
10
+ def call(env)
11
+ @headers.each do |header, value|
12
+ env[:request_headers][header] ||= value
13
+ end
14
+ @app.call(env)
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -1,5 +1,57 @@
1
1
  module Faraday
2
2
  module Conductivity
3
+
4
+ module Error
5
+
6
+ attr_accessor :request, :response, :response_time
7
+
8
+ def initialize(*)
9
+ @request = {}
10
+ @response = {}
11
+ super
12
+ end
13
+
14
+ def message
15
+ if @wrapped_exception
16
+ "#{@wrapped_exception.class}: #{super}"
17
+ else
18
+ "#{request[:method].to_s.upcase} #{request[:url]} responded with status #{response[:status]}"
19
+ end
20
+ end
21
+
22
+ def inspect
23
+ "<#{self.class}> #{message}"
24
+ end
25
+
26
+ end
27
+
28
+ # Use this to raise errors on certain HTTP statuses. These are basically
29
+ # the same errors as Faraday raises when you use the "raise_error"
30
+ # middleware, but with added fields to better inspect what went wrong.
31
+ #
32
+ # Examples:
33
+ #
34
+ # # specify an array
35
+ # faraday.response :selective_errors, on: [422,500]
36
+ # # or a range:
37
+ # faraday.response :selective_errors, on: 500...600
38
+ # # specify errors:
39
+ # faraday.response :selective_errors, except: [404,422]
40
+ #
41
+ # Rescueing the errors:
42
+ #
43
+ # begin
44
+ # do_request
45
+ # rescue Faraday::Conductivity::Error => error
46
+ # puts error.request[:url]
47
+ # puts error.request[:method]
48
+ # puts error.request[:body]
49
+ # puts error.request[:headers]
50
+ #
51
+ # puts error.response[:status]
52
+ # puts error.response[:body]
53
+ # puts error.response[:headers]
54
+ # end
3
55
  class SelectiveErrors < Faraday::Middleware
4
56
 
5
57
  ClientErrorStatuses = 400...600
@@ -11,12 +63,43 @@ module Faraday
11
63
  end
12
64
 
13
65
  def call(env)
14
- # capture request_body because not accessible afterwards
15
- request_body = env[:body]
66
+ # capture request because it will be modified during the request
67
+ request = {
68
+ :method => env[:method],
69
+ :url => env[:url],
70
+ :body => env[:body],
71
+ :headers => env[:request_headers],
72
+ }
73
+
74
+ start_time = Time.now
75
+
16
76
  @app.call(env).on_complete do
17
- if should_raise_error?(env[:status])
18
- raise_error(env, request_body)
77
+
78
+ status = env[:status]
79
+
80
+ if should_raise_error?(status)
81
+ response = {
82
+ :status => env[:status],
83
+ :body => env[:body],
84
+ :headers => env[:response_headers],
85
+ }
86
+ error = case status
87
+ when 404
88
+ Faraday::Error::ResourceNotFound.new(response)
89
+ when 407
90
+ # mimic the behavior that we get with proxy requests with HTTPS
91
+ Faraday::Error::ConnectionFailed.new(%{407 "Proxy Authentication Required "})
92
+ else
93
+ Faraday::Error::ClientError.new(response)
94
+ end
95
+ error.extend Error
96
+ error.response = response
97
+ error.request = request
98
+ error.response_time = Time.now - start_time
99
+ raise error
100
+
19
101
  end
102
+
20
103
  end
21
104
  end
22
105
 
@@ -24,29 +107,6 @@ module Faraday
24
107
  @on.include?(status) && !@except.include?(status)
25
108
  end
26
109
 
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
110
  end
51
111
  end
52
112
  end
@@ -1,5 +1,5 @@
1
1
  module Faraday
2
2
  module Conductivity
3
- VERSION = "0.2.1"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -0,0 +1,28 @@
1
+ require "spec_helper"
2
+
3
+ describe Faraday::Conductivity::RequestHeaders do
4
+
5
+ it "includes the mimetype specified" do
6
+ connection = create_connection do |faraday|
7
+ faraday.request :request_headers, :accept => "application/json", :x_version_number => "123"
8
+ end
9
+ request_headers = connection.get("/test").env[:request_headers]
10
+
11
+ request_headers["Accept"].should eq "application/json"
12
+ request_headers["X-Version-Number"].should eq "123"
13
+ end
14
+
15
+ it "doesn't override locally specified headers" do
16
+ connection = create_connection do |faraday|
17
+ faraday.request :request_headers, :accept => "application/json"
18
+ end
19
+ response = connection.get("/test") do |request|
20
+ request.headers[:accept] = "application/xml"
21
+ end
22
+
23
+ request_headers = response.env[:request_headers]
24
+
25
+ request_headers["Accept"].should eq "application/xml"
26
+ end
27
+
28
+ end
@@ -34,18 +34,39 @@ describe Faraday::Conductivity::SelectiveErrors do
34
34
  expect { response_with_status(407) }.to raise_error Faraday::Error::ConnectionFailed
35
35
  end
36
36
 
37
+ it "stores more information about the request and response" do
38
+ apply_selective_errors on: 403..422, except: [408]
39
+ error = response_with_status(422) rescue $!
40
+ expect(error.message).to eq "GET http://widgets.example.org/test responded with status 422"
41
+
42
+ expect(error.request[:url].to_s).to eq "http://widgets.example.org/test"
43
+ expect(error.request[:method]).to eq :get
44
+
45
+ expect(error.response[:status]).to eq 422
46
+
47
+ expect(error.request[:body]).to eq "the request body"
48
+ expect(error.response[:body]).to eq "the response body"
49
+
50
+ expect(error.request[:headers]).to eq "Accept" => "application/json"
51
+ expect(error.response[:headers]).to eq "X-Foo-Bar" => "y"
52
+ expect(error.response_time).to be_a Float
53
+ end
54
+
37
55
  def apply_selective_errors(options)
38
56
  @options = options
39
57
  end
40
58
 
41
59
  def response_with_status(status)
42
60
  stubs = create_stubs do |stub|
43
- stub.get("/test") { |e| [status, {}, "response"] }
61
+ stub.get("/test") { |e| [status, { :x_foo_bar => "y" }, "the response body"] }
44
62
  end
45
63
  connection = create_connection(stubs) do |faraday|
46
64
  faraday.response :selective_errors, @options
47
65
  end
48
- connection.get("/test")
66
+ connection.get("/test") do |f|
67
+ f.body = "the request body"
68
+ f.headers = { "Accept" => "application/json" }
69
+ end
49
70
  end
50
71
 
51
72
  end
metadata CHANGED
@@ -1,83 +1,97 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday-conductivity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.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-10-22 00:00:00.000000000 Z
11
+ date: 2014-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0.8'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0.8'
27
27
  - !ruby/object:Gem::Dependency
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
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: appraisal
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
81
95
  - !ruby/object:Gem::Version
82
96
  version: '0'
83
97
  description: Extra Faraday middleware, geared towards a service oriented architecture.
@@ -87,18 +101,22 @@ executables: []
87
101
  extensions: []
88
102
  extra_rdoc_files: []
89
103
  files:
90
- - .gitignore
91
- - .rspec
92
- - .travis.yml
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Appraisals
93
108
  - Gemfile
94
109
  - LICENSE.txt
95
110
  - README.md
96
111
  - Rakefile
97
112
  - faraday-conductivity.gemspec
113
+ - gemfiles/faraday_0.8.gemfile
114
+ - gemfiles/faraday_0.9.gemfile
98
115
  - lib/faraday/conductivity.rb
99
116
  - lib/faraday/conductivity/extended_logging.rb
100
117
  - lib/faraday/conductivity/mimetype.rb
101
118
  - lib/faraday/conductivity/repeater.rb
119
+ - lib/faraday/conductivity/request_headers.rb
102
120
  - lib/faraday/conductivity/request_id.rb
103
121
  - lib/faraday/conductivity/request_id_filter.rb
104
122
  - lib/faraday/conductivity/selective_errors.rb
@@ -108,6 +126,7 @@ files:
108
126
  - spec/middleware/extended_logging_spec.rb
109
127
  - spec/middleware/mimetype_spec.rb
110
128
  - spec/middleware/repeater_spec.rb
129
+ - spec/middleware/request_headers_spec.rb
111
130
  - spec/middleware/request_id_spec.rb
112
131
  - spec/middleware/selective_errors_spec.rb
113
132
  - spec/middleware/user_agent_spec.rb
@@ -122,17 +141,17 @@ require_paths:
122
141
  - lib
123
142
  required_ruby_version: !ruby/object:Gem::Requirement
124
143
  requirements:
125
- - - '>='
144
+ - - ">="
126
145
  - !ruby/object:Gem::Version
127
146
  version: '0'
128
147
  required_rubygems_version: !ruby/object:Gem::Requirement
129
148
  requirements:
130
- - - '>='
149
+ - - ">="
131
150
  - !ruby/object:Gem::Version
132
151
  version: '0'
133
152
  requirements: []
134
153
  rubyforge_project:
135
- rubygems_version: 2.1.0
154
+ rubygems_version: 2.2.2
136
155
  signing_key:
137
156
  specification_version: 4
138
157
  summary: Extra Faraday middleware, geared towards a service oriented architecture.
@@ -141,6 +160,7 @@ test_files:
141
160
  - spec/middleware/extended_logging_spec.rb
142
161
  - spec/middleware/mimetype_spec.rb
143
162
  - spec/middleware/repeater_spec.rb
163
+ - spec/middleware/request_headers_spec.rb
144
164
  - spec/middleware/request_id_spec.rb
145
165
  - spec/middleware/selective_errors_spec.rb
146
166
  - spec/middleware/user_agent_spec.rb