api-resource 0.7.7 → 0.7.8
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 +2 -1
- data/lib/api-resource/version.rb +1 -1
- data/spec/aggregate_resource_spec.rb +96 -0
- data/spec/resource_spec.rb +0 -74
- data/spec/spec_helper.rb +3 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 158646a91a4a3110f1b329e23671282ba974c01c
|
4
|
+
data.tar.gz: 708e375cf7e7ee164b949e44d5569e5161ee6c71
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e23c1f41326346e04cbc90dfec95428a686bc002a80cc31d89ddd18727b0de02774e14ffa4a38c0bc27cc7b84eaf5ca751f07b495eb8c1bc23518149241fcc36
|
7
|
+
data.tar.gz: 7a50f726da7adec4512d69ae9bfaae9d0b63c06647af9307d0e62adbdcb8773ecb5e6b257b606dd81df859edc73643be26c377427199fdc133a5a8d0868e51f2
|
@@ -202,7 +202,8 @@ module ApiResource
|
|
202
202
|
end
|
203
203
|
|
204
204
|
def self.load_class(tableized_class)
|
205
|
-
|
205
|
+
my_name = name || ancestors.map(&:name).find {|e| e}
|
206
|
+
tableized_class = my_name.to_s.tableize.split('/')[0...-1].push(tableized_class).join('/')
|
206
207
|
klass = tableized_class && tableized_class.to_s.singularize.classify.constantize rescue nil
|
207
208
|
klass if klass && klass < ApiResource::Resource
|
208
209
|
end
|
data/lib/api-resource/version.rb
CHANGED
@@ -0,0 +1,96 @@
|
|
1
|
+
require_relative 'blog_api'
|
2
|
+
|
3
|
+
RSpec.describe ApiResource::Resource do
|
4
|
+
before do
|
5
|
+
@blog = { data: { id: 1257, title: 'Tarte a la creme' } }
|
6
|
+
@tags_data = [{ id: 33, name: 'food' }, { id: 39, name: 'cuisine' }]
|
7
|
+
@category_data = { id: 33, name: 'Hobbies' }
|
8
|
+
end
|
9
|
+
|
10
|
+
context 'has_many' do
|
11
|
+
def check_has_many(relation_name, relation_values, aggregate_class=BlogApi::Blog, component_class=BlogApi::Tag)
|
12
|
+
resource_values = @blog.merge(data: { relation_name => relation_values })
|
13
|
+
resource_id = 3
|
14
|
+
req(:get, "/#{aggregate_class.resource_path}/#{resource_id}", resource_values)
|
15
|
+
blog = aggregate_class.find(resource_id)
|
16
|
+
check_returned_object(aggregate_class, resource_values, blog, relation_name)
|
17
|
+
expect(blog).to respond_to(relation_name)
|
18
|
+
check_returned_array(component_class, relation_values, blog.send(relation_name))
|
19
|
+
yield blog if block_given?
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'guess type from field name for `data` rooted array of data' do
|
23
|
+
check_has_many(:tags, data: @tags_data)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'guess type from field name for plain array of data' do
|
27
|
+
check_has_many(:tags, @tags_data)
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'override type with meta.type and retrieve other meta.* info' do
|
31
|
+
check_has_many(:my_sweet_tags,
|
32
|
+
data: [{ id: 33, name: 'food' }, { id: 39, name: 'cuisine' }],
|
33
|
+
meta: { type: :tags, page: 3, per_page: 50, total: 102 }) do |blog|
|
34
|
+
expect(blog.my_sweet_tags.page).to eq(3)
|
35
|
+
expect(blog.my_sweet_tags.per_page).to eq(50)
|
36
|
+
expect(blog.my_sweet_tags.total).to eq(102)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context '::by_parent called on aggregate class' do
|
41
|
+
it 'guess type from field name for `data` rooted array of data' do
|
42
|
+
check_has_many(:tags, { data: @tags_data }, BlogApi::Blog.by_author('Flinn'))
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'guess type from field name for plain array of data' do
|
46
|
+
check_has_many(:tags, @tags_data, BlogApi::Blog.by_author('Flinn'))
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'override type with meta.type and retrieve other meta.* info' do
|
50
|
+
check_has_many(:my_sweet_tags,
|
51
|
+
{ data: [{ id: 33, name: 'food' }, { id: 39, name: 'cuisine' }],
|
52
|
+
meta: { type: :tags, page: 3, per_page: 50, total: 102 } },
|
53
|
+
BlogApi::Blog.by_author('Flinn')) do |blog|
|
54
|
+
expect(blog.my_sweet_tags.page).to eq(3)
|
55
|
+
expect(blog.my_sweet_tags.per_page).to eq(50)
|
56
|
+
expect(blog.my_sweet_tags.total).to eq(102)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context 'has_one' do
|
63
|
+
let(:raw_data) { { 'foo' => 'bar', 'baz' => 3 } }
|
64
|
+
|
65
|
+
def check_has_one(relation_name, relation_values, relation_type=BlogApi::Category)
|
66
|
+
@blog[:data].merge!(relation_name => relation_values)
|
67
|
+
resource_id = 3
|
68
|
+
stub = req(:get, "/blogs/#{resource_id}", @blog)
|
69
|
+
blog = BlogApi::Blog.find(resource_id)
|
70
|
+
check_returned_object(BlogApi::Blog, @blog, blog, relation_name)
|
71
|
+
expect(blog).to respond_to(relation_name)
|
72
|
+
check_returned_object(relation_type, relation_values, blog.send(relation_name))
|
73
|
+
expect(stub).to have_been_requested
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'gets data rooted inlined json' do
|
77
|
+
check_has_one(:category, data: @category_data)
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'gets un-rooted inlined json' do
|
81
|
+
check_has_one(:category, @category_data)
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'override type from meta' do
|
85
|
+
check_has_one(:main_category, data: { id: 33, name: 'Hobbies' }, meta: { type: :category })
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'creates method for raw types' do
|
89
|
+
check_has_one(:raw_attr, raw_data, Hash)
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'replaces method with raw types' do
|
93
|
+
check_has_one(:do_stuff_method, raw_data, Hash)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
data/spec/resource_spec.rb
CHANGED
@@ -84,77 +84,6 @@ RSpec.describe ApiResource::Resource do
|
|
84
84
|
end
|
85
85
|
end
|
86
86
|
|
87
|
-
context '#child_ressources' do
|
88
|
-
before do
|
89
|
-
@blog = { data: { id: 1257, title: 'Tarte a la creme' } }
|
90
|
-
@tags_data = [{ id: 33, name: 'food' }, { id: 39, name: 'cuisine' }]
|
91
|
-
@category_data = { id: 33, name: 'Hobbies' }
|
92
|
-
end
|
93
|
-
context 'has_many' do
|
94
|
-
def check_has_many(relation_name, relation_values)
|
95
|
-
resource_values = @blog.merge(data: { relation_name => relation_values })
|
96
|
-
resource_id = 3
|
97
|
-
req(:get, "/blogs/#{resource_id}", resource_values)
|
98
|
-
blog = BlogApi::Blog.find(resource_id)
|
99
|
-
check_returned_object(BlogApi::Blog, resource_values, blog, relation_name)
|
100
|
-
expect(blog).to respond_to(relation_name)
|
101
|
-
check_returned_array(BlogApi::Tag, relation_values, blog.send(relation_name))
|
102
|
-
yield blog if block_given?
|
103
|
-
end
|
104
|
-
|
105
|
-
it 'gets data rooted inlined array' do
|
106
|
-
check_has_many(:tags, data: @tags_data)
|
107
|
-
end
|
108
|
-
it 'gets un-rooted inlined array' do
|
109
|
-
check_has_many(:tags, @tags_data)
|
110
|
-
end
|
111
|
-
|
112
|
-
it 'override type with meta.type and retrieve other meta.* info' do
|
113
|
-
check_has_many(:my_sweet_tags,
|
114
|
-
data: [{ id: 33, name: 'food' }, { id: 39, name: 'cuisine' }],
|
115
|
-
meta: { type: :tags, page: 3, per_page: 50, total: 102 }) do |blog|
|
116
|
-
expect(blog.my_sweet_tags.page).to eq(3)
|
117
|
-
expect(blog.my_sweet_tags.per_page).to eq(50)
|
118
|
-
expect(blog.my_sweet_tags.total).to eq(102)
|
119
|
-
end
|
120
|
-
end
|
121
|
-
end
|
122
|
-
context 'has_one' do
|
123
|
-
let(:raw_data) { { 'foo' => 'bar', 'baz' => 3 } }
|
124
|
-
|
125
|
-
def check_has_one(relation_name, relation_values, relation_type=BlogApi::Category)
|
126
|
-
@blog[:data].merge!(relation_name => relation_values)
|
127
|
-
resource_id = 3
|
128
|
-
stub = req(:get, "/blogs/#{resource_id}", @blog)
|
129
|
-
blog = BlogApi::Blog.find(resource_id)
|
130
|
-
check_returned_object(BlogApi::Blog, @blog, blog, relation_name)
|
131
|
-
expect(blog).to respond_to(relation_name)
|
132
|
-
check_returned_object(relation_type, relation_values, blog.send(relation_name))
|
133
|
-
expect(stub).to have_been_requested
|
134
|
-
end
|
135
|
-
|
136
|
-
it 'gets data rooted inlined json' do
|
137
|
-
check_has_one(:category, data: @category_data)
|
138
|
-
end
|
139
|
-
|
140
|
-
it 'gets un-rooted inlined json' do
|
141
|
-
check_has_one(:category, @category_data)
|
142
|
-
end
|
143
|
-
|
144
|
-
it 'override type from meta' do
|
145
|
-
check_has_one(:main_category, data: { id: 33, name: 'Hobbies' }, meta: { type: :category })
|
146
|
-
end
|
147
|
-
|
148
|
-
it 'creates method for raw types' do
|
149
|
-
check_has_one(:raw_attr, raw_data, Hash)
|
150
|
-
end
|
151
|
-
|
152
|
-
it 'replaces method with raw types' do
|
153
|
-
check_has_one(:do_stuff_method, raw_data, Hash)
|
154
|
-
end
|
155
|
-
end
|
156
|
-
end
|
157
|
-
|
158
87
|
context 'empty base_url' do
|
159
88
|
it 'throws on requests' do
|
160
89
|
class Foo < ApiResource::Resource;
|
@@ -241,9 +170,6 @@ RSpec.describe ApiResource::Resource do
|
|
241
170
|
let(:sign_up_data) { { email: 'user@example.com', password: 'pass', password_confirmation: 'pass', name: 'Joe' } }
|
242
171
|
let(:sign_up) { SignUp.new(sign_up_data) }
|
243
172
|
|
244
|
-
before do
|
245
|
-
|
246
|
-
end
|
247
173
|
it 'uses the resource_path and uses the type in meta' do
|
248
174
|
expected_value = { data: { id: 3, email: 'user@example.com', name: 'Jane' }, meta: { type: 'user' } }
|
249
175
|
stub = req(:post, '/sign_up', expected_value, 200, sign_up_data)
|
data/spec/spec_helper.rb
CHANGED
@@ -105,4 +105,6 @@ def req(method, path, returned_resource, status=200, params=nil)
|
|
105
105
|
stub_request(method, "#{BlogApi::Resource.base_url}#{path}").
|
106
106
|
with(headers: { 'Accept' => 'application/json' }, body: params).
|
107
107
|
to_return(status: status, body: response_body)
|
108
|
-
end
|
108
|
+
end
|
109
|
+
|
110
|
+
ApiResource::Resource::log = RestClient::log = 'stdout'
|
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.7.
|
4
|
+
version: 0.7.8
|
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-06
|
11
|
+
date: 2015-07-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: simple-hmac
|
@@ -135,6 +135,7 @@ files:
|
|
135
135
|
- lib/api-resource.rb
|
136
136
|
- lib/api-resource/resource.rb
|
137
137
|
- lib/api-resource/version.rb
|
138
|
+
- spec/aggregate_resource_spec.rb
|
138
139
|
- spec/blog_api.rb
|
139
140
|
- spec/model_name_spec.rb
|
140
141
|
- spec/resource_collection_spec.rb
|
@@ -167,6 +168,7 @@ signing_key:
|
|
167
168
|
specification_version: 4
|
168
169
|
summary: A lightweight REST API access library.
|
169
170
|
test_files:
|
171
|
+
- spec/aggregate_resource_spec.rb
|
170
172
|
- spec/blog_api.rb
|
171
173
|
- spec/model_name_spec.rb
|
172
174
|
- spec/resource_collection_spec.rb
|