faraday_middleware 0.9.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,7 +7,7 @@ describe FaradayMiddleware::EncodeJson do
7
7
  def process(body, content_type = nil)
8
8
  env = {:body => body, :request_headers => Faraday::Utils::Headers.new}
9
9
  env[:request_headers]['content-type'] = content_type if content_type
10
- middleware.call(env)
10
+ middleware.call(faraday_env(env))
11
11
  end
12
12
 
13
13
  def result_body() result[:body] end
@@ -200,6 +200,9 @@ describe FaradayMiddleware::FollowRedirects do
200
200
  if env[:status] or env[:response] or env[:response_headers]
201
201
  raise "invalid request: #{env.inspect}"
202
202
  end
203
+ if defined?(Faraday::Env) && !env.is_a?(Faraday::Env)
204
+ raise "expected Faraday::Env, got #{env.class}"
205
+ end
203
206
  app.call(env)
204
207
  end
205
208
  end
@@ -5,7 +5,7 @@ if ENV['COVERAGE']
5
5
  def format(result)
6
6
  SimpleCov::Formatter::HTMLFormatter.new.format(result) unless ENV['CI']
7
7
  File.open('coverage/covered_percent', 'w') do |f|
8
- f.puts result.source_files.covered_percent.to_i
8
+ f.printf "%.2f", result.source_files.covered_percent
9
9
  end
10
10
  end
11
11
  end
@@ -17,6 +17,17 @@ if ENV['COVERAGE']
17
17
  end
18
18
 
19
19
  require 'rspec'
20
+ require 'faraday'
21
+
22
+ module EnvCompatibility
23
+ def faraday_env(env)
24
+ if defined?(Faraday::Env)
25
+ Faraday::Env.from(env)
26
+ else
27
+ env
28
+ end
29
+ end
30
+ end
20
31
 
21
32
  module ResponseMiddlewareExampleGroup
22
33
  def self.included(base)
@@ -35,11 +46,12 @@ module ResponseMiddlewareExampleGroup
35
46
  :response_headers => Faraday::Utils::Headers.new(headers)
36
47
  }
37
48
  env[:response_headers]['content-type'] = content_type if content_type
38
- middleware.call(env)
49
+ middleware.call(faraday_env(env))
39
50
  end
40
51
  end
41
52
 
42
53
  RSpec.configure do |config|
54
+ config.include EnvCompatibility
43
55
  config.include ResponseMiddlewareExampleGroup, :type => :response
44
56
  config.expect_with :rspec do |c|
45
57
  c.syntax = :expect
@@ -2,98 +2,69 @@ require 'helper'
2
2
  require 'faraday_middleware/response/mashify'
3
3
 
4
4
  describe FaradayMiddleware::Mashify do
5
- context "during configuration" do
6
- it "allows for a custom Mash class to be set" do
7
- expect(described_class).to respond_to(:mash_class)
8
- expect(described_class).to respond_to(:mash_class=)
9
- end
10
- end
11
-
12
- context "when used" do
13
- before(:each) { described_class.mash_class = ::Hashie::Mash }
14
- let(:mashify) { described_class.new }
5
+ MyMash = Struct.new(:body)
15
6
 
7
+ context "when used", :type => :response do
16
8
  it "creates a Hashie::Mash from the body" do
17
- env = { :body => { "name" => "Erik Michaels-Ober", "username" => "sferik" } }
18
- me = mashify.on_complete(env)
19
- expect(me.class).to eq(Hashie::Mash)
9
+ body = { "name" => "Erik Michaels-Ober", "username" => "sferik" }
10
+ me = process(body).body
11
+ expect(me.name).to eq("Erik Michaels-Ober")
12
+ expect(me.username).to eq("sferik")
20
13
  end
21
14
 
22
15
  it "handles strings" do
23
- env = { :body => "Most amazing string EVER" }
24
- me = mashify.on_complete(env)
16
+ body = "Most amazing string EVER"
17
+ me = process(body).body
25
18
  expect(me).to eq("Most amazing string EVER")
26
19
  end
27
20
 
28
21
  it "handles arrays" do
29
- env = { :body => [123, 456] }
30
- values = mashify.on_complete(env)
31
- expect(values.first).to eq(123)
32
- expect(values.last).to eq(456)
22
+ body = [123, 456]
23
+ values = process(body).body
24
+ expect(values).to eq([123, 456])
33
25
  end
34
26
 
35
27
  it "handles arrays of hashes" do
