api-resource 0.4.2 → 0.4.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/api-resource/resource.rb +15 -11
- data/lib/api-resource/version.rb +1 -1
- data/spec/resource_spec.rb +25 -9
- 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: 75454aa3ecbcf1e99126cbfad6d289de2b134268
|
4
|
+
data.tar.gz: 85cd9a9492a38e315998ae2a1df4d3b4dfea64a7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bcaeb7fda2107f869d38c16e2ee037342dc2885388b248afd40b33bb7796ec4e6b25ab0e53f224da0d952fcb049151d1cb3c2e00ceb4ed31daa0beb158259f8d
|
7
|
+
data.tar.gz: 5cde687ca57ec77b89c673f93375d57f7037373e1f157664844b4823d4f18702eed854122112a561257c12af166eb33f2fe5d7d483d84e4db4bb0806d14e5e99
|
@@ -20,6 +20,8 @@ module ApiResource
|
|
20
20
|
class_attribute :api_id
|
21
21
|
class_attribute :api_secret
|
22
22
|
class_attribute :hmac_options
|
23
|
+
class_attribute :submitted_attributes_filter
|
24
|
+
|
23
25
|
|
24
26
|
def self.with_hmac(api_id, api_secret, options={})
|
25
27
|
self.hmac = true
|
@@ -28,24 +30,24 @@ module ApiResource
|
|
28
30
|
self.hmac_options = options
|
29
31
|
end
|
30
32
|
|
33
|
+
def self.filter_submitted_attributes(&block)
|
34
|
+
self.submitted_attributes_filter = block if block_given?
|
35
|
+
end
|
36
|
+
|
31
37
|
def initialize(hash=nil)
|
32
38
|
self.attributes = hash if hash
|
33
39
|
end
|
34
40
|
|
35
41
|
def self.resource_path
|
36
|
-
@resource_path ||
|
42
|
+
@resource_path || name.to_s.tableize
|
37
43
|
end
|
38
44
|
|
39
45
|
def self.resource_path=(path)
|
40
46
|
@resource_path = path
|
41
47
|
end
|
42
48
|
|
43
|
-
def
|
44
|
-
|
45
|
-
end
|
46
|
-
|
47
|
-
[:resource_name, :resource_path].each do |name|
|
48
|
-
define_method(name) { self.class.public_send(name) }
|
49
|
+
def resource_path
|
50
|
+
self.class.resource_path
|
49
51
|
end
|
50
52
|
|
51
53
|
def self.find(id)
|
@@ -88,7 +90,7 @@ module ApiResource
|
|
88
90
|
path = options.fetch(:path, resource_path)
|
89
91
|
type = options[:type]
|
90
92
|
json = submit_resource(path, false)
|
91
|
-
meta = json['meta']
|
93
|
+
meta = json && json['meta']
|
92
94
|
type ||= meta && meta['type']
|
93
95
|
if type
|
94
96
|
returned_type = self.class.load_class(type)
|
@@ -104,7 +106,7 @@ module ApiResource
|
|
104
106
|
end
|
105
107
|
|
106
108
|
def attributes
|
107
|
-
instance_values.with_indifferent_access
|
109
|
+
instance_values.with_indifferent_access.except(:errors, :validation_context)
|
108
110
|
end
|
109
111
|
|
110
112
|
protected
|
@@ -113,13 +115,15 @@ module ApiResource
|
|
113
115
|
raise ResourceError unless valid?
|
114
116
|
verb = respond_to?(:id) && id ? :put : :post
|
115
117
|
begin
|
116
|
-
|
118
|
+
attrs = attributes
|
119
|
+
attrs = submitted_attributes_filter[attrs] if submitted_attributes_filter
|
120
|
+
result = self.class.client(verb, attrs, path)
|
117
121
|
rescue RestClient::ExceptionWithResponse => e
|
118
122
|
result = e.http_body
|
119
123
|
raise ResourceError, 'Error executing request'
|
120
124
|
ensure
|
121
125
|
json = parse_and_check_error(result)
|
122
|
-
self.attributes = json['data'] || json if verb == :post && save_attributes
|
126
|
+
self.attributes = json['data'] || json if json && verb == :post && save_attributes
|
123
127
|
end
|
124
128
|
json
|
125
129
|
end
|
data/lib/api-resource/version.rb
CHANGED
data/spec/resource_spec.rb
CHANGED
@@ -27,9 +27,9 @@ 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, status=200)
|
30
|
+
def req(verb, path, resource, status=200, params=nil)
|
31
31
|
stub_request(verb, "#{BlogResource.base_url}#{path}").
|
32
|
-
with(headers: { 'Accept' => 'application/json' }).
|
32
|
+
with(headers: { 'Accept' => 'application/json' }, body: params).
|
33
33
|
to_return(status: status, body: resource.is_a?(String) || resource.blank? ? resource : resource.to_json)
|
34
34
|
end
|
35
35
|
|
@@ -189,9 +189,9 @@ RSpec.describe ApiResource::Resource do
|
|
189
189
|
it 'POSTs data when id is nil' do
|
190
190
|
blog = Blog.new(title: 'hello')
|
191
191
|
expect(blog.id).to be_nil
|
192
|
-
expected_value = { data: { id:
|
192
|
+
expected_value = { data: { id: 32, title: 'hello' } }
|
193
193
|
|
194
|
-
stub = req(:post, '/blogs', expected_value, 201)
|
194
|
+
stub = req(:post, '/blogs', expected_value, 201, { title: 'hello' })
|
195
195
|
returned = blog.save!
|
196
196
|
expect(returned).to eq(blog)
|
197
197
|
check_returned_object(Blog, expected_value, blog)
|
@@ -201,7 +201,7 @@ RSpec.describe ApiResource::Resource do
|
|
201
201
|
blog_id = 525
|
202
202
|
blog = Blog.new(id: blog_id, title: 'hello')
|
203
203
|
|
204
|
-
stub = req(:put, '/blogs', nil, 204)
|
204
|
+
stub = req(:put, '/blogs', nil, 204, id: blog_id.to_s, title: 'hello')
|
205
205
|
returned = blog.save!
|
206
206
|
expect(returned).to eq(blog)
|
207
207
|
expect(blog.id).to eq(blog_id)
|
@@ -238,14 +238,15 @@ RSpec.describe ApiResource::Resource do
|
|
238
238
|
attr_accessor :id, :email, :name
|
239
239
|
end
|
240
240
|
|
241
|
-
let
|
241
|
+
let(:sign_up_data) { { email: 'user@example.com', password: 'pass', password_confirmation: 'pass', name: 'Joe' } }
|
242
|
+
let (:sign_up) { SignUp.new(sign_up_data) }
|
242
243
|
|
243
244
|
before do
|
244
245
|
|
245
246
|
end
|
246
247
|
it 'uses the resource_path and uses the type in meta' do
|
247
248
|
expected_value = { data: { id: 3, email: 'user@example.com', name: 'Jane' }, meta: { type: 'user' } }
|
248
|
-
stub = req(:post, '/sign_up', expected_value, 200)
|
249
|
+
stub = req(:post, '/sign_up', expected_value, 200, sign_up_data)
|
249
250
|
returned = sign_up.submit!
|
250
251
|
check_returned_object(User, expected_value, returned)
|
251
252
|
expect(stub).to have_been_requested
|
@@ -253,7 +254,7 @@ RSpec.describe ApiResource::Resource do
|
|
253
254
|
|
254
255
|
it 'sets the returned errors' do
|
255
256
|
expected_value = { errors: { password_confrimation: ["can't be blank"], email: ["can't be blank", 'is invalid'] } }
|
256
|
-
stub = req(:post, '/sign_up', expected_value, 422)
|
257
|
+
stub = req(:post, '/sign_up', expected_value, 422, sign_up.attributes)
|
257
258
|
expect(proc { sign_up.submit! }).to raise_error(ApiResource::ResourceError)
|
258
259
|
expect(sign_up.errors.messages).to match(expected_value[:errors])
|
259
260
|
expect(stub).to have_been_requested
|
@@ -261,10 +262,25 @@ RSpec.describe ApiResource::Resource do
|
|
261
262
|
|
262
263
|
it 'no request if not valid' do
|
263
264
|
sign_up.name = nil
|
264
|
-
stub
|
265
|
+
stub = req(:post, '/sign_up', nil, 422)
|
265
266
|
expect(proc { sign_up.submit! }).to raise_error(ApiResource::ResourceError)
|
266
267
|
expect(sign_up.errors[:name]).to be_truthy
|
267
268
|
expect(stub).to_not have_been_requested
|
268
269
|
end
|
269
270
|
end
|
271
|
+
context '::filter_submit_params' do
|
272
|
+
class Purchase < BlogResource
|
273
|
+
filter_submitted_attributes do |attrs|
|
274
|
+
address_attrs = [:name, :street, :zip_code]
|
275
|
+
attrs.except(*address_attrs).merge(address: attrs.slice(*address_attrs))
|
276
|
+
end
|
277
|
+
attr_accessor :amount, :name, :street, :zip_code
|
278
|
+
end
|
279
|
+
it 'submit! filters params before being submitted' do
|
280
|
+
comment = Purchase.new(amount: 30, name: 'Jane', street: 'Old Street', zip_code: 'EC1Y 1BE')
|
281
|
+
stub = req(:post, '/purchases', nil, 200, { amount: '30', address: { name: 'Jane', street: 'Old Street', zip_code: 'EC1Y 1BE' } })
|
282
|
+
comment.submit!
|
283
|
+
expect(stub).to have_been_requested
|
284
|
+
end
|
285
|
+
end
|
270
286
|
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.
|
4
|
+
version: 0.4.3
|
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-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: simple-hmac
|