api-model 2.5.1 → 2.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +9 -9
- data/README.md +22 -0
- data/api-model.gemspec +1 -1
- data/lib/api_model/response.rb +5 -1
- data/spec/api-model/api_model_spec.rb +14 -0
- data/spec/support/mock_models/blog_post.rb +7 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c94f2b66c8f85cf256adbf1dfd4a3725e4ac22ab
|
4
|
+
data.tar.gz: c83649ded772f0f02b420d2021cfb1cce13f6ec5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a27cd7390f3c73ac46689f45624a295f8511aa05393e656817ad20db76cea717a522c6bdf75a3b9ee2c8b48e774da75c85541dc096168eb2ea0daf0eb403d3b1
|
7
|
+
data.tar.gz: 3799f90cc7cae5f731689caf61765b2d060b39716c6c2bb5d972ec48f190fbf582597caa1b15cfdb61f7b16db4d89daf2f0a9a1d911f677d14b0fbd6afdb0c30
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
api-model (2.
|
4
|
+
api-model (2.6.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.
|
16
|
-
activesupport (= 4.2.
|
15
|
+
activemodel (4.2.2)
|
16
|
+
activesupport (= 4.2.2)
|
17
17
|
builder (~> 3.1)
|
18
|
-
activesupport (4.2.
|
18
|
+
activesupport (4.2.2)
|
19
19
|
i18n (~> 0.7)
|
20
20
|
json (~> 1.7, >= 1.7.7)
|
21
21
|
minitest (~> 5.1)
|
@@ -38,7 +38,7 @@ GEM
|
|
38
38
|
domain_name (0.5.24)
|
39
39
|
unf (>= 0.0.5, < 1.0.0)
|
40
40
|
equalizer (0.0.11)
|
41
|
-
ethon (0.7.
|
41
|
+
ethon (0.7.4)
|
42
42
|
ffi (>= 1.3.0)
|
43
43
|
ffi (1.9.8)
|
44
44
|
hash-pipe (0.3.0)
|
@@ -47,9 +47,9 @@ GEM
|
|
47
47
|
domain_name (~> 0.5)
|
48
48
|
i18n (0.7.0)
|
49
49
|
ice_nine (0.11.1)
|
50
|
-
json (1.8.
|
50
|
+
json (1.8.3)
|
51
51
|
method_source (0.8.2)
|
52
|
-
minitest (5.
|
52
|
+
minitest (5.7.0)
|
53
53
|
pry (0.9.12.2)
|
54
54
|
coderay (~> 1.0.5)
|
55
55
|
method_source (~> 0.8)
|
@@ -65,8 +65,8 @@ GEM
|
|
65
65
|
safe_yaml (0.9.7)
|
66
66
|
slop (3.4.6)
|
67
67
|
thread_safe (0.3.5)
|
68
|
-
typhoeus (0.7.
|
69
|
-
ethon (>= 0.7.
|
68
|
+
typhoeus (0.7.2)
|
69
|
+
ethon (>= 0.7.4)
|
70
70
|
tzinfo (1.2.2)
|
71
71
|
thread_safe (~> 0.1)
|
72
72
|
unf (0.1.4)
|
data/README.md
CHANGED
@@ -95,6 +95,22 @@ use as a builder should respond to `#build`, with the instance hash as an argume
|
|
95
95
|
MyModel.get_json "/foo", { some_param: "bar" }, builder: MyCustomBuilder.new
|
96
96
|
```
|
97
97
|
|
98
|
+
It is also possible to create slightly more advanced builders which can access the entire response object,
|
99
|
+
so that they can modify the return result more directly, or add to the `metadata` object on the response.
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
class MyCustomBuilder
|
103
|
+
def build(response, hash)
|
104
|
+
response.metadata.pagination = hash["pagination"]
|
105
|
+
MyModel.new name: hash["name"]
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
result = MyModel.get_json "/foo", { some_param: "bar" }, builder: MyCustomBuilder.new
|
110
|
+
result.name # => whatever was in hash["name"]
|
111
|
+
result.metadata # => OpenStruct of values
|
112
|
+
```
|
113
|
+
|
98
114
|
Handling validation errors in responses
|
99
115
|
---------------------------------------
|
100
116
|
|
@@ -113,6 +129,12 @@ ActiveModel validations, you can do that, too:
|
|
113
129
|
car.errors[:name] # => ["cannot be blank"]
|
114
130
|
```
|
115
131
|
|
132
|
+
Metadata
|
133
|
+
--------
|
134
|
+
|
135
|
+
Metadata can be stored directly on the response object, even when it contains an array. By using custom builders,
|
136
|
+
you can leverage this to store useful data such as pagination metadata (see the builders section for an example).
|
137
|
+
|
116
138
|
Configuring API Model
|
117
139
|
---------------------
|
118
140
|
|
data/api-model.gemspec
CHANGED
@@ -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.
|
5
|
+
s.version = "2.6.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']
|
data/lib/api_model/response.rb
CHANGED
@@ -65,6 +65,20 @@ describe ApiModel do
|
|
65
65
|
custom_built_blog_post.title.should eq "FOOBAR"
|
66
66
|
end
|
67
67
|
end
|
68
|
+
|
69
|
+
describe "with an advanced custom builder" do
|
70
|
+
let(:custom_built_blog_post) do
|
71
|
+
VCR.use_cassette('posts') do
|
72
|
+
BlogPost.get_json "http://api-model-specs.com/single_post", {}, builder: BlogPost::AdvancedCustomBuilder.new
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should use the response object when building" do
|
77
|
+
custom_built_blog_post.should be_a(BlogPost)
|
78
|
+
custom_built_blog_post.metadata.custom_attr.should eq "Hello"
|
79
|
+
custom_built_blog_post.name.should eq "foo"
|
80
|
+
end
|
81
|
+
end
|
68
82
|
end
|
69
83
|
|
70
84
|
describe "using Virtus to build with attribute coercion" do
|
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.
|
4
|
+
version: 2.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Damien Timewell
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-
|
12
|
+
date: 2015-07-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|