logical_model 0.5.4 → 0.5.5

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.4
1
+ 0.5.5
@@ -4,6 +4,8 @@ require 'typhoeus'
4
4
  require 'active_support/all' # todo migrate to yajl
5
5
  require 'kaminari'
6
6
 
7
+ require 'logical_model/hydra'
8
+ require 'logical_model/responses_configuration'
7
9
  require 'logical_model/rest_actions'
8
10
  require 'logical_model/url_helper'
9
11
  require 'logical_model/safe_log'
@@ -50,6 +52,8 @@ require 'logical_model/attributes'
50
52
  # RemoteResource#destroy
51
53
  class LogicalModel
52
54
 
55
+ include LogicalModel::Hydra
56
+ include LogicalModel::ResponsesConfiguration
53
57
  include LogicalModel::Attributes
54
58
  include LogicalModel::RESTActions
55
59
  include LogicalModel::UrlHelper
@@ -87,18 +91,6 @@ class LogicalModel
87
91
  def retries; @retries ||= DEFAULT_RETRIES; end
88
92
  def delete_multiple_enabled?; @enable_delete_multiple ||= false; end
89
93
 
90
- def use_hydra(hydra)
91
- self.hydra=(hydra)
92
- end
93
-
94
- def hydra
95
- @@hydra
96
- end
97
-
98
- def hydra=(hydra)
99
- @@hydra = hydra
100
- end
101
-
102
94
  def validates_associated(*associations)
103
95
  associations.each do |association|
104
96
  validates_each association do |record, attr, value|
@@ -0,0 +1,25 @@
1
+ class LogicalModel
2
+ module Hydra
3
+
4
+ def self.included(base)
5
+ base.send(:extend, ClassMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+
10
+ # Set which Hydra this class should use for calls.
11
+ # @param hydra [Typhoues::Hydra]
12
+ def use_hydra(hydra)
13
+ self.hydra=(hydra)
14
+ end
15
+
16
+ def hydra
17
+ @@hydra
18
+ end
19
+
20
+ def hydra=(hydra)
21
+ @@hydra = hydra
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,35 @@
1
+ class LogicalModel
2
+ module ResponsesConfiguration
3
+
4
+ def self.included(base)
5
+ base.send(:extend, ClassMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+ # By default paginate and all will expect a response in the format:
10
+ # { collection: [....], total: X }
11
+ # Where collection contains an array of hashes that initialize the Object and
12
+ # total contains the total number of elements in result (used for pagination)
13
+ #
14
+ # configure_index_response allows to change this defaults.
15
+ # @example
16
+ # configure_index_response {collection: 'items', total: 'count'}
17
+ # This will expect response to have format: {items: [...], count: X}
18
+ #
19
+ # If collection is nil then array is expected at root and total will be ignored.
20
+ # If total is nil it will be ignored
21
+ def configure_index_response(hash_response)
22
+ @collection_key = hash_response[:collection]
23
+ @total_key = hash_response[:total]
24
+ end
25
+
26
+ def collection_key
27
+ @collection_key ||= 'collection'
28
+ end
29
+
30
+ def total_key
31
+ @total_key ||= 'total'
32
+ end
33
+ end
34
+ end
35
+ end
@@ -175,11 +175,48 @@ class LogicalModel
175
175
  # Following methods are API specific.
176
176
  # They assume we are using a RESTfull API.
177
177
  # for get, put, delete :id is expected
178
- # for post, put attributes are excepted under class_name directly. eg. put( {:id => 1, :class_name => {:attr => "new value for attr"}} )
178
+ # for post, put attributes are excepted under class_name directly.
179
+ # eg. put( {:id => 1, :class_name => {:attr => "new value for attr"}} )
179
180
  #
180
- # On error (400) a "errors" is expected.
181
+ # On error (400) a "errors" key is expected in response
181
182
  # ============================================================================================================
182
183
 
184
+ # @param options [Hash] will be forwarded to API
185
+ def async_all(options={})
186
+ options = self.merge_key(options)
187
+ request = Typhoeus::Request.new(resource_uri, :params => options)
188
+ request.on_complete do |response|
189
+ if response.code >= 200 && response.code < 400
190
+ log_ok(response)
191
+
192
+ result_set = self.from_json(response.body)
193
+ collection = result_set[:collection]
194
+
195
+ yield collection
196
+ else
197
+ log_failed(response)
198
+ end
199
+ end
200
+ self.hydra.queue(request)
201
+ end
202
+
203
+ def all(options={})
204
+ result = nil
205
+ self.retries.times do
206
+ begin
207
+ async_all(options){|i| result = i}
208
+ Timeout::timeout(self.timeout/1000) do
209
+ self.hydra.run
210
+ end
211
+ break unless result.nil?
212
+ rescue Timeout::Error
213
+ self.logger.warn("timeout")
214
+ result = nil
215
+ end
216
+ end
217
+ result
218
+ end
219
+
183
220
  # Asynchronic Pagination
184
221
  # This pagination won't block excecution waiting for result, pagination will be enqueued in Objectr#hydra.
185
222
  #
@@ -397,14 +434,20 @@ class LogicalModel
397
434
  end
398
435
 
399
436
  ##
400
- # Will parse JSON string and initialize classes for all hashes in json_string[collection].
437
+ # Will parse JSON string and initialize classes for all hashes in json_string[collection_key].
401
438
  #
402
439
  # @param json_string [JSON String] This JSON should have format: {collection: [...], total: X}
403
440
  #
404
441
  def self.from_json(json_string)
405
- parsed = ActiveSupport::JSON.decode(json_string)
406
- collection = parsed["collection"].map{|i|self.new(i)}
407
- return { :collection => collection, :total => parsed["total"].to_i }
442
+ parsed_response = ActiveSupport::JSON.decode(json_string)
443
+ parsed_collection = collection_key.nil?? parsed_response : parsed_response[collection_key]
444
+ collection = parsed_collection.map{|i| self.new(i)}
445
+
446
+ if total_key
447
+ {collection: collection, total: parsed_response[total_key].to_i}
448
+ else
449
+ { collection: collection }
450
+ end
408
451
  end
409
452
 
410
453
  end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "logical_model"
8
- s.version = "0.5.4"
8
+ s.version = "0.5.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Dwayne Macgowan"]
@@ -36,6 +36,8 @@ Gem::Specification.new do |s|
36
36
  "lib/logical_model/attributes.rb",
37
37
  "lib/logical_model/belongs_to.rb",
38
38
  "lib/logical_model/has_many_keys.rb",
39
+ "lib/logical_model/hydra.rb",
40
+ "lib/logical_model/responses_configuration.rb",
39
41
  "lib/logical_model/rest_actions.rb",
40
42
  "lib/logical_model/safe_log.rb",
41
43
  "lib/logical_model/url_helper.rb",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logical_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.4
4
+ version: 0.5.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -327,6 +327,8 @@ files:
327
327
  - lib/logical_model/attributes.rb
328
328
  - lib/logical_model/belongs_to.rb
329
329
  - lib/logical_model/has_many_keys.rb
330
+ - lib/logical_model/hydra.rb
331
+ - lib/logical_model/responses_configuration.rb
330
332
  - lib/logical_model/rest_actions.rb
331
333
  - lib/logical_model/safe_log.rb
332
334
  - lib/logical_model/url_helper.rb
@@ -352,7 +354,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
352
354
  version: '0'
353
355
  segments:
354
356
  - 0
355
- hash: -1027467821
357
+ hash: 551778145
356
358
  required_rubygems_version: !ruby/object:Gem::Requirement
357
359
  none: false
358
360
  requirements: