agiley-faraday_middleware 0.8.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/.gemtest +0 -0
  2. data/.gitignore +31 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +7 -0
  5. data/CHANGELOG.md +10 -0
  6. data/Gemfile +7 -0
  7. data/LICENSE.md +20 -0
  8. data/README.md +54 -0
  9. data/Rakefile +17 -0
  10. data/faraday_middleware.gemspec +24 -0
  11. data/lib/faraday_middleware.rb +42 -0
  12. data/lib/faraday_middleware/addressable_patch.rb +20 -0
  13. data/lib/faraday_middleware/backwards_compatibility.rb +15 -0
  14. data/lib/faraday_middleware/instrumentation.rb +30 -0
  15. data/lib/faraday_middleware/rack_compatible.rb +76 -0
  16. data/lib/faraday_middleware/request/encode_json.rb +50 -0
  17. data/lib/faraday_middleware/request/oauth.rb +64 -0
  18. data/lib/faraday_middleware/request/oauth2.rb +62 -0
  19. data/lib/faraday_middleware/response/caching.rb +76 -0
  20. data/lib/faraday_middleware/response/follow_redirects.rb +53 -0
  21. data/lib/faraday_middleware/response/mashify.rb +28 -0
  22. data/lib/faraday_middleware/response/parse_json.rb +38 -0
  23. data/lib/faraday_middleware/response/parse_marshal.rb +13 -0
  24. data/lib/faraday_middleware/response/parse_nokogiri_xml.rb +14 -0
  25. data/lib/faraday_middleware/response/parse_xml.rb +14 -0
  26. data/lib/faraday_middleware/response/parse_yaml.rb +13 -0
  27. data/lib/faraday_middleware/response/rashify.rb +13 -0
  28. data/lib/faraday_middleware/response_middleware.rb +78 -0
  29. data/lib/faraday_middleware/version.rb +3 -0
  30. data/spec/caching_test.rb +122 -0
  31. data/spec/encode_json_spec.rb +95 -0
  32. data/spec/follow_redirects_spec.rb +33 -0
  33. data/spec/helper.rb +33 -0
  34. data/spec/mashify_spec.rb +79 -0
  35. data/spec/oauth2_spec.rb +118 -0
  36. data/spec/oauth_spec.rb +101 -0
  37. data/spec/parse_json_spec.rb +94 -0
  38. data/spec/parse_marshal_spec.rb +16 -0
  39. data/spec/parse_xml_spec.rb +71 -0
  40. data/spec/parse_yaml_spec.rb +53 -0
  41. data/spec/rashify_spec.rb +69 -0
  42. metadata +202 -0
