api-model 2.6.0 → 2.7.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: c94f2b66c8f85cf256adbf1dfd4a3725e4ac22ab
4
- data.tar.gz: c83649ded772f0f02b420d2021cfb1cce13f6ec5
3
+ metadata.gz: 7d0dd9a26e6eafcc5cfde5f0f7efe1fb7f1ef3e4
4
+ data.tar.gz: efea549d31e9bc395b67f02f02d0bd9e2761e191
5
5
  SHA512:
6
- metadata.gz: a27cd7390f3c73ac46689f45624a295f8511aa05393e656817ad20db76cea717a522c6bdf75a3b9ee2c8b48e774da75c85541dc096168eb2ea0daf0eb403d3b1
7
- data.tar.gz: 3799f90cc7cae5f731689caf61765b2d060b39716c6c2bb5d972ec48f190fbf582597caa1b15cfdb61f7b16db4d89daf2f0a9a1d911f677d14b0fbd6afdb0c30
6
+ metadata.gz: 0151bb44bddbf6764c40719142841857b1ee27d5dd0ce2a67f7dccb541862c54129eef6b035d1a501a13909ac9eb151c3e2efb62a3b08eba11c4882d50a91c1d
7
+ data.tar.gz: 0d2abb36cbc68bdc02555f99fb8ae9435fc4822b2c76d9ccd682974f91d461a17b1d02f2a90339830718a27e148cd4bfe19b2565ef942268729dcd9bd01c189a
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- api-model (2.6.0)
4
+ api-model (2.7.0)
5
5
  activemodel (~> 4.1)
6
6
  activesupport (~> 4.1)
7
7
  hash-pipe (~> 0.0)
@@ -12,10 +12,10 @@ PATH
12
12
  GEM
13
13
  remote: https://rubygems.org/
14
14
  specs:
15
- activemodel (4.2.2)
16
- activesupport (= 4.2.2)
15
+ activemodel (4.2.3)
16
+ activesupport (= 4.2.3)
17
17
  builder (~> 3.1)
18
- activesupport (4.2.2)
18
+ activesupport (4.2.3)
19
19
  i18n (~> 0.7)
20
20
  json (~> 1.7, >= 1.7.7)
21
21
  minitest (~> 5.1)
@@ -40,7 +40,7 @@ GEM
40
40
  equalizer (0.0.11)
41
41
  ethon (0.7.4)
42
42
  ffi (>= 1.3.0)
43
- ffi (1.9.8)
43
+ ffi (1.9.10)
44
44
  hash-pipe (0.3.0)
45
45
  activesupport (~> 4.1)
46
46
  http-cookie (1.0.2)
data/README.md CHANGED
@@ -192,6 +192,9 @@ an API which returns something other than JSON, you can set custom parsers to de
192
192
  before they are sent to builder classes. The parser should work in the same way as a custom builder, except it needs
193
193
  to respond to `#parse`, with the raw response body as an argument.
194
194
 
195
+ Parsers can also access the raw `response` object, like builders, by using a #parse method which takes two
196
+ arguments.
197
+
195
198
  ### Raising exceptions
196
199
 
197
200
  ```ruby
@@ -2,7 +2,7 @@ $:.push File.expand_path("../lib", __FILE__)
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "api-model"
5
- s.version = "2.6.0"
5
+ s.version = "2.7.0"
6
6
  s.authors = ["Damien Timewell", "Erik Rothoff Andersson"]
7
7
  s.email = ["mail@damientimewell.com", "erik.rothoff@gmail.com"]
8
8
  s.licenses = ['MIT']
@@ -46,7 +46,13 @@ module ApiModel
46
46
  end
47
47
 
48
48
  def response_body
49
- @response_body ||= @_config.parser.parse http_response.api_call.body
49
+ return @response_body if @response_body.present?
50
+
51
+ if @_config.parser.method(:parse).arity == 2
52
+ @response_body = @_config.parser.parse self, http_response.api_call.body
53
+ else
54
+ @response_body = @_config.parser.parse http_response.api_call.body
55
+ end
50
56
  end
51
57
 
52
58
  def successful?
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+ require 'support/mock_models/blog_post'
3
+
4
+ describe "Parsing" do
5
+
6
+ after :each do
7
+ BlogPost.reset_api_configuration
8
+ end
9
+
10
+ describe "Default parser" do
11
+ it "should produce a hash given valid json" do
12
+ ApiModel::ResponseParser::Json.new.parse("{\"name\":\"foo\"}")["name"].should eq "foo"
13
+ end
14
+
15
+ it "should catch errors from parsing invalid json" do
16
+ ApiModel::Log.should_receive(:info).with "Could not parse JSON response: blah"
17
+
18
+ expect {
19
+ ApiModel::ResponseParser::Json.new.parse("blah")
20
+ }.to_not raise_error
21
+ end
22
+ end
23
+
24
+ describe "Parser with response handling" do
25
+ class CustomResponseParser
26
+ def parse(response, body)
27
+ response.metadata.foobar = "Baz"
28
+ { name: "A blog post" }
29
+ end
30
+ end
31
+
32
+ before do
33
+ BlogPost.api_config do |config|
34
+ config.parser = CustomResponseParser.new
35
+ end
36
+ end
37
+
38
+ it "should use the response object" do
39
+ VCR.use_cassette('posts') do
40
+ res = BlogPost.get_json "http://api-model-specs.com/single_post"
41
+ res.metadata.foobar.should eq "Baz"
42
+ res.name.should eq "A blog post"
43
+ end
44
+ end
45
+ end
46
+
47
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.6.0
4
+ version: 2.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Damien Timewell
@@ -183,7 +183,7 @@ files:
183
183
  - spec/api-model/configuration_spec.rb
184
184
  - spec/api-model/http_request_spec.rb
185
185
  - spec/api-model/initializer_spec.rb
186
- - spec/api-model/json_response_parser_spec.rb
186
+ - spec/api-model/parser_spec.rb
187
187
  - spec/api-model/response_spec.rb
188
188
  - spec/spec_helper.rb
189
189
  - spec/support/fixtures/cars.yml
@@ -226,7 +226,7 @@ test_files:
226
226
  - spec/api-model/configuration_spec.rb
227
227
  - spec/api-model/http_request_spec.rb
228
228
  - spec/api-model/initializer_spec.rb
229
- - spec/api-model/json_response_parser_spec.rb
229
+ - spec/api-model/parser_spec.rb
230
230
  - spec/api-model/response_spec.rb
231
231
  - spec/spec_helper.rb
232
232
  - spec/support/fixtures/cars.yml
@@ -1,17 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe ApiModel::ResponseParser::Json do
4
-
5
- it "should produce a hash given valid json" do
6
- ApiModel::ResponseParser::Json.new.parse("{\"name\":\"foo\"}")["name"].should eq "foo"
7
- end
8
-
9
- it "should catch errors from parsing invalid json" do
10
- ApiModel::Log.should_receive(:info).with "Could not parse JSON response: blah"
11
-
12
- expect {
13
- ApiModel::ResponseParser::Json.new.parse("blah")
14
- }.to_not raise_error
15
- end
16
-
17
- end