36
- env = { :body => [{ "username" => "sferik" }, { "username" => "pengwynn" }] }
37
- us = mashify.on_complete(env)
28
+ body = [{ "username" => "sferik" }, { "username" => "pengwynn" }]
29
+ us = process(body).body
38
30
  expect(us.first.username).to eq('sferik')
39
31
  expect(us.last.username).to eq('pengwynn')
40
32
  end
41
33
 
42
34
  it "handles nested arrays of hashes" do
43
- env = { :body => [[{ "username" => "sferik" }, { "username" => "pengwynn" }]] }
44
- us = mashify.on_complete(env)
45
- expect(us.first.first.username).to eq('sferik')
46
- expect(us.first.last.username).to eq('pengwynn')
35
+ body = [[{ "username" => "sferik" }, { "username" => "pengwynn" }]]
36
+ us = process(body).body.first
37
+ expect(us.first.username).to eq('sferik')
38
+ expect(us.last.username).to eq('pengwynn')
47
39
  end
48
40
 
49
41
  it "handles mixed arrays" do
50
- env = { :body => [123, { "username" => "sferik" }, 456] }
51
- values = mashify.on_complete(env)
42
+ body = [123, { "username" => "sferik" }, 456]
43
+ values = process(body).body
52
44
  expect(values.first).to eq(123)
53
45
  expect(values.last).to eq(456)
54
46
  expect(values[1].username).to eq('sferik')
55
47
  end
56
48
 
57
49
  it "allows for use of custom Mash subclasses at the class level" do
58
- class MyMash < ::Hashie::Mash; end
50
+ original_class = described_class.mash_class
59
51
  described_class.mash_class = MyMash
60
52
 
61
- env = { :body => { "name" => "Erik Michaels-Ober", "username" => "sferik" } }
62
- me = mashify.on_complete(env)
63
-
64
- expect(me.class).to eq(MyMash)
65
- end
66
-
67
- it "allows for use of custom Mash subclasses at the instance level" do
68
- class MyMash < ::Hashie::Mash; end
69
- mashify = described_class.new(nil, :mash_class => MyMash)
70
-
71
- env = { :body => { "name" => "Erik Michaels-Ober", "username" => "sferik" } }
72
- me = mashify.on_complete(env)
73
-
74
- expect(me.class).to eq(MyMash)
53
+ begin
54
+ me = process({}).body
55
+ expect(me).to be_instance_of(MyMash)
56
+ ensure
57
+ described_class.mash_class = original_class
58
+ end
75
59
  end
76
60
  end
77
61
 
78
- context "integration test" do
79
- let(:stubs) { Faraday::Adapter::Test::Stubs.new }
80
- let(:connection) do
81
- Faraday::Connection.new do |builder|
82
- builder.adapter :test, stubs
83
- builder.use described_class
84
- end
85
- end
62
+ context "custom mash subclass", :type => :response do
63
+ let(:options) { {:mash_class => MyMash} }
86
64
 
87
- # although it is not good practice to pass a hash as the body, if we add ParseJson
88
- # to the middleware stack we end up testing two middlewares instead of one
89
- it "creates a Hash from the body" do
90
- stubs.get('/hash') {
91
- data = { 'name' => 'Erik Michaels-Ober', 'username' => 'sferik' }
92
- [200, {'content-type' => 'application/json; charset=utf-8'}, data]
93
- }
94
- me = connection.get('/hash').body
95
- expect(me.name).to eq('Erik Michaels-Ober')
96
- expect(me.username).to eq('sferik')
65
+ it "instance level" do
66
+ me = process({}).body
67
+ expect(me).to be_instance_of(MyMash)
97
68
  end
98
69
  end
99
70
  end
@@ -4,7 +4,7 @@ require 'faraday_middleware/request/method_override'
4
4
  describe FaradayMiddleware::MethodOverride do
5
5
 
6
6
  let(:middleware) { described_class.new(lambda {|env| env }, *options) }
7
- let(:env) { middleware.call request_env(request_method) }
7
+ let(:env) { middleware.call faraday_env(request_env(request_method)) }
8
8
 
9
9
  def request_env(method)
10
10
  { :method => method,
@@ -85,7 +85,7 @@ describe FaradayMiddleware::MethodOverride do
85
85
  let(:request_method) { :get }
86
86
 
87
87
  it "raises key error" do
88
- expect{ env }.to raise_error(IndexError, /key not found/)
88
+ expect{ env }.to raise_error(IndexError, /key [\s\w]*not found/)
89
89
  end
90
90
  end
91
91
 
@@ -19,7 +19,7 @@ describe FaradayMiddleware::OAuth2 do
19
19
  :request_headers => Faraday::Utils::Headers.new.update(headers)
20
20
  }
