faraday_middleware 0.8.6 → 0.8.7
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -3
- data/lib/faraday_middleware/response/mashify.rb +9 -2
- data/lib/faraday_middleware/response/parse_json.rb +4 -2
- data/lib/faraday_middleware/version.rb +1 -1
- data/spec/caching_test.rb +16 -1
- data/spec/mashify_spec.rb +11 -1
- data/spec/parse_json_spec.rb +13 -1
- metadata +63 -19
data/.gitignore
CHANGED
@@ -4,6 +4,8 @@ module FaradayMiddleware
|
|
4
4
|
# Public: Converts parsed response bodies to a Hashie::Mash if they were of
|
5
5
|
# Hash or Array type.
|
6
6
|
class Mashify < Faraday::Response::Middleware
|
7
|
+
attr_accessor :mash_class
|
8
|
+
|
7
9
|
class << self
|
8
10
|
attr_accessor :mash_class
|
9
11
|
end
|
@@ -13,12 +15,17 @@ module FaradayMiddleware
|
|
13
15
|
self.mash_class = ::Hashie::Mash
|
14
16
|
end
|
15
17
|
|
18
|
+
def initialize(app = nil, options = {})
|
19
|
+
super(app)
|
20
|
+
self.mash_class = options[:mash_class] || self.class.mash_class
|
21
|
+
end
|
22
|
+
|
16
23
|
def parse(body)
|
17
24
|
case body
|
18
25
|
when Hash
|
19
|
-
|
26
|
+
mash_class.new(body)
|
20
27
|
when Array
|
21
|
-
body.map { |item| item.is_a?(Hash) ?
|
28
|
+
body.map { |item| item.is_a?(Hash) ? mash_class.new(item) : item }
|
22
29
|
else
|
23
30
|
body
|
24
31
|
end
|
@@ -3,10 +3,12 @@ require 'faraday_middleware/response_middleware'
|
|
3
3
|
module FaradayMiddleware
|
4
4
|
# Public: Parse response bodies as JSON.
|
5
5
|
class ParseJson < ResponseMiddleware
|
6
|
-
dependency
|
6
|
+
dependency {
|
7
|
+
require 'json' unless defined?(JSON)
|
8
|
+
}
|
7
9
|
|
8
10
|
define_parser { |body|
|
9
|
-
JSON.parse body unless body.empty?
|
11
|
+
JSON.parse body unless body.strip.empty?
|
10
12
|
}
|
11
13
|
|
12
14
|
# Public: Override the content-type of the response with "application/json"
|
data/spec/caching_test.rb
CHANGED
@@ -2,6 +2,7 @@ require 'test/unit'
|
|
2
2
|
require 'forwardable'
|
3
3
|
require 'fileutils'
|
4
4
|
require 'rack/cache'
|
5
|
+
require 'faraday'
|
5
6
|
require 'faraday_middleware/response/caching'
|
6
7
|
require 'faraday_middleware/rack_compatible'
|
7
8
|
|
@@ -87,10 +88,22 @@ end
|
|
87
88
|
class HttpCachingTest < Test::Unit::TestCase
|
88
89
|
include FileUtils
|
89
90
|
|
90
|
-
CACHE_DIR = File.expand_path('
|
91
|
+
CACHE_DIR = File.expand_path('../../tmp/cache', __FILE__)
|
92
|
+
|
93
|
+
# middleware to check whether "rack.errors" is free of error reports
|
94
|
+
class RackErrorsComplainer < Struct.new(:app)
|
95
|
+
def call(env)
|
96
|
+
response = app.call(env)
|
97
|
+
error_stream = env['rack.errors'].string
|
98
|
+
raise %(unexpected error in 'rack.errors') if error_stream.include? 'error'
|
99
|
+
response
|
100
|
+
end
|
101
|
+
end
|
91
102
|
|
92
103
|
def setup
|
93
104
|
rm_r CACHE_DIR if File.exists? CACHE_DIR
|
105
|
+
# force reinitializing cache dirs
|
106
|
+
Rack::Cache::Storage.instance.clear
|
94
107
|
|
95
108
|
request_count = 0
|
96
109
|
response = lambda { |env|
|
@@ -101,6 +114,8 @@ class HttpCachingTest < Test::Unit::TestCase
|
|
101
114
|
}
|
102
115
|
|
103
116
|
@conn = Faraday.new do |b|
|
117
|
+
b.use RackErrorsComplainer
|
118
|
+
|
104
119
|
b.use FaradayMiddleware::RackCompatible, Rack::Cache::Context,
|
105
120
|
:metastore => "file:#{CACHE_DIR}/rack/meta",
|
106
121
|
:entitystore => "file:#{CACHE_DIR}/rack/body",
|
data/spec/mashify_spec.rb
CHANGED
@@ -47,7 +47,7 @@ describe FaradayMiddleware::Mashify do
|
|
47
47
|
values[1].username.should == 'sferik'
|
48
48
|
end
|
49
49
|
|
50
|
-
it 'should allow for use of custom Mash subclasses' do
|
50
|
+
it 'should allow for use of custom Mash subclasses at the class level' do
|
51
51
|
class MyMash < ::Hashie::Mash; end
|
52
52
|
described_class.mash_class = MyMash
|
53
53
|
|
@@ -56,6 +56,16 @@ describe FaradayMiddleware::Mashify do
|
|
56
56
|
|
57
57
|
me.class.should == MyMash
|
58
58
|
end
|
59
|
+
|
60
|
+
it 'should allow for use of custom Mash subclasses at the instance level' do
|
61
|
+
class MyMash < ::Hashie::Mash; end
|
62
|
+
mashify = described_class.new(nil, :mash_class => MyMash)
|
63
|
+
|
64
|
+
env = { :body => { "name" => "Erik Michaels-Ober", "username" => "sferik" } }
|
65
|
+
me = mashify.on_complete(env)
|
66
|
+
|
67
|
+
me.class.should == MyMash
|
68
|
+
end
|
59
69
|
end
|
60
70
|
|
61
71
|
context 'integration test' do
|
data/spec/parse_json_spec.rb
CHANGED
@@ -61,7 +61,7 @@ describe FaradayMiddleware::ParseJson, :type => :response do
|
|
61
61
|
end
|
62
62
|
|
63
63
|
it "chokes on invalid json" do
|
64
|
-
['{!', '
|
64
|
+
['{!', '"a"', 'true', 'null', '1'].each do |data|
|
65
65
|
expect { process(data) }.to raise_error(Faraday::Error::ParsingError)
|
66
66
|
end
|
67
67
|
end
|
@@ -97,4 +97,16 @@ describe FaradayMiddleware::ParseJson, :type => :response do
|
|
97
97
|
response['content-type'].should eql('application/json')
|
98
98
|
end
|
99
99
|
end
|
100
|
+
|
101
|
+
context "HEAD responses" do
|
102
|
+
it "should nullify the body if it's only one space" do
|
103
|
+
response = process(' ')
|
104
|
+
response.body.should be_nil
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should nullify the body if it's two spaces" do
|
108
|
+
response = process(' ')
|
109
|
+
response.body.should be_nil
|
110
|
+
end
|
111
|
+
end
|
100
112
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday_middleware
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,11 +10,11 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-
|
13
|
+
date: 2012-04-18 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: faraday
|
17
|
-
requirement:
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ! '>='
|
@@ -25,10 +25,18 @@ dependencies:
|
|
25
25
|
version: '0.9'
|
26
26
|
type: :runtime
|
27
27
|
prerelease: false
|
28
|
-
version_requirements:
|
28
|
+
version_requirements: !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.7.4
|
34
|
+
- - <
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0.9'
|
29
37
|
- !ruby/object:Gem::Dependency
|
30
38
|
name: multi_xml
|
31
|
-
requirement:
|
39
|
+
requirement: !ruby/object:Gem::Requirement
|
32
40
|
none: false
|
33
41
|
requirements:
|
34
42
|
- - ~>
|
@@ -36,10 +44,15 @@ dependencies:
|
|
36
44
|
version: '0.2'
|
37
45
|
type: :development
|
38
46
|
prerelease: false
|
39
|
-
version_requirements:
|
47
|
+
version_requirements: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ~>
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0.2'
|
40
53
|
- !ruby/object:Gem::Dependency
|
41
54
|
name: rake
|
42
|
-
requirement:
|
55
|
+
requirement: !ruby/object:Gem::Requirement
|
43
56
|
none: false
|
44
57
|
requirements:
|
45
58
|
- - ~>
|
@@ -47,10 +60,15 @@ dependencies:
|
|
47
60
|
version: '0.9'
|
48
61
|
type: :development
|
49
62
|
prerelease: false
|
50
|
-
version_requirements:
|
63
|
+
version_requirements: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.9'
|
51
69
|
- !ruby/object:Gem::Dependency
|
52
70
|
name: hashie
|
53
|
-
requirement:
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
54
72
|
none: false
|
55
73
|
requirements:
|
56
74
|
- - ~>
|
@@ -58,10 +76,15 @@ dependencies:
|
|
58
76
|
version: '1.2'
|
59
77
|
type: :development
|
60
78
|
prerelease: false
|
61
|
-
version_requirements:
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ~>
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '1.2'
|
62
85
|
- !ruby/object:Gem::Dependency
|
63
86
|
name: rash
|
64
|
-
requirement:
|
87
|
+
requirement: !ruby/object:Gem::Requirement
|
65
88
|
none: false
|
66
89
|
requirements:
|
67
90
|
- - ~>
|
@@ -69,10 +92,15 @@ dependencies:
|
|
69
92
|
version: '0.3'
|
70
93
|
type: :development
|
71
94
|
prerelease: false
|
72
|
-
version_requirements:
|
95
|
+
version_requirements: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ~>
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0.3'
|
73
101
|
- !ruby/object:Gem::Dependency
|
74
102
|
name: rspec
|
75
|
-
requirement:
|
103
|
+
requirement: !ruby/object:Gem::Requirement
|
76
104
|
none: false
|
77
105
|
requirements:
|
78
106
|
- - ~>
|
@@ -80,10 +108,15 @@ dependencies:
|
|
80
108
|
version: '2.6'
|
81
109
|
type: :development
|
82
110
|
prerelease: false
|
83
|
-
version_requirements:
|
111
|
+
version_requirements: !ruby/object:Gem::Requirement
|
112
|
+
none: false
|
113
|
+
requirements:
|
114
|
+
- - ~>
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '2.6'
|
84
117
|
- !ruby/object:Gem::Dependency
|
85
118
|
name: simple_oauth
|
86
|
-
requirement:
|
119
|
+
requirement: !ruby/object:Gem::Requirement
|
87
120
|
none: false
|
88
121
|
requirements:
|
89
122
|
- - ~>
|
@@ -91,10 +124,15 @@ dependencies:
|
|
91
124
|
version: '0.1'
|
92
125
|
type: :development
|
93
126
|
prerelease: false
|
94
|
-
version_requirements:
|
127
|
+
version_requirements: !ruby/object:Gem::Requirement
|
128
|
+
none: false
|
129
|
+
requirements:
|
130
|
+
- - ~>
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0.1'
|
95
133
|
- !ruby/object:Gem::Dependency
|
96
134
|
name: rack-cache
|
97
|
-
requirement:
|
135
|
+
requirement: !ruby/object:Gem::Requirement
|
98
136
|
none: false
|
99
137
|
requirements:
|
100
138
|
- - ~>
|
@@ -102,7 +140,12 @@ dependencies:
|
|
102
140
|
version: '1.1'
|
103
141
|
type: :development
|
104
142
|
prerelease: false
|
105
|
-
version_requirements:
|
143
|
+
version_requirements: !ruby/object:Gem::Requirement
|
144
|
+
none: false
|
145
|
+
requirements:
|
146
|
+
- - ~>
|
147
|
+
- !ruby/object:Gem::Version
|
148
|
+
version: '1.1'
|
106
149
|
description: Various middleware for Faraday
|
107
150
|
email:
|
108
151
|
- sferik@gmail.com
|
@@ -171,7 +214,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
171
214
|
version: 1.3.6
|
172
215
|
requirements: []
|
173
216
|
rubyforge_project:
|
174
|
-
rubygems_version: 1.8.
|
217
|
+
rubygems_version: 1.8.22
|
175
218
|
signing_key:
|
176
219
|
specification_version: 3
|
177
220
|
summary: Various middleware for Faraday
|
@@ -188,3 +231,4 @@ test_files:
|
|
188
231
|
- spec/parse_xml_spec.rb
|
189
232
|
- spec/parse_yaml_spec.rb
|
190
233
|
- spec/rashify_spec.rb
|
234
|
+
has_rdoc:
|