api-resource 0.3.13 → 0.4.0
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 +20 -6
- data/lib/api-resource/version.rb +1 -1
- data/spec/resource_spec.rb +48 -8
- 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: 3e364dd3af6ba5324fbc0815f624e7ebc9854be1
|
4
|
+
data.tar.gz: 691d9b2272b51d7a4e360b63c88f082480e4de9a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cc8f81f3b3ffe088c9a8245aa600fe31c94114bc0e725f80ef26ea6de27b35cac59f90003f04cc7a4de3ede18187b9c26a35ff6a4c950050595aacb233f4fd8f
|
7
|
+
data.tar.gz: 5560d6dddef680a52344dd4b79554fdf2920dcd022c6f32f9cd85fcba125cb0e50067d35454f77344a3c7723719998f0355181b1552a53c01f48f6e4b3825f3e
|
@@ -44,15 +44,15 @@ module ApiResource
|
|
44
44
|
end
|
45
45
|
|
46
46
|
def self.all_pages(options={})
|
47
|
-
options
|
47
|
+
options = { page: 1, per_page: 500 }.merge(options)
|
48
48
|
per_page = options[:per_page].to_i
|
49
|
-
page
|
49
|
+
page = options[:page].to_i
|
50
50
|
begin
|
51
51
|
options[:page] = page
|
52
|
-
result
|
53
|
-
total_count
|
54
|
-
arr
|
55
|
-
count
|
52
|
+
result = self.where(options)
|
53
|
+
total_count = result.try :total_count
|
54
|
+
arr = result.to_a
|
55
|
+
count = arr.length
|
56
56
|
break if yield(arr, page, per_page, total_count) || (page - 1) * per_page + count >= total_count
|
57
57
|
page += 1
|
58
58
|
end while true
|
@@ -63,6 +63,20 @@ module ApiResource
|
|
63
63
|
create_resource_collection(result)
|
64
64
|
end
|
65
65
|
|
66
|
+
def save!
|
67
|
+
if self.id
|
68
|
+
self.class.client(:put, attributes, self.class.resource_name)
|
69
|
+
else
|
70
|
+
result = self.class.client(:post, attributes, self.class.resource_name)
|
71
|
+
json = JSON.parse(result)
|
72
|
+
self.attributes = json['data'] || json
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def destroy!
|
77
|
+
self.class.client(:delete, id, self.class.resource_name) if self.id
|
78
|
+
end
|
79
|
+
|
66
80
|
def attributes
|
67
81
|
instance_values.with_indifferent_access
|
68
82
|
end
|
data/lib/api-resource/version.rb
CHANGED
data/spec/resource_spec.rb
CHANGED
@@ -27,10 +27,16 @@ RSpec.describe ApiResource::Resource do
|
|
27
27
|
expect(actual.attributes).to match((expected[:data] || expected).except(*except))
|
28
28
|
end
|
29
29
|
|
30
|
-
def req(verb, path, resource)
|
30
|
+
def req(verb, path, resource, status=200)
|
31
31
|
stub_request(verb, "#{BlogResource.base_url}#{path}").
|
32
32
|
with(headers: { 'Accept' => 'application/json' }).
|
33
|
-
to_return(status:
|
33
|
+
to_return(status: status, body: resource === String ? resource : resource.to_json)
|
34
|
+
end
|
35
|
+
|
36
|
+
before do
|
37
|
+
@expected_resources = { data: [{ id: 1257, title: 'Hello' },
|
38
|
+
{ id: 33, title: 'World' },
|
39
|
+
{ id: 38, title: 'Bonjour!' }] }
|
34
40
|
end
|
35
41
|
|
36
42
|
context '::all_pages' do
|
@@ -53,11 +59,6 @@ RSpec.describe ApiResource::Resource do
|
|
53
59
|
end
|
54
60
|
end
|
55
61
|
|
56
|
-
before do
|
57
|
-
@expected_resources = { data: [{ id: 1257, title: 'Hello' },
|
58
|
-
{ id: 33, title: 'World' },
|
59
|
-
{ id: 38, title: 'Bonjour!' }] }
|
60
|
-
end
|
61
62
|
context '::find' do
|
62
63
|
def check_found_resource(expected_value)
|
63
64
|
resource_id = 3
|
@@ -175,10 +176,49 @@ RSpec.describe ApiResource::Resource do
|
|
175
176
|
end
|
176
177
|
end
|
177
178
|
end
|
179
|
+
|
178
180
|
context 'empty base_url' do
|
179
181
|
it 'throws on requests' do
|
180
|
-
class Foo < ApiResource::Resource;
|
182
|
+
class Foo < ApiResource::Resource;
|
183
|
+
end
|
181
184
|
expect(-> { Foo.all }).to raise_exception(StandardError)
|
182
185
|
end
|
183
186
|
end
|
187
|
+
|
188
|
+
context '#save!' do
|
189
|
+
it 'POSTs data when id is nil' do
|
190
|
+
blog = Blog.new(title: 'hello')
|
191
|
+
expect(blog.id).to be_nil
|
192
|
+
expected_value = { data: { id: 325, title: 'hello' } }
|
193
|
+
|
194
|
+
stub = req(:post, '/blogs', expected_value, 201)
|
195
|
+
blog.save!
|
196
|
+
check_returned_object(Blog, expected_value, blog)
|
197
|
+
expect(stub).to have_been_requested
|
198
|
+
end
|
199
|
+
it 'PUTs data when id is not nil' do
|
200
|
+
blog_id = 525
|
201
|
+
blog = Blog.new(id: blog_id, title: 'hello')
|
202
|
+
|
203
|
+
stub = req(:put, '/blogs', '', 204)
|
204
|
+
blog.save!
|
205
|
+
expect(blog.id).to eq(blog_id)
|
206
|
+
expect(stub).to have_been_requested
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
context '#destroy!' do
|
211
|
+
it 'DELETEs data when id is not nil' do
|
212
|
+
blog = Blog.new(id: 323, title: 'hello')
|
213
|
+
stub = req(:delete, '/blogs', '', 204)
|
214
|
+
blog.destroy!
|
215
|
+
expect(stub).to have_been_requested
|
216
|
+
end
|
217
|
+
it 'not DELETE data when id is nil' do
|
218
|
+
blog = Blog.new(id: nil, title: 'hello')
|
219
|
+
stub = req(:delete, '/blogs', '', 204)
|
220
|
+
blog.destroy!
|
221
|
+
expect(stub).to_not have_been_requested
|
222
|
+
end
|
223
|
+
end
|
184
224
|
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.
|
4
|
+
version: 0.4.0
|
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-03-
|
11
|
+
date: 2015-03-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: simple-hmac
|