21
21
  app = make_app
22
- app.call(env)
22
+ app.call(faraday_env(env))
23
23
  end
24
24
 
25
25
  def make_app
@@ -64,7 +64,7 @@ describe FaradayMiddleware::OAuth2 do
64
64
 
65
65
  it "clears default with empty explicit token" do
66
66
  request = perform(:q => 'hello', :access_token => nil)
67
- expect(query_params(request)).to eq('q' => 'hello', 'access_token' => nil)
67
+ expect(query_params(request).fetch('access_token')).to_not eq('XYZ')
68
68
  expect(auth_header(request)).to be_nil
69
69
  end
70
70
  end
@@ -26,7 +26,7 @@ describe FaradayMiddleware::OAuth do
26
26
  env[:request][:oauth] = oauth_options
27
27
  end
28
28
  app = make_app
29
- app.call(env)
29
+ app.call(faraday_env(env))
30
30
  end
31
31
 
32
32
  def make_app
@@ -15,6 +15,11 @@ describe FaradayMiddleware::ParseDates, :type => :response do
15
15
  expect(process({"x" => "2012-02-01T13:14:15Z"}).body["x"].to_s).to eq(parsed)
16
16
  end
17
17
 
18
+ it "parses dates with milliseconds" do
19
+ date_str = "2012-02-01T13:14:15.123Z"
20
+ expect(process({"x" => date_str}).body["x"]).to eq(Time.parse(date_str))
21
+ end
22
+
18
23
  it "parses nested dates in hash" do
19
24
  expect(process({"x" => {"y" => "2012-02-01T13:14:15Z"}}).body["x"]["y"].to_s).to eq(parsed)
20
25
  end
@@ -2,71 +2,46 @@ require 'helper'
2
2
  require 'faraday_middleware/response/rashify'
3
3
 
4
4
  describe FaradayMiddleware::Rashify do
5
-
6
- context "when used" do
7
- let(:rashify) { described_class.new }
8
-
5
+ context "when used", :type => :response do
9
6
  it "creates a Hashie::Rash from the body" do
10
- env = { :body => { "name" => "Erik Michaels-Ober", "username" => "sferik" } }
11
- me = rashify.on_complete(env)
12
- expect(me.class).to eq(Hashie::Rash)
7
+ body = { "name" => "Erik Michaels-Ober", "username" => "sferik" }
8
+ me = process(body).body
9
+ expect(me.name).to eq("Erik Michaels-Ober")
10
+ expect(me.username).to eq("sferik")
13
11
  end
14
12
 
15
13
  it "handles strings" do
16
- env = { :body => "Most amazing string EVER" }
17
- me = rashify.on_complete(env)
14
+ body = "Most amazing string EVER"
15
+ me = process(body).body
18
16
  expect(me).to eq("Most amazing string EVER")
19
17
  end
20
18
 
21
19
  it "handles hashes and decamelcase the keys" do
22
- env = { :body => { "name" => "Erik Michaels-Ober", "userName" => "sferik" } }
23
- me = rashify.on_complete(env)
20
+ body = { "name" => "Erik Michaels-Ober", "userName" => "sferik" }
21
+ me = process(body).body
24
22
  expect(me.name).to eq('Erik Michaels-Ober')
25
23
  expect(me.user_name).to eq('sferik')
26
24
  end
27
25
 
28
26
  it "handles arrays" do
29
- env = { :body => [123, 456] }
30
- values = rashify.on_complete(env)
31
- expect(values.first).to eq(123)
32
- expect(values.last).to eq(456)
27
+ body = [123, 456]
28
+ values = process(body).body
29
+ expect(values).to eq([123, 456])
33
30
  end
34
31
 
35
32
  it "handles arrays of hashes" do
36
- env = { :body => [{ "username" => "sferik" }, { "username" => "pengwynn" }] }
37
- us = rashify.on_complete(env)
33
+ body = [{ "username" => "sferik" }, { "username" => "pengwynn" }]
34
+ us = process(body).body
38
35
  expect(us.first.username).to eq('sferik')
39
36
  expect(us.last.username).to eq('pengwynn')
40
37
  end
41
38
 
42
39
  it "handles mixed arrays" do
