api-resource 0.3.1 → 0.3.5
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/lib/api-resource/resource.rb +13 -5
- data/lib/api-resource/version.rb +1 -1
- data/spec/resource_spec.rb +66 -92
- 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: d12c1f0d2bfa63f06021846af93ce43caa9b513c
|
4
|
+
data.tar.gz: 988a25712ce86f55beff42464da754d2d368648f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6e3d9f2c3dc5cb3a22e15f434dbfb600b6c019a7d8ca3e0b3eaf24ea6245826b989908d2dee3923d1c7b81530f2ae55ecf9101caf856089c8ee31291e750e813
|
7
|
+
data.tar.gz: 9a80b9bf3a460a9a4a8370d428abf75534aac82bfddac9312abd487a95132a0f96851badb696b4e566e220c34c9266829f6fcc7418e735f01a0c6b6fee0ccc25
|
@@ -34,7 +34,7 @@ module ApiResource
|
|
34
34
|
def self.find(id)
|
35
35
|
result = client(:get, {}, resource_name, id)
|
36
36
|
json = JSON.parse(result)
|
37
|
-
self.new(json['data'])
|
37
|
+
self.new(json['data'] || json)
|
38
38
|
end
|
39
39
|
|
40
40
|
def self.all
|
@@ -60,15 +60,23 @@ module ApiResource
|
|
60
60
|
send("#{key}=", value)
|
61
61
|
else
|
62
62
|
# check for embedded resource
|
63
|
-
|
64
|
-
|
63
|
+
if value.is_a?(Hash)
|
64
|
+
meta = value['meta']
|
65
|
+
data = value['data'] || value
|
66
|
+
elsif value.is_a?(Array)
|
67
|
+
meta = nil
|
68
|
+
data = value
|
69
|
+
else
|
70
|
+
next
|
71
|
+
end
|
72
|
+
puts meta
|
73
|
+
child_class = self.class.load_class((meta && meta['type']) || key)
|
65
74
|
next unless child_class
|
66
|
-
data = value['data']
|
67
75
|
if data.is_a?(Hash)
|
68
76
|
child_value = child_class.new(data)
|
69
77
|
define_singleton_method(key) { child_value }
|
70
78
|
elsif data.is_a?(Array)
|
71
|
-
child_values = ResourceCollection.new(data,
|
79
|
+
child_values = ResourceCollection.new(data, meta, child_class)
|
72
80
|
define_singleton_method(key) { child_values }
|
73
81
|
end
|
74
82
|
end
|
data/lib/api-resource/version.rb
CHANGED
data/spec/resource_spec.rb
CHANGED
@@ -19,12 +19,18 @@ RSpec.describe ApiResource::Resource do
|
|
19
19
|
def check_returned_array(type, expected, actual)
|
20
20
|
expect(actual).to be_a Enumerable
|
21
21
|
actual.each { |e| expect(e).to be_a type }
|
22
|
-
expect(actual.map { |e| e.attributes }).to match (expected[:data])
|
22
|
+
expect(actual.map { |e| e.attributes }).to match ((expected.is_a?(Hash) && expected[:data]) || expected)
|
23
23
|
end
|
24
24
|
|
25
25
|
def check_returned_object(type, expected, actual, *except)
|
26
26
|
expect(actual).to be_a(type)
|
27
|
-
expect(actual.attributes).to match(expected[:data].except(*except))
|
27
|
+
expect(actual.attributes).to match((expected[:data] || expected).except(*except))
|
28
|
+
end
|
29
|
+
|
30
|
+
def req(verb, path, resource)
|
31
|
+
stub_request(verb, "#{BlogResource.base_url}#{path}").
|
32
|
+
with(headers: { 'Accept' => 'application/json' }).
|
33
|
+
to_return(status: 200, body: resource.to_json)
|
28
34
|
end
|
29
35
|
|
30
36
|
before do
|
@@ -33,25 +39,26 @@ RSpec.describe ApiResource::Resource do
|
|
33
39
|
{ id: 38, title: 'Bonjour!' }] }
|
34
40
|
end
|
35
41
|
context '#find' do
|
36
|
-
|
37
|
-
|
38
|
-
expected_value = { data: { id: 1257, title: 'Hello' } }
|
42
|
+
def check_found_resource(expected_value)
|
43
|
+
expected_value = { id: 1257, title: 'Hello' }
|
39
44
|
resource_id = 3
|
40
|
-
|
41
|
-
with(headers: { 'Accept' => 'application/json' }).
|
42
|
-
to_return(status: 200, body: expected_value.to_json)
|
45
|
+
req(:get, "/blogs/#{resource_id}", expected_value)
|
43
46
|
result = Blog.find(resource_id)
|
44
47
|
check_returned_object(Blog, expected_value, result)
|
45
48
|
end
|
49
|
+
|
50
|
+
it 'creates resource with data rooted json' do
|
51
|
+
check_found_resource(data: { id: 1257, title: 'Hello' })
|
52
|
+
end
|
53
|
+
it 'creates resource with un-rooted json' do
|
54
|
+
check_found_resource(id: 1257, title: 'Hello')
|
55
|
+
end
|
46
56
|
end
|
47
57
|
|
48
58
|
context '#all' do
|
49
59
|
|
50
60
|
it 'fetches all resources' do
|
51
|
-
|
52
|
-
with(headers: { 'Accept' => 'application/json' }).
|
53
|
-
to_return(status: 200, body: @expected_resources.to_json)
|
54
|
-
|
61
|
+
req(:get, '/blogs', @expected_resources)
|
55
62
|
result = Blog.all
|
56
63
|
check_returned_array(Blog, @expected_resources, result)
|
57
64
|
end
|
@@ -60,10 +67,7 @@ RSpec.describe ApiResource::Resource do
|
|
60
67
|
context '#where' do
|
61
68
|
|
62
69
|
it 'fetches resourced filtered by query string with GET' do
|
63
|
-
|
64
|
-
with(headers: { 'Accept' => 'application/json' }).
|
65
|
-
to_return(status: 200, body: @expected_resources.to_json)
|
66
|
-
|
70
|
+
req(:get, '/blogs?title=hello+world&tags%5B%5D=hello&tags%5B%5D=first&tags%5B%5D=greeting', @expected_resources)
|
67
71
|
result = Blog.where(title: 'hello world', tags: ['hello', 'first', 'greeting'])
|
68
72
|
check_returned_array(Blog, @expected_resources, result)
|
69
73
|
end
|
@@ -83,97 +87,67 @@ RSpec.describe ApiResource::Resource do
|
|
83
87
|
|
84
88
|
it 'fetches resource nested with parent resource' do
|
85
89
|
resource_id = 3
|
86
|
-
|
87
|
-
stub_request(:get, "#{Blog.base_url}/tags/#{resource_id}/blogs").
|
88
|
-
with(headers: { 'Accept' => 'application/json' }).
|
89
|
-
to_return(status: 200, body: @expected_resources.to_json)
|
90
|
-
|
90
|
+
req(:get, "/tags/#{resource_id}/blogs", @expected_resources)
|
91
91
|
result = Blog.by_tag(resource_id)
|
92
92
|
check_returned_array(Blog, @expected_resources, result)
|
93
93
|
end
|
94
94
|
end
|
95
95
|
|
96
96
|
context '#child_ressources' do
|
97
|
+
before do
|
98
|
+
@blog = { data: { id: 1257, title: 'Tarte a la creme' } }
|
99
|
+
@tags_data = [{ id: 33, name: 'food' }, { id: 39, name: 'cuisine' }]
|
100
|
+
@category_data = { id: 33, name: 'Hobbies' }
|
101
|
+
end
|
97
102
|
context 'has_many' do
|
98
|
-
|
99
|
-
resource_values =
|
100
|
-
id: 1257, title: 'Tarte a la creme',
|
101
|
-
tags: { data: [{ id: 33, name: 'food' }, { id: 39, name: 'cuisine' }] }
|
102
|
-
} }
|
103
|
+
def check_has_many(relation_name, relation_values)
|
104
|
+
resource_values = @blog.merge(data: { relation_name => relation_values })
|
103
105
|
resource_id = 3
|
104
|
-
|
105
|
-
stub_request(:get, "#{Blog.base_url}/blogs/#{resource_id}").
|
106
|
-
with(headers: { 'Accept' => 'application/json' }).
|
107
|
-
to_return(status: 200, body: resource_values.to_json)
|
108
|
-
|
106
|
+
req(:get, "/blogs/#{resource_id}", resource_values)
|
109
107
|
blog = Blog.find(resource_id)
|
110
|
-
check_returned_object(Blog, resource_values, blog,
|
111
|
-
|
108
|
+
check_returned_object(Blog, resource_values, blog, relation_name)
|
109
|
+
expect(blog).to respond_to(relation_name)
|
110
|
+
check_returned_array(Tag, relation_values, blog.send(relation_name))
|
111
|
+
yield blog if block_given?
|
112
112
|
end
|
113
113
|
|
114
|
-
it '
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
my_sweet_tags: {
|
120
|
-
data: [{ id: 33, name: 'food' }, { id: 39, name: 'cuisine' }],
|
121
|
-
meta: { type: :tags, page: 3, per_page: 50, total: 102 }
|
122
|
-
}
|
123
|
-
}
|
124
|
-
}
|
125
|
-
resource_id = 3
|
126
|
-
|
127
|
-
stub_request(:get, "#{Blog.base_url}/blogs/#{resource_id}").
|
128
|
-
with(headers: { 'Accept' => 'application/json' }).
|
129
|
-
to_return(status: 200, body: resource_values.to_json)
|
130
|
-
|
131
|
-
blog = Blog.find(resource_id)
|
132
|
-
check_returned_object(Blog, resource_values, blog, :my_sweet_tags)
|
133
|
-
check_returned_array(Tag, resource_values[:data][:my_sweet_tags], blog.my_sweet_tags)
|
134
|
-
|
135
|
-
expect(blog.my_sweet_tags.page).to eq(3)
|
136
|
-
expect(blog.my_sweet_tags.per_page).to eq(50)
|
137
|
-
expect(blog.my_sweet_tags.total).to eq(102)
|
114
|
+
it 'gets data rooted inlined array' do
|
115
|
+
check_has_many(:tags, data: @tags_data)
|
116
|
+
end
|
117
|
+
it 'gets un-rooted inlined array' do
|
118
|
+
check_has_many(:tags, @tags_data)
|
138
119
|
end
|
139
|
-
end
|
140
|
-
|
141
|
-
it 'has_one association with inlined json' do
|
142
|
-
resource_values = { data: {
|
143
|
-
id: 1257, title: 'Tarte a la creme',
|
144
|
-
category: { data: { id: 33, name: 'Hobbies' } }
|
145
|
-
} }
|
146
|
-
resource_id = 3
|
147
|
-
|
148
|
-
stub_request(:get, "#{Blog.base_url}/blogs/#{resource_id}").
|
149
|
-
with(headers: { 'Accept' => 'application/json' }).
|
150
|
-
to_return(status: 200, body: resource_values.to_json)
|
151
120
|
|
152
|
-
|
153
|
-
|
154
|
-
|
121
|
+
it 'override type with meta.type and retrieve other meta.* info' do
|
122
|
+
check_has_many(:my_sweet_tags,
|
123
|
+
data: [{ id: 33, name: 'food' }, { id: 39, name: 'cuisine' }],
|
124
|
+
meta: { type: :tags, page: 3, per_page: 50, total: 102 }) do |blog|
|
125
|
+
expect(blog.my_sweet_tags.page).to eq(3)
|
126
|
+
expect(blog.my_sweet_tags.per_page).to eq(50)
|
127
|
+
expect(blog.my_sweet_tags.total).to eq(102)
|
128
|
+
end
|
129
|
+
end
|
155
130
|
end
|
131
|
+
context 'has_one' do
|
132
|
+
def check_has_one(relation_name, relation_values)
|
133
|
+
resource_values = @blog.merge(data: { relation_name => relation_values })
|
134
|
+
resource_id = 3
|
135
|
+
req(:get, "/blogs/#{resource_id}", resource_values)
|
136
|
+
blog = Blog.find(resource_id)
|
137
|
+
check_returned_object(Blog, resource_values, blog, relation_name)
|
138
|
+
expect(blog).to respond_to(relation_name)
|
139
|
+
check_returned_object(Category, relation_values, blog.send(relation_name))
|
140
|
+
end
|
156
141
|
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
}
|
167
|
-
}
|
168
|
-
resource_id = 3
|
169
|
-
|
170
|
-
stub_request(:get, "#{Blog.base_url}/blogs/#{resource_id}").
|
171
|
-
with(headers: { 'Accept' => 'application/json' }).
|
172
|
-
to_return(status: 200, body: resource_values.to_json)
|
173
|
-
|
174
|
-
blog = Blog.find(resource_id)
|
175
|
-
check_returned_object(Blog, resource_values, blog, :main_category)
|
176
|
-
check_returned_object(Category, resource_values[:data][:main_category], blog.main_category)
|
142
|
+
it 'gets data rooted inlined json' do
|
143
|
+
check_has_one(:category, data: @category_data)
|
144
|
+
end
|
145
|
+
it 'gets un-rooted inlined json' do
|
146
|
+
check_has_one(:category, @category_data)
|
147
|
+
end
|
148
|
+
it 'override type from meta' do
|
149
|
+
check_has_one(:main_category, data: { id: 33, name: 'Hobbies' }, meta: { type: :category })
|
150
|
+
end
|
177
151
|
end
|
178
152
|
end
|
179
153
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: api-resource
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chaker Nakhli
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: simple-hmac
|