chain 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/chain.rb +2 -0
- data/lib/chain/middleware/hashie_mash_response.rb +21 -5
- data/lib/chain/middleware/parse_error.rb +9 -0
- data/lib/chain/middleware/request_error.rb +12 -0
- data/lib/chain/version.rb +1 -1
- data/spec/middleware/hasie_mash_response_spec.rb +38 -0
- data/spec/url_spec.rb +8 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d41aefd46847e3024559446254ca04cff1145beb
|
4
|
+
data.tar.gz: ad97bde45dc46839e592b45c3244b2eae472c2a1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0f7475d798767b183a715e9952f486818d27fbf4887900e7551c06d062fdf9776b53ce7661668d7b6982a082a34c043ec2bf06a842b6a69a550c731a89cb1e95
|
7
|
+
data.tar.gz: 652d27ed64b97d9d0d056f6d0891511d32a44b972a13572d0bcbdd71b54801666d29c2558090604aa8280758a14ab31c1f137a0c79e0459b945b85ef18d2cd28
|
data/README.md
CHANGED
data/lib/chain.rb
CHANGED
@@ -2,12 +2,28 @@ module Chain
|
|
2
2
|
module Middleware
|
3
3
|
class HashieMashResponse < Faraday::Response::Middleware
|
4
4
|
def on_complete(env)
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
5
|
+
case env[:status]
|
6
|
+
when 200
|
7
|
+
body = env[:body].to_s.encode('UTF-8', {invalid: :replace, undef: :replace, replace: '?'})
|
8
|
+
|
9
|
+
json = JSON.parse(body)
|
10
|
+
headers = env[:response_headers]
|
11
|
+
|
12
|
+
env[:body] = Hashie::Mash.new(json).tap do |item|
|
13
|
+
item._headers = Hashie::Mash.new(headers)
|
14
|
+
item._status = env[:status]
|
15
|
+
end
|
16
|
+
else
|
17
|
+
raise Chain::Middleware::RequestError, env[:status]
|
10
18
|
end
|
19
|
+
|
20
|
+
rescue JSON::ParserError => ex
|
21
|
+
raise Chain::Middleware::ParseError, "Unable to parse JSON response: #{ex.message}"
|
22
|
+
|
23
|
+
rescue NoMethodError => ex
|
24
|
+
# This captures parsing errors from Hashie::Mash. Unfortunately, HM does not raise
|
25
|
+
# their own errors.
|
26
|
+
raise Chain::Middleware::ParseError, "Unable to parse JSON as object: #{ex.message}"
|
11
27
|
end
|
12
28
|
end
|
13
29
|
end
|
data/lib/chain/version.rb
CHANGED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Chain::Middleware::HashieMashResponse do
|
4
|
+
|
5
|
+
subject do
|
6
|
+
described_class.new
|
7
|
+
end
|
8
|
+
|
9
|
+
describe '#on_complete' do
|
10
|
+
|
11
|
+
context 'a valid response response with a 200 status' do
|
12
|
+
let(:env){{body: {success: true}.to_json, status: 200}}
|
13
|
+
|
14
|
+
it 'should yield a Hashie::Mash object from the request' do
|
15
|
+
subject.on_complete(env)
|
16
|
+
expect(env[:body].success).to be true
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'a response with invalid json' do
|
21
|
+
let(:env){{body: "[1,2,3]", status: 200}}
|
22
|
+
|
23
|
+
it 'should raise a ParseError instance' do
|
24
|
+
expect { subject.on_complete(env) }.to raise_error(Chain::Middleware::ParseError)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'a response with a 404 status' do
|
29
|
+
let(:env){{body: nil, status: 404}}
|
30
|
+
|
31
|
+
it 'should raise an instance of RequestError with a 404 status code' do
|
32
|
+
expect { subject.on_complete(env) }.to raise_error do |exception|
|
33
|
+
expect(exception.status_code).to eq(404)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/spec/url_spec.rb
CHANGED
@@ -88,6 +88,14 @@ describe Chain::Url do
|
|
88
88
|
should have_been_made
|
89
89
|
end
|
90
90
|
|
91
|
+
it 'should generate a request if a hash is passed into a method' do
|
92
|
+
subject.item(foo: 'bar')
|
93
|
+
|
94
|
+
a_request(:get, 'http://test.com/item').
|
95
|
+
with(query: hash_including({'foo' => 'bar'})).
|
96
|
+
should have_been_made
|
97
|
+
end
|
98
|
+
|
91
99
|
it 'should parse the _method parameter as the HTTP request type' do
|
92
100
|
subject.item[_method: :post]._fetch
|
93
101
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chain
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stefan Novak
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-11-
|
11
|
+
date: 2013-11-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -109,8 +109,11 @@ files:
|
|
109
109
|
- chain.gemspec
|
110
110
|
- lib/chain.rb
|
111
111
|
- lib/chain/middleware/hashie_mash_response.rb
|
112
|
+
- lib/chain/middleware/parse_error.rb
|
113
|
+
- lib/chain/middleware/request_error.rb
|
112
114
|
- lib/chain/url.rb
|
113
115
|
- lib/chain/version.rb
|
116
|
+
- spec/middleware/hasie_mash_response_spec.rb
|
114
117
|
- spec/spec_helper.rb
|
115
118
|
- spec/url_spec.rb
|
116
119
|
homepage: https://github.com/slnovak/chain
|
@@ -138,5 +141,6 @@ signing_key:
|
|
138
141
|
specification_version: 4
|
139
142
|
summary: Access API endpoints via method chaining in Ruby.
|
140
143
|
test_files:
|
144
|
+
- spec/middleware/hasie_mash_response_spec.rb
|
141
145
|
- spec/spec_helper.rb
|
142
146
|
- spec/url_spec.rb
|