43
- env = { :body => [123, { "username" => "sferik" }, 456] }
44
- values = rashify.on_complete(env)
40
+ body = [123, { "username" => "sferik" }, 456]
41
+ values = process(body).body
45
42
  expect(values.first).to eq(123)
46
43
  expect(values.last).to eq(456)
47
44
  expect(values[1].username).to eq('sferik')
48
45
  end
49
46
  end
50
-
51
- context "integration test" do
52
- let(:stubs) { Faraday::Adapter::Test::Stubs.new }
53
- let(:connection) do
54
- Faraday::Connection.new do |builder|
55
- builder.adapter :test, stubs
56
- builder.use described_class
57
- end
58
- end
59
-
60
- # although it is not good practice to pass a hash as the body, if we add ParseJson
61
- # to the middleware stack we end up testing two middlewares instead of one
62
- it "creates a Hash from the body" do
63
- stubs.get('/hash') {
64
- data = { 'name' => 'Erik Michaels-Ober', 'username' => 'sferik' }
65
- [200, {'content-type' => 'application/json; charset=utf-8'}, data]
66
- }
67
- me = connection.get('/hash').body
68
- expect(me.name).to eq('Erik Michaels-Ober')
69
- expect(me.username).to eq('sferik')
70
- end
71
- end
72
47
  end
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday_middleware
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
5
- prerelease:
4
+ version: 0.9.1
6
5
  platform: ruby
7
6
  authors:
8
7
  - Erik Michaels-Ober
@@ -10,142 +9,42 @@ authors:
10
9
  autorequire:
11
10
  bindir: bin
12
11
  cert_chain: []
13
- date: 2012-10-26 00:00:00.000000000 Z
12
+ date: 2014-04-19 00:00:00.000000000 Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: faraday
17
16
  requirement: !ruby/object:Gem::Requirement
18
- none: false
19
17
  requirements:
20
- - - ! '>='
18
+ - - ">="
21
19
  - !ruby/object:Gem::Version
22
20
  version: 0.7.4
23
- - - <
21
+ - - "<"
24
22
  - !ruby/object:Gem::Version
25
- version: '0.9'
23
+ version: '0.10'
26
24
  type: :runtime
27
25
  prerelease: false
28
26
  version_requirements: !ruby/object:Gem::Requirement
29
- none: false
30
27
  requirements:
31
- - - ! '>='
28
+ - - ">="
32
29
  - !ruby/object:Gem::Version
33
30
  version: 0.7.4
34
- - - <
31
+ - - "<"
35
32
  - !ruby/object:Gem::Version
36
- version: '0.9'
33
+ version: '0.10'
37
34
  - !ruby/object:Gem::Dependency
38
- name: multi_xml
35
+ name: bundler
39
36
  requirement: !ruby/object:Gem::Requirement
40
- none: false
41
37
  requirements:
42
- - - ~>
38
+ - - "~>"
43
39
  - !ruby/object:Gem::Version
44
- version: '0.2'
40
+ version: '1.0'
45
41
  type: :development
46
42
  prerelease: false
47
43
  version_requirements: !ruby/object:Gem::Requirement
48
- none: false
49
44
  requirements:
50
- - - ~>
45
+ - - "~>"
51
46
  - !ruby/object:Gem::Version
52
- version: '0.2'
53
- - !ruby/object:Gem::Dependency
54
- name: rake
55
- requirement: !ruby/object:Gem::Requirement
56
- none: false
57
- requirements:
58
- - - ~>
59
- - !ruby/object:Gem::Version
60
- version: '0.9'
61
- type: :development
62
- prerelease: false
63
- version_requirements: !ruby/object:Gem::Requirement
64
- none: false
65
- requirements:
66
- - - ~>
67
- - !ruby/object:Gem::Version
68
- version: '0.9'
69
- - !ruby/object:Gem::Dependency
70
- name: hashie
71
- requirement: !ruby/object:Gem::Requirement
72
- none: false
73
- requirements:
74
- - - ~>
75
- - !ruby/object:Gem::Version
76
- version: '1.2'
77
- type: :development
78
- prerelease: false
79
- version_requirements: !ruby/object:Gem::Requirement
80
- none: false
81
- requirements:
82
- - - ~>
83
- - !ruby/object:Gem::Version
84
- version: '1.2'
85
- - !ruby/object:Gem::Dependency
86
- name: rash
87
- requirement: !ruby/object:Gem::Requirement
88
- none: false
89
- requirements:
90
- - - ~>
91
- - !ruby/object:Gem::Version
92
- version: '0.3'
93
- type: :development
94
- prerelease: false
95
- version_requirements: !ruby/object:Gem::Requirement
96
- none: false
97
- requirements:
98
- - - ~>
99
- - !ruby/object:Gem::Version
100
- version: '0.3'
101
- - !ruby/object:Gem::Dependency
102
- name: rspec
103
- requirement: !ruby/object:Gem::Requirement
104
- none: false
105
- requirements:
106
- - - ~>
107
- - !ruby/object:Gem::Version
108
- version: '2.6'
109
- type: :development
110
- prerelease: false
111
- version_requirements: !ruby/object:Gem::Requirement
112
- none: false
113
- requirements:
114
- - - ~>
115
- - !ruby/object:Gem::Version
116
- version: '2.6'
117
- - !ruby/object:Gem::Dependency
118
- name: simple_oauth
119
- requirement: !ruby/object:Gem::Requirement
120
- none: false
121
- requirements:
122
- - - ~>
123
- - !ruby/object:Gem::Version
124
- version: '0.1'
125
- type: :development
126
- prerelease: false
127
- version_requirements: !ruby/object:Gem::Requirement
128
- none: false
129
- requirements:
130
- - - ~>
131
- - !ruby/object:Gem::Version
132
- version: '0.1'
133
- - !ruby/object:Gem::Dependency
134
- name: rack-cache
135
- requirement: !ruby/object:Gem::Requirement
136
- none: false
137
- requirements:
138
- - - ~>
139
- - !ruby/object:Gem::Version
140
- version: '1.1'
141
- type: :development
142
- prerelease: false
143
- version_requirements: !ruby/object:Gem::Requirement
144
- none: false
145
- requirements:
146
- - - ~>
147
- - !ruby/object:Gem::Version
148
- version: '1.1'
47
+ version: '1.0'
149
48
  description: Various middleware for Faraday
150
49
  email:
151
50
  - sferik@gmail.com
@@ -154,12 +53,8 @@ executables: []
154
53
  extensions: []
155
54
  extra_rdoc_files: []
156
55
  files:
157
- - .gemtest
158
- - .gitignore
159
- - .rspec
160
- - .travis.yml
161
56
  - CHANGELOG.md
162
- - Gemfile
57
+ - CONTRIBUTING.md
163
58
  - LICENSE.md
164
59
  - README.md
165
60
  - Rakefile
@@ -185,7 +80,7 @@ files:
185
80
  - lib/faraday_middleware/response/rashify.rb
186
81
  - lib/faraday_middleware/response_middleware.rb
187
82
  - lib/faraday_middleware/version.rb
188
- - spec/caching_test.rb
83
+ - spec/caching_spec.rb
189
84
  - spec/chunked_spec.rb
190
85
  - spec/encode_json_spec.rb
191
86
  - spec/follow_redirects_spec.rb
@@ -200,32 +95,32 @@ files:
200
95
  - spec/parse_xml_spec.rb
201
96
  - spec/parse_yaml_spec.rb
202
97
  - spec/rashify_spec.rb
203
- homepage: https://github.com/pengwynn/faraday_middleware
204
- licenses: []
98
+ homepage: https://github.com/lostisland/faraday_middleware
99
+ licenses:
100
+ - MIT
101
+ metadata: {}
205
102
  post_install_message:
206
103
  rdoc_options: []
207
104
  require_paths:
208
105
  - lib
209
106
  required_ruby_version: !ruby/object:Gem::Requirement
210
- none: false
211
107
  requirements:
212
- - - ! '>='
108
+ - - ">="
213
109
  - !ruby/object:Gem::Version
214
110
  version: '0'
215
111
  required_rubygems_version: !ruby/object:Gem::Requirement
216
- none: false
217
112
  requirements:
218
- - - ! '>='
113
+ - - ">="
219
114
  - !ruby/object:Gem::Version
220
- version: 1.3.6
115
+ version: 1.3.5
221
116
  requirements: []
222
117
  rubyforge_project:
223
- rubygems_version: 1.8.23
118
+ rubygems_version: 2.2.0
224
119
  signing_key:
225
- specification_version: 3
120
+ specification_version: 4
226
121
  summary: Various middleware for Faraday
227
122
  test_files:
228
- - spec/caching_test.rb
123
+ - spec/caching_spec.rb
229
124
  - spec/chunked_spec.rb
230
125
  - spec/encode_json_spec.rb
231
126
  - spec/follow_redirects_spec.rb