api-resource 0.6.1 → 0.7.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 +32 -22
- data/lib/api-resource/version.rb +1 -1
- data/spec/resource_spec.rb +8 -35
- data/spec/spec_helper.rb +20 -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: 1b43230ab4592f487f3ced50d5148946ad42811c
|
4
|
+
data.tar.gz: bf945dbe477665f5ae40e0e8a8d40416d14b7952
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d78d747c15f45145f9f4cc89b5816f4973a1cba477b08ad5479af57c4fdfb10ae4f7f4a9df0da23bcb4156d90a2ac9faca3c85372de58df6f0fff74798a8d46e
|
7
|
+
data.tar.gz: 201e9c121ab99db3182c3f76941e5c8689c9153b69a3c69d63c1fa0745fc7702bf2cdf137a397672447ded96b76cee3215b0f4eeb839b10a9eba8c3149c907a3
|
@@ -60,23 +60,23 @@ module ApiResource
|
|
60
60
|
self.class.resource_path = path
|
61
61
|
end
|
62
62
|
|
63
|
-
def self.find(id,
|
64
|
-
result = client(:get,
|
63
|
+
def self.find(id, params={})
|
64
|
+
result = client(:get, params, resource_path, id)
|
65
65
|
json = JSON.parse(result)
|
66
66
|
self.new(json['data'] || json)
|
67
67
|
end
|
68
68
|
|
69
|
-
def self.all
|
70
|
-
self.where({}
|
69
|
+
def self.all
|
70
|
+
self.where({})
|
71
71
|
end
|
72
72
|
|
73
|
-
def self.all_pages(
|
74
|
-
|
75
|
-
per_page =
|
76
|
-
page =
|
73
|
+
def self.all_pages(params={})
|
74
|
+
params = { page: 1, per_page: default_per_page }.merge(params)
|
75
|
+
per_page = params[:per_page].to_i
|
76
|
+
page = params[:page].to_i
|
77
77
|
begin
|
78
|
-
|
79
|
-
result = self.where(
|
78
|
+
params[:page] = page
|
79
|
+
result = self.where(params)
|
80
80
|
total_count = result.try :total_count
|
81
81
|
arr = result.to_a
|
82
82
|
count = arr.length
|
@@ -85,8 +85,8 @@ module ApiResource
|
|
85
85
|
end while true
|
86
86
|
end
|
87
87
|
|
88
|
-
def self.where(
|
89
|
-
result = client(
|
88
|
+
def self.where(params)
|
89
|
+
result = client(:get, params, resource_path)
|
90
90
|
create_resource_collection(result)
|
91
91
|
end
|
92
92
|
|
@@ -110,7 +110,7 @@ module ApiResource
|
|
110
110
|
end
|
111
111
|
|
112
112
|
def destroy!(options={})
|
113
|
-
|
113
|
+
submit_resource(resource_path, false, options[:id_attr], :delete)
|
114
114
|
self
|
115
115
|
end
|
116
116
|
|
@@ -162,9 +162,9 @@ module ApiResource
|
|
162
162
|
def self.method_missing(m, *args, &_)
|
163
163
|
case (m)
|
164
164
|
when /^by_(.+)/
|
165
|
-
resource_path = self.resource_path
|
165
|
+
resource_path = build_url($1.pluralize, args[0], self.resource_path)
|
166
166
|
Class.new(self) do
|
167
|
-
self.resource_path =
|
167
|
+
self.resource_path = resource_path
|
168
168
|
end
|
169
169
|
else
|
170
170
|
super
|
@@ -174,13 +174,23 @@ module ApiResource
|
|
174
174
|
def method_missing(m, *args, &_)
|
175
175
|
case m
|
176
176
|
when /^by_(.+)/
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
177
|
+
resource_path = self.class.build_url($1.pluralize, args[0], self.resource_path)
|
178
|
+
Class.new do
|
179
|
+
def initialize(proxied, resource_path)
|
180
|
+
@proxied = proxied
|
181
|
+
@resource_path = resource_path
|
182
|
+
end
|
183
|
+
|
184
|
+
def method_missing(m, *args, &block)
|
185
|
+
begin
|
186
|
+
old_resource_path = @proxied.resource_path
|
187
|
+
@proxied.resource_path = @resource_path
|
188
|
+
@proxied.public_send(m, *args, &block)
|
189
|
+
ensure
|
190
|
+
@proxied.resource_path = old_resource_path
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end.new(self, resource_path)
|
184
194
|
else
|
185
195
|
super
|
186
196
|
end
|
data/lib/api-resource/version.rb
CHANGED
data/spec/resource_spec.rb
CHANGED
@@ -20,23 +20,6 @@ RSpec.describe ApiResource::Resource do
|
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
-
def check_returned_array(type, expected, actual)
|
24
|
-
expect(actual).to be_a Enumerable
|
25
|
-
actual.each { |e| expect(e).to be_a type }
|
26
|
-
expect(actual.map { |e| e.attributes }).to match ((expected.is_a?(Hash) && expected[:data]) || expected)
|
27
|
-
end
|
28
|
-
|
29
|
-
def check_returned_object(type, expected, actual, *except)
|
30
|
-
expect(actual).to be_a(type)
|
31
|
-
expect(actual.try(:attributes) || actual).to match((expected[:data] || expected).except(*except))
|
32
|
-
end
|
33
|
-
|
34
|
-
def req(verb, path, resource, status=200, params=nil)
|
35
|
-
stub_request(verb, "#{BlogApi::Resource.base_url}#{path}").
|
36
|
-
with(headers: { 'Accept' => 'application/json' }, body: params).
|
37
|
-
to_return(status: status, body: resource.is_a?(String) || resource.blank? ? resource : resource.to_json)
|
38
|
-
end
|
39
|
-
|
40
23
|
before do
|
41
24
|
@expected_resources = { data: [{ id: 1257, title: 'Hello' },
|
42
25
|
{ id: 33, title: 'World' },
|
@@ -65,7 +48,7 @@ RSpec.describe ApiResource::Resource do
|
|
65
48
|
|
66
49
|
context '::find' do
|
67
50
|
let(:blog_attrs) { { id: 1257, title: 'Hello' } }
|
68
|
-
let(:extra_params) { { foo: 3, bar: 'baz' }}
|
51
|
+
let(:extra_params) { { foo: 3, bar: 'baz' } }
|
69
52
|
|
70
53
|
def check_found_resource(returned_data, extra_params={})
|
71
54
|
resource_id = blog_attrs[:id]
|
@@ -77,7 +60,7 @@ RSpec.describe ApiResource::Resource do
|
|
77
60
|
|
78
61
|
it 'creates resource from data rooted json' do
|
79
62
|
check_found_resource(data: blog_attrs)
|
80
|
-
check_found_resource({data: blog_attrs}, extra_params)
|
63
|
+
check_found_resource({ data: blog_attrs }, extra_params)
|
81
64
|
end
|
82
65
|
|
83
66
|
it 'creates resource from un-rooted json' do
|
@@ -96,20 +79,10 @@ RSpec.describe ApiResource::Resource do
|
|
96
79
|
end
|
97
80
|
|
98
81
|
context '::where' do
|
99
|
-
|
82
|
+
let(:query_params) { { title: 'hello world', tags: ['hello', 'first', 'greeting'] } }
|
100
83
|
it 'fetches resourced filtered by query string with GET' do
|
101
|
-
req(:get,
|
102
|
-
result = BlogApi::Blog.where(
|
103
|
-
check_returned_array(BlogApi::Blog, @expected_resources, result)
|
104
|
-
end
|
105
|
-
|
106
|
-
it 'fetches resources filtered by query string with POST' do
|
107
|
-
stub_request(:post, "#{BlogApi::Blog.base_url}/blogs").
|
108
|
-
with(headers: { 'Accept' => 'application/json', 'Content-Type' => 'application/x-www-form-urlencoded' },
|
109
|
-
body: 'title=hello%20world&tags[]=hello&tags[]=first&tags[]=greeting').
|
110
|
-
to_return(status: 200, body: @expected_resources.to_json)
|
111
|
-
|
112
|
-
result = BlogApi::Blog.where({ title: 'hello world', tags: ['hello', 'first', 'greeting'] }, :post)
|
84
|
+
req(:get, "/blogs?#{query_params.to_param}", @expected_resources)
|
85
|
+
result = BlogApi::Blog.where(query_params)
|
113
86
|
check_returned_array(BlogApi::Blog, @expected_resources, result)
|
114
87
|
end
|
115
88
|
|
@@ -241,7 +214,7 @@ RSpec.describe ApiResource::Resource do
|
|
241
214
|
user_id = 'ux345'
|
242
215
|
|
243
216
|
stub = req(:post, "/users/#{user_id}/blogs", expected_value, 201, blog_attrs.except(:id))
|
244
|
-
returned = blog.
|
217
|
+
returned = blog.by_user(user_id).save!
|
245
218
|
check_returned_object(BlogApi::Blog, expected_value, blog)
|
246
219
|
expect(returned).to eq(blog)
|
247
220
|
expect(blog.id).to eq(blog_id)
|
@@ -253,7 +226,7 @@ RSpec.describe ApiResource::Resource do
|
|
253
226
|
user_id = 'ux345'
|
254
227
|
|
255
228
|
stub = req(:put, "/users/#{user_id}/blogs/#{blog_id}", nil, 204, blog_attrs.except(:id))
|
256
|
-
returned = blog.
|
229
|
+
returned = blog.by_user(user_id).save!
|
257
230
|
expect(returned).to eq(blog)
|
258
231
|
expect(blog.id).to eq(blog_id)
|
259
232
|
expect(stub).to have_been_requested
|
@@ -285,7 +258,7 @@ RSpec.describe ApiResource::Resource do
|
|
285
258
|
end
|
286
259
|
|
287
260
|
let(:sign_up_data) { { email: 'user@example.com', password: 'pass', password_confirmation: 'pass', name: 'Joe' } }
|
288
|
-
let
|
261
|
+
let(:sign_up) { SignUp.new(sign_up_data) }
|
289
262
|
|
290
263
|
before do
|
291
264
|
|
data/spec/spec_helper.rb
CHANGED
@@ -86,3 +86,23 @@ RSpec.configure do |config|
|
|
86
86
|
# as the one that triggered the failure.
|
87
87
|
Kernel.srand config.seed
|
88
88
|
end
|
89
|
+
|
90
|
+
def check_returned_array(type, expected, actual)
|
91
|
+
expect(actual).to be_a Enumerable
|
92
|
+
actual.each { |e| expect(e).to be_a type }
|
93
|
+
expect(actual.map { |e| e.attributes }).to match ((expected.is_a?(Hash) && expected[:data]) || expected)
|
94
|
+
end
|
95
|
+
|
96
|
+
def check_returned_object(type, expected, actual, *except)
|
97
|
+
expect(actual).to be_a(type)
|
98
|
+
expect(actual.try(:attributes) || actual).to match((expected[:data] || expected).except(*except))
|
99
|
+
end
|
100
|
+
|
101
|
+
def req(method, path, returned_resource, status=200, params=nil)
|
102
|
+
response_body = returned_resource.is_a?(String) || returned_resource.blank? ?
|
103
|
+
returned_resource :
|
104
|
+
returned_resource.to_json
|
105
|
+
stub_request(method, "#{BlogApi::Resource.base_url}#{path}").
|
106
|
+
with(headers: { 'Accept' => 'application/json' }, body: params).
|
107
|
+
to_return(status: status, body: response_body)
|
108
|
+
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.7.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-04-
|
11
|
+
date: 2015-04-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: simple-hmac
|