@@ -0,0 +1,94 @@
1
+ require 'helper'
2
+ require 'faraday_middleware/response/parse_json'
3
+
4
+ describe FaradayMiddleware::ParseJson, :type => :response do
5
+ context "no type matching" do
6
+ it "doesn't change nil body" do
7
+ process(nil).body.should be_nil
8
+ end
9
+
10
+ it "nullifies empty body" do
11
+ process('').body.should be_nil
12
+ end
13
+
14
+ it "parses json body" do
15
+ response = process('{"a":1}')
16
+ response.body.should eql('a' => 1)
17
+ response.env[:raw_body].should be_nil
18
+ end
19
+ end
20
+
21
+ context "with preserving raw" do
22
+ let(:options) { {:preserve_raw => true} }
23
+
24
+ it "parses json body" do
25
+ response = process('{"a":1}')
26
+ response.body.should eql('a' => 1)
27
+ response.env[:raw_body].should eql('{"a":1}')
28
+ end
29
+
30
+ it "can opt out of preserving raw" do
31
+ response = process('{"a":1}', nil, :preserve_raw => false)
32
+ response.env[:raw_body].should be_nil
33
+ end
34
+ end
35
+
36
+ context "with regexp type matching" do
37
+ let(:options) { {:content_type => /\bjson$/} }
38
+
39
+ it "parses json body of correct type" do
40
+ response = process('{"a":1}', 'application/x-json')
41
+ response.body.should eql('a' => 1)
42
+ end
43
+
44
+ it "ignores json body of incorrect type" do
45
+ response = process('{"a":1}', 'text/json-xml')
46
+ response.body.should eql('{"a":1}')
47
+ end
48
+ end
49
+
50
+ context "with array type matching" do
51
+ let(:options) { {:content_type => %w[a/b c/d]} }
52
+
53
+ it "parses json body of correct type" do
54
+ process('{"a":1}', 'a/b').body.should be_a(Hash)
55
+ process('{"a":1}', 'c/d').body.should be_a(Hash)
56
+ end
57
+
58
+ it "ignores json body of incorrect type" do
59
+ process('{"a":1}', 'a/d').body.should_not be_a(Hash)
60
+ end
61
+ end
62
+
63
+ it "chokes on invalid json" do
64
+ ['{!', ' ', '"a"', 'true', 'null', '1'].each do |data|
65
+ expect { process(data) }.to raise_error(Faraday::Error::ParsingError)
66
+ end
67
+ end
68
+
69
+ context "with mime type fix" do
70
+ let(:middleware) {
71
+ app = described_class::MimeTypeFix.new(lambda {|env|
72
+ Faraday::Response.new(env)
73
+ }, :content_type => /^text\//)
74
+ described_class.new(app, :content_type => 'application/json')
75
+ }
76
+
77
+ it "ignores completely incompatible type" do
78
+ response = process('{"a":1}', 'application/xml')
79
+ response.body.should eql('{"a":1}')
80
+ end
81
+
82
+ it "ignores compatible type with bad data" do
83
+ response = process('var a = 1', 'text/javascript')
84
+ response.body.should eql('var a = 1')
85
+ response['content-type'].should eql('text/javascript')
86
+ end
87
+
88
+ it "corrects compatible type and data" do
89
+ response = process('{"a":1}', 'text/javascript')
90
+ response.body.should be_a(Hash)
91
+ response['content-type'].should eql('application/json')
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,16 @@
1
+ require 'helper'
2
+ require 'faraday_middleware/response/parse_marshal'
3
+
4
+ describe FaradayMiddleware::ParseMarshal, :type => :response do
5
+ it "restores a marshaled dump" do
6
+ process(Marshal.dump(:a => 1)).body.should be_eql(:a => 1)
7
+ end
8
+
9
+ it "nulifies blank response" do
10
+ process('').body.should be_nil
11
+ end
12
+
13
+ it "chokes on invalid content" do
14
+ expect { process('abc') }.to raise_error(Faraday::Error::ParsingError)
15
+ end
16
+ end
@@ -0,0 +1,71 @@
1
+ require 'helper'
2
+ require 'faraday_middleware/response/parse_xml'
3
+
4
+ describe FaradayMiddleware::ParseXml, :type => :response do
5
+ let(:xml) { '<user><name>Erik Michaels-Ober</name><screen_name>sferik</screen_name></user>' }
6
+ let(:user) { {'user' => {'name' => 'Erik Michaels-Ober', 'screen_name' => 'sferik'} } }
7
+
8
+ context "no type matching" do
9
+ it "doesn't change nil body" do
10
+ process(nil).body.should be_nil
11
+ end
12
+
13
+ it "turns empty body into empty hash" do
14
+ process('').body.should be_eql({})
15
+ end
16
+
17
+ it "parses xml body" do
18
+ response = process(xml)
19
+ response.body.should eql(user)
20
+ response.env[:raw_body].should be_nil
21
+ end
22
+ end
23
+
24
+ context "with preserving raw" do
25
+ let(:options) { {:preserve_raw => true} }
26
+
27
+ it "parses xml body" do
28
+ response = process(xml)
29
+ response.body.should eql(user)
30
+ response.env[:raw_body].should eql(xml)
31
+ end
32
+
33
+ it "can opt out of preserving raw" do
34
+ response = process(xml, nil, :preserve_raw => false)
35
+ response.env[:raw_body].should be_nil
36
+ end
37
+ end
38
+
39
+ context "with regexp type matching" do
40
+ let(:options) { {:content_type => /\bxml$/} }
41
+
42
+ it "parses xml body of correct type" do
43
+ response = process(xml, 'application/xml')
44
+ response.body.should eql(user)
45
+ end
46
+
47
+ it "ignores xml body of incorrect type" do
48
+ response = process(xml, 'text/html')
49
+ response.body.should eql(xml)
50
+ end
51
+ end
52
+
53
+ context "with array type matching" do
54
+ let(:options) { {:content_type => %w[a/b c/d]} }
55
+
56
+ it "parses xml body of correct type" do
57
+ process(xml, 'a/b').body.should be_a(Hash)
58
+ process(xml, 'c/d').body.should be_a(Hash)
59
+ end
60
+
61
+ it "ignores xml body of incorrect type" do
62
+ process(xml, 'a/d').body.should_not be_a(Hash)
63
+ end
64
+ end
65
+
66
+ it "chokes on invalid xml" do
67
+ ['{!', '"a"', 'true', 'null', '1'].each do |data|
68
+ expect { process(data) }.to raise_error(Faraday::Error::ParsingError)
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,53 @@
1
+ require 'helper'
2
+ require 'faraday_middleware/response/parse_yaml'
3
+
4
+ describe FaradayMiddleware::ParseYaml, :type => :response do
5
+ context "no type matching" do
6
+ it "doesn't change nil body" do
7
+ process(nil).body.should be_nil
8
+ end
9
+
10
+ it "returns false for empty body" do
11
+ process('').body.should be_false
12
+ end
13
+
14
+ it "parses yaml body" do
15
+ response = process('a: 1')
16
+ response.body.should eql('a' => 1)
17
+ response.env[:raw_body].should be_nil
18
+ end
19
+ end
20
+
21
+ context "with preserving raw" do
22
+ let(:options) { {:preserve_raw => true} }
23
+
24
+ it "parses yaml body" do
25
+ response = process('a: 1')
26
+ response.body.should eql('a' => 1)
27
+ response.env[:raw_body].should eql('a: 1')
28
+ end
29
+
30
+ it "can opt out of preserving raw" do
31
+ response = process('a: 1', nil, :preserve_raw => false)
32
+ response.env[:raw_body].should be_nil
33
+ end
34
+ end
35
+
36
+ context "with regexp type matching" do
37
+ let(:options) { {:content_type => /\byaml$/} }
38
+
39
+ it "parses json body of correct type" do
40
+ response = process('a: 1', 'application/x-yaml')
41
+ response.body.should eql('a' => 1)
42
+ end
43
+
44
+ it "ignores json body of incorrect type" do
45
+ response = process('a: 1', 'text/yaml-xml')
46
+ response.body.should eql('a: 1')
47
+ end
48
+ end
49
+
50
+ it "chokes on invalid yaml" do
51
+ expect { process('{!') }.to raise_error(Faraday::Error::ParsingError)
52
+ end
53
+ end
@@ -0,0 +1,69 @@
1
+ require 'helper'
2
+ require 'faraday_middleware/response/rashify'
3
+
4
+ describe FaradayMiddleware::Rashify do
5
+
6
+ context 'when used' do
7
+ let(:rashify) { described_class.new }
8
+
9
+ it 'should create a Hashie::Rash from the body' do
10
+ env = { :body => { "name" => "Erik Michaels-Ober", "username" => "sferik" } }
11
+ me = rashify.on_complete(env)
12
+ me.class.should == Hashie::Rash
13
+ end
14
+
15
+ it 'should handle strings' do
16
+ env = { :body => "Most amazing string EVER" }
17
+ me = rashify.on_complete(env)
18
+ me.should == "Most amazing string EVER"
19
+ end
20
+
21
+ it 'should handle hashes and decamelcase the keys' do
22
+ env = { :body => { "name" => "Erik Michaels-Ober", "userName" => "sferik" } }
23
+ me = rashify.on_complete(env)
24
+ me.name.should == 'Erik Michaels-Ober'
25
+ me.user_name.should == 'sferik'
26
+ end
27
+
28
+ it 'should handle arrays' do
29
+ env = { :body => [123, 456] }
30
+ values = rashify.on_complete(env)
31
+ values.first.should == 123
32
+ values.last.should == 456
33
+ end
34
+
35
+ it 'should handle arrays of hashes' do
36
+ env = { :body => [{ "username" => "sferik" }, { "username" => "pengwynn" }] }
37
+ us = rashify.on_complete(env)
38
+ us.first.username.should == 'sferik'
39
+ us.last.username.should == 'pengwynn'
40
+ end
41
+
42
+ it 'should handle mixed arrays' do
43
+ env = { :body => [123, { "username" => "sferik" }, 456] }
44
+ values = rashify.on_complete(env)
45
+ values.first.should == 123
46
+ values.last.should == 456
47
+ values[1].username.should == 'sferik'
48
+ end
49
+ 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 'should create a Hash from the body' do
63
+ stubs.get('/hash') {[200, {'content-type' => 'application/json; charset=utf-8'}, { "name" => "Erik Michaels-Ober", "username" => "sferik" }]}
64
+ me = connection.get('/hash').body
65
+ me.name.should == 'Erik Michaels-Ober'
66
+ me.username.should == 'sferik'
67
+ end
68
+ end
69
+ end
metadata ADDED
@@ -0,0 +1,202 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: agiley-faraday_middleware
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.8.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Erik Michaels-Ober
9
+ - Wynn Netherland
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-01-29 00:00:00.000000000Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: faraday
17
+ requirement: &70130128226140 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 0.7.4
23
+ - - <
24
+ - !ruby/object:Gem::Version
25
+ version: '0.9'
26
+ type: :runtime
27
+ prerelease: false
28
+ version_requirements: *70130128226140
29
+ - !ruby/object:Gem::Dependency
30
+ name: multi_xml
31
+ requirement: &70130128225380 !ruby/object:Gem::Requirement
32
+ none: false
33
+ requirements:
34
+ - - ~>
35
+ - !ruby/object:Gem::Version
36
+ version: '0.2'
37
+ type: :development
38
+ prerelease: false
39
+ version_requirements: *70130128225380
40
+ - !ruby/object:Gem::Dependency
41
+ name: nokogiri
42
+ requirement: &70130128224920 !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 1.5.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: *70130128224920
51
+ - !ruby/object:Gem::Dependency
52
+ name: rake
53
+ requirement: &70130128224460 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ version: '0.9'
59
+ type: :development
60
+ prerelease: false
61
+ version_requirements: *70130128224460
62
+ - !ruby/object:Gem::Dependency
63
+ name: hashie
64
+ requirement: &70130128224000 !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '1.2'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: *70130128224000
73
+ - !ruby/object:Gem::Dependency
74
+ name: rash
75
+ requirement: &70130128223540 !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ~>
79
+ - !ruby/object:Gem::Version
80
+ version: '0.3'
81
+ type: :development
82
+ prerelease: false
83
+ version_requirements: *70130128223540
84
+ - !ruby/object:Gem::Dependency
85
+ name: rspec
86
+ requirement: &70130128223080 !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ~>
90
+ - !ruby/object:Gem::Version
91
+ version: '2.6'
92
+ type: :development
93
+ prerelease: false
94
+ version_requirements: *70130128223080
95
+ - !ruby/object:Gem::Dependency
96
+ name: simple_oauth
97
+ requirement: &70130128222620 !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ~>
101
+ - !ruby/object:Gem::Version
102
+ version: '0.1'
103
+ type: :development
104
+ prerelease: false
105
+ version_requirements: *70130128222620
106
+ - !ruby/object:Gem::Dependency
107
+ name: rack-cache
108
+ requirement: &70130128222160 !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ~>
112
+ - !ruby/object:Gem::Version
113
+ version: '1.1'
114
+ type: :development
115
+ prerelease: false
116
+ version_requirements: *70130128222160
117
+ description: Various middleware for Faraday
118
+ email:
119
+ - sferik@gmail.com
120
+ - wynn.netherland@gmail.com
121
+ executables: []
122
+ extensions: []
123
+ extra_rdoc_files: []
124
+ files:
125
+ - .gemtest
126
+ - .gitignore
127
+ - .rspec
128
+ - .travis.yml
129
+ - CHANGELOG.md
130
+ - Gemfile
131
+ - LICENSE.md
132
+ - README.md
133
+ - Rakefile
134
+ - faraday_middleware.gemspec
135
+ - lib/faraday_middleware.rb
136
+ - lib/faraday_middleware/addressable_patch.rb
137
+ - lib/faraday_middleware/backwards_compatibility.rb
138
+ - lib/faraday_middleware/instrumentation.rb
139
+ - lib/faraday_middleware/rack_compatible.rb
140
+ - lib/faraday_middleware/request/encode_json.rb
141
+ - lib/faraday_middleware/request/oauth.rb
142
+ - lib/faraday_middleware/request/oauth2.rb
143
+ - lib/faraday_middleware/response/caching.rb
144
+ - lib/faraday_middleware/response/follow_redirects.rb
145
+ - lib/faraday_middleware/response/mashify.rb
146
+ - lib/faraday_middleware/response/parse_json.rb
147
+ - lib/faraday_middleware/response/parse_marshal.rb
148
+ - lib/faraday_middleware/response/parse_nokogiri_xml.rb
149
+ - lib/faraday_middleware/response/parse_xml.rb
150
+ - lib/faraday_middleware/response/parse_yaml.rb
151
+ - lib/faraday_middleware/response/rashify.rb
152
+ - lib/faraday_middleware/response_middleware.rb
153
+ - lib/faraday_middleware/version.rb
154
+ - spec/caching_test.rb
155
+ - spec/encode_json_spec.rb
156
+ - spec/follow_redirects_spec.rb
157
+ - spec/helper.rb
158
+ - spec/mashify_spec.rb
159
+ - spec/oauth2_spec.rb
160
+ - spec/oauth_spec.rb
161
+ - spec/parse_json_spec.rb
162
+ - spec/parse_marshal_spec.rb
163
+ - spec/parse_xml_spec.rb
164
+ - spec/parse_yaml_spec.rb
165
+ - spec/rashify_spec.rb
166
+ homepage: https://github.com/pengwynn/faraday_middleware
167
+ licenses: []
168
+ post_install_message:
169
+ rdoc_options: []
170
+ require_paths:
171
+ - lib
172
+ required_ruby_version: !ruby/object:Gem::Requirement
173
+ none: false
174
+ requirements:
175
+ - - ! '>='
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ required_rubygems_version: !ruby/object:Gem::Requirement
179
+ none: false
180
+ requirements:
181
+ - - ! '>='
182
+ - !ruby/object:Gem::Version
183
+ version: 1.3.6
184
+ requirements: []
185
+ rubyforge_project:
186
+ rubygems_version: 1.8.15
187
+ signing_key:
188
+ specification_version: 3
189
+ summary: Various middleware for Faraday
190
+ test_files:
191
+ - spec/caching_test.rb
192
+ - spec/encode_json_spec.rb
193
+ - spec/follow_redirects_spec.rb
194
+ - spec/helper.rb
195
+ - spec/mashify_spec.rb
196
+ - spec/oauth2_spec.rb
197
+ - spec/oauth_spec.rb
198
+ - spec/parse_json_spec.rb
199
+ - spec/parse_marshal_spec.rb
200
+ - spec/parse_xml_spec.rb
201
+ - spec/parse_yaml_spec.rb
202
+ - spec/rashify_spec.rb