api-resource 0.4.4 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6afbb3ebeef30833689dd643fe2d4adb207fea3a
4
- data.tar.gz: 858ed311b43747cfb3b27013d97a34643aebbb4f
3
+ metadata.gz: 23cbaeb15eab2892df46438827daafa4fab93168
4
+ data.tar.gz: 2a6bc12c082d02bd7938d7b57091a08afc97e05b
5
5
  SHA512:
6
- metadata.gz: 8baeb9905f08887b6aa068ed08775a587a5ca04c9b899ad8d9623e2701dc7b61f7298f4e4e1daf4d605300cd5cae94eea4c6d3d74930b1bc62e3ab9524323e38
7
- data.tar.gz: 14ccce8abbe4c02c5b7fe0f5a225e0386498aa201a680b7dac1038a2b29e06bc96df5813aad1d5ab5c829b8deb3b653fd9abc0af685073582c6459fa1a700ec0
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
- result = client(:get, {}, resource_path)
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 = attributes
119
- attrs = submitted_attributes_filter[attrs] if submitted_attributes_filter
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
- unless args.empty?
171
- by_method_match = /^by_(.+)/.match(m)
172
- if by_method_match
173
- parent_resource_name = by_method_match[1]
174
- with_parent_resource(args[0], parent_resource_name)
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
- result = client(:get, {}, parent_resource_name.pluralize, parent_resource_id, resource_path)
181
- create_resource_collection(result)
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
- raise RuntimeError, 'Empty base_url' if base_url.blank?
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
 
@@ -1,3 +1,3 @@
1
1
  module ApiResource
2
- VERSION = '0.4.4'
2
+ VERSION = '0.5.0'
3
3
  end
@@ -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.4
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-26 00:00:00.000000000 Z
11
+ date: 2015-03-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: simple-hmac