api-resource 0.4.4 → 0.5.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 +21 -17
- data/lib/api-resource/version.rb +1 -1
- data/spec/resource_spec.rb +1 -1
- 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: 23cbaeb15eab2892df46438827daafa4fab93168
|
4
|
+
data.tar.gz: 2a6bc12c082d02bd7938d7b57091a08afc97e05b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 005bb30e8ab91f0aaead583e085e4468e29a7b481705ca99de2de49a19b78402ecf722cf005b91169fe0c5b665cae9ae60447a7d8b22ae111b02b668620d9c51
|
7
|
+
data.tar.gz: 3a8d3545b5d6ac2c4bc70ac1741d8359446686fafb762074880a27538dc7ee7ea34a30cd44ea7f14a9cf9d178bfd7247dde939a5d7e2dbd2f8605d0b22d736e0
|
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'uri'
|
1
2
|
require 'simple-hmac'
|
2
3
|
require 'rest_client'
|
3
4
|
require 'active_model'
|
@@ -43,7 +44,7 @@ module ApiResource
|
|
43
44
|
end
|
44
45
|
|
45
46
|
def self.resource_path=(path)
|
46
|
-
@resource_path = path
|
47
|
+
@resource_path = path.gsub(%r{\A/+|/+\Z}, '')
|
47
48
|
end
|
48
49
|
|
49
50
|
def resource_path
|
@@ -57,8 +58,7 @@ module ApiResource
|
|
57
58
|
end
|
58
59
|
|
59
60
|
def self.all
|
60
|
-
|
61
|
-
create_resource_collection(result)
|
61
|
+
self.where({}, :get)
|
62
62
|
end
|
63
63
|
|
64
64
|
def self.all_pages(options={})
|
@@ -115,8 +115,8 @@ module ApiResource
|
|
115
115
|
raise ResourceError unless valid?
|
116
116
|
verb = respond_to?(:id) && id ? :put : :post
|
117
117
|
begin
|
118
|
-
attrs
|
119
|
-
attrs
|
118
|
+
attrs = attributes
|
119
|
+
attrs = submitted_attributes_filter[attrs] if submitted_attributes_filter
|
120
120
|
result = self.class.client(verb, attrs, path)
|
121
121
|
rescue RestClient::ExceptionWithResponse => e
|
122
122
|
result = e.http_body
|
@@ -167,18 +167,20 @@ module ApiResource
|
|
167
167
|
end
|
168
168
|
|
169
169
|
def self.method_missing(m, *args, &_)
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
end
|
170
|
+
case (m)
|
171
|
+
when /^by_(.+)/
|
172
|
+
with_parent_resource(args[0], $1) unless args.empty?
|
173
|
+
else
|
174
|
+
# do nothing
|
176
175
|
end
|
177
176
|
end
|
178
177
|
|
179
178
|
def self.with_parent_resource(parent_resource_id, parent_resource_name)
|
180
|
-
|
181
|
-
|
179
|
+
parent_resource_name = parent_resource_name.pluralize
|
180
|
+
child_resource_path = resource_path
|
181
|
+
Class.new(self) do
|
182
|
+
self.resource_path = build_url(parent_resource_name, parent_resource_id, child_resource_path)
|
183
|
+
end
|
182
184
|
end
|
183
185
|
|
184
186
|
def self.create_resource_collection(result)
|
@@ -192,10 +194,7 @@ module ApiResource
|
|
192
194
|
end
|
193
195
|
|
194
196
|
def self.client(verb, params, *paths)
|
195
|
-
|
196
|
-
url = base_url
|
197
|
-
url += '/' unless url.end_with?('/')
|
198
|
-
url += paths.join('/')
|
197
|
+
url = build_url(*paths)
|
199
198
|
headers = { accept: :json }
|
200
199
|
req_params = { url: url, method: verb, headers: headers }
|
201
200
|
if verb == :get
|
@@ -211,6 +210,11 @@ module ApiResource
|
|
211
210
|
req.execute
|
212
211
|
end
|
213
212
|
|
213
|
+
def self.build_url(*paths)
|
214
|
+
raise RuntimeError, 'Empty base_url' if base_url.blank?
|
215
|
+
URI::parse(base_url).merge(paths.map { |p| "#{URI.encode p.to_s}" }.join('/')).to_s
|
216
|
+
end
|
217
|
+
|
214
218
|
class ResourceCollection
|
215
219
|
include Enumerable
|
216
220
|
|
data/lib/api-resource/version.rb
CHANGED
data/spec/resource_spec.rb
CHANGED
@@ -114,7 +114,7 @@ RSpec.describe ApiResource::Resource do
|
|
114
114
|
it 'fetches resource nested with parent resource' do
|
115
115
|
resource_id = 3
|
116
116
|
req(:get, "/tags/#{resource_id}/blogs", @expected_resources)
|
117
|
-
result = Blog.by_tag(resource_id)
|
117
|
+
result = Blog.by_tag(resource_id).all
|
118
118
|
check_returned_array(Blog, @expected_resources, result)
|
119
119
|
end
|
120
120
|
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.5.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-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: simple-hmac
|