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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3d7d95d480605f33ce43a9218cbe26cef96f73e7
4
- data.tar.gz: 49eb448b3c170f71b93d0f987012df51250071f7
3
+ metadata.gz: c94f2b66c8f85cf256adbf1dfd4a3725e4ac22ab
4
+ data.tar.gz: c83649ded772f0f02b420d2021cfb1cce13f6ec5
5
5
  SHA512:
6
- metadata.gz: 0b3e9784c55ba0d165b23b40f3d8ff5afa4be9861d09cee0ff2698c5442eccc7a910e15d1ec3c2d55fc17ad470476d36793dfcce35b2747ae0904ab6b9471721
7
- data.tar.gz: c64ca14dbde963cf0e949b3ff75235119cf83a43c82e0efd6ffd2bf5f1e042bcf035344246398f172591d4acd230885cc24ab0f241c164af5ad9beb5757e98ed
6
+ metadata.gz: a27cd7390f3c73ac46689f45624a295f8511aa05393e656817ad20db76cea717a522c6bdf75a3b9ee2c8b48e774da75c85541dc096168eb2ea0daf0eb403d3b1
7
+ data.tar.gz: 3799f90cc7cae5f731689caf61765b2d060b39716c6c2bb5d972ec48f190fbf582597caa1b15cfdb61f7b16db4d89daf2f0a9a1d911f677d14b0fbd6afdb0c30
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- api-model (2.5.1)
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.1)
16
- activesupport (= 4.2.1)
15
+ activemodel (4.2.2)
16
+ activesupport (= 4.2.2)
17
17
  builder (~> 3.1)
18
- activesupport (4.2.1)
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.3)
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.2)
50
+ json (1.8.3)
51
51
  method_source (0.8.2)
52
- minitest (5.6.1)
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.1)
69
- ethon (>= 0.7.1)
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
 
@@ -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.1"
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']
@@ -35,7 +35,11 @@ module ApiModel
35
35
 
36
36
  def build(builder, hash)
37
37
  if builder.respond_to? :build
38
- builder.build hash
38
+ if builder.method(:build).arity == 2
39
+ builder.build self, hash
40
+ else
41
+ builder.build hash
42
+ end
39
43
  else
40
44
  builder.new hash
41
45
  end
@@ -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
@@ -8,4 +8,11 @@ class BlogPost < ApiModel::Base
8
8
  end
9
9
  end
10
10
 
11
+ class AdvancedCustomBuilder
12
+ def build(response, hash)
13
+ response.metadata.custom_attr = "Hello"
14
+ BlogPost.new hash
15
+ end
16
+ end
17
+
11
18
  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.5.1
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-05-11 00:00:00.000000000 Z
12
+ date: 2015-07-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport