api-resource 0.5.1 → 0.5.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7fb25fa5e1a25eec2eca37948e5c8638d2ac17b2
4
- data.tar.gz: 112ad65c7f8b3d611ac2e8e1a5781adc82736efa
3
+ metadata.gz: 5e46ba73409d59af235f64ccaebdd554a627d284
4
+ data.tar.gz: 6f8b0023573cb1d2895371a77732c10b5a7e8786
5
5
  SHA512:
6
- metadata.gz: f4b7f563692b808b8120f3e88d5ca01b100601b354a869b9265fcdd84cf2b7661d8f2292b9950fb21bff0f06b9b16d804978e54e7966e80886ec629ae42f7edb
7
- data.tar.gz: 7ebc0f1b6768180cac7fa13465e56d7acbfa20f70dc1a1d7bbc0ec6d94ea26403ba5063fd7c1e7de261f28da1a1f4b9cba1917cdb654b2cf9489151965672652
6
+ metadata.gz: bda443d6b27415cfd8e53519e14ebfa3781eb30f3158a4644db23ecc0b56668a72059c36e2a2f67bfb9721e4b981c9d1982d5a57128451167c0ff6cfd1191617
7
+ data.tar.gz: 6e49673ae732c15b76ac12086fa83c0c174a87d77796d6140e9e7615c8a29983ce2cd9463a64c18596b8998f14752748ff62a93d80ee96ec01cc942c7594796f
@@ -23,7 +23,7 @@ module ApiResource
23
23
  class_attribute :api_secret
24
24
  class_attribute :hmac_options
25
25
  class_attribute :submitted_attributes_filter
26
-
26
+ class_attribute :log
27
27
 
28
28
  def self.with_hmac(api_id, api_secret, options={})
29
29
  self.hmac = true
@@ -173,7 +173,7 @@ module ApiResource
173
173
  when /^by_(.+)/
174
174
  with_parent_resource(args[0], $1) unless args.empty?
175
175
  else
176
- # do nothing
176
+ super
177
177
  end
178
178
  end
179
179
 
@@ -193,7 +193,7 @@ module ApiResource
193
193
  def self.load_class(tableized_class)
194
194
  tableized_class = name.to_s.tableize.split('/')[0...-1].push(tableized_class).join('/')
195
195
  klass = tableized_class && tableized_class.to_s.singularize.classify.constantize rescue nil
196
- klass if klass && klass < ApiResource::Resource
196
+ klass if klass && klass < Logging::Resource
197
197
  end
198
198
 
199
199
  def self.client(verb, params, *paths)
@@ -210,7 +210,9 @@ module ApiResource
210
210
  if hmac
211
211
  req.sign!(api_id, api_secret, hmac_options)
212
212
  end
213
- req.execute
213
+ result = req.execute
214
+ Logging.log << result
215
+ result
214
216
  end
215
217
 
216
218
  def self.build_url(*paths)
@@ -218,17 +220,57 @@ module ApiResource
218
220
  URI::parse(base_url).merge(paths.map { |p| "#{URI.encode p.to_s}" }.join('/')).to_s
219
221
  end
220
222
 
221
- class ResourceCollection
222
- include Enumerable
223
+ def self.logger=(param)
224
+ self.log = create_log(param)
225
+ end
223
226
 
224
- def initialize(data, meta, klass)
225
- meta.each { |k, v| self.class.class_eval { define_method(k) { v } } } if meta
226
- @resources = data.map { |e| klass.new(e) }
227
- end
227
+ def self.logger
228
+ self.log ||= Class.new do
229
+ def <<(_)
230
+ end
231
+ end.new
232
+ end
228
233
 
229
- def each(&block)
230
- @resources.each(&block)
234
+ # Create a log that respond to << like a logger
235
+ # param can be 'stdout', 'stderr', a string (then we will log to that file) or a logger (then we return it)
236
+ def self.create_log(param)
237
+ return param unless param.is_a? String
238
+ case param
239
+ when 'stdout'
240
+ Class.new do
241
+ def <<(obj)
242
+ STDOUT.puts obj
243
+ end
244
+ end.new
245
+ when 'stderr'
246
+ Class.new do
247
+ def <<(obj)
248
+ STDERR.puts obj
249
+ end
250
+ end.new
251
+ else
252
+ Class.new do
253
+ def initialize(file)
254
+ @file = file
255
+ end
256
+
257
+ def <<(obj)
258
+ File.open(@file, 'a') { |f| f.puts obj }
259
+ end
260
+ end.new(param)
231
261
  end
232
262
  end
233
263
  end
234
- end
264
+ class ResourceCollection
265
+ include Enumerable
266
+
267
+ def initialize(data, meta, klass)
268
+ meta.each { |k, v| self.class.class_eval { define_method(k) { v } } } if meta
269
+ @resources = data.map { |e| klass.new(e) }
270
+ end
271
+
272
+ def each(&block)
273
+ @resources.each(&block)
274
+ end
275
+ end
276
+ end
@@ -1,3 +1,3 @@
1
1
  module ApiResource
2
- VERSION = '0.5.1'
2
+ VERSION = '0.5.2'
3
3
  end
@@ -1,6 +1,6 @@
1
- RSpec.describe ApiResource::Resource do
1
+ RSpec.describe Logging::Resource do
2
2
  module BlogApi
3
- class Resource < ApiResource::Resource
3
+ class Resource < Logging::Resource
4
4
  self.base_url = 'http://api.example.com'
5
5
  end
6
6
 
@@ -195,7 +195,7 @@ RSpec.describe ApiResource::Resource do
195
195
 
196
196
  context 'empty base_url' do
197
197
  it 'throws on requests' do
198
- class Foo < ApiResource::Resource;
198
+ class Foo < Logging::Resource;
199
199
  end
200
200
  expect(-> { Foo.all }).to raise_exception(StandardError)
201
201
  end
@@ -271,7 +271,7 @@ RSpec.describe ApiResource::Resource do
271
271
  it 'sets the returned errors' do
272
272
  expected_value = { errors: { password_confrimation: ["can't be blank"], email: ["can't be blank", 'is invalid'] } }
273
273
  stub = req(:post, '/sign_up', expected_value, 422, sign_up.attributes)
274
- expect(proc { sign_up.submit! }).to raise_error(ApiResource::ResourceError)
274
+ expect(proc { sign_up.submit! }).to raise_error(Logging::ResourceError)
275
275
  expect(sign_up.errors.messages).to match(expected_value[:errors])
276
276
  expect(stub).to have_been_requested
277
277
  end
@@ -279,7 +279,7 @@ RSpec.describe ApiResource::Resource do
279
279
  it 'no request if not valid' do
280
280
  sign_up.name = nil
281
281
  stub = req(:post, '/sign_up', nil, 422)
282
- expect(proc { sign_up.submit! }).to raise_error(ApiResource::ResourceError)
282
+ expect(proc { sign_up.submit! }).to raise_error(Logging::ResourceError)
283
283
  expect(sign_up.errors[:name]).to be_truthy
284
284
  expect(stub).to_not have_been_requested
285
285
  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.5.1
4
+ version: 0.5.2
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-29 00:00:00.000000000 Z
11
+ date: 2015-03-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: simple-hmac