Class: DistributedPress::V1::Social::Collection
- Inherits:
-
ReferencedObject
- Object
- ReferencedObject
- DistributedPress::V1::Social::Collection
- Includes:
- Enumerable
- Defined in:
- lib/distributed_press/v1/social/collection.rb
Overview
A Collection or OrderedCollection is a an object with a list of items. Sometimes, collections are paginated, so we provide methods to traverse them.
Constant Summary collapse
- ATTRIBUTES =
%w[items orderedItems].freeze
- RECURSIVE_ATTRIBUTES =
%w[first next].freeze
Constants inherited from ReferencedObject
ReferencedObject::REFERENTIABLE_ATTRIBUTES, ReferencedObject::URI_FIXES
Instance Attribute Summary
Attributes inherited from ReferencedObject
#dereferencer, #object, #referenced
Instance Method Summary collapse
-
#each(&block) ⇒ Object
If the Collection has items, yield them and keep processing any pages forward until the end.
Methods inherited from ReferencedObject
#_dump, _load, #initialize, #parsed_response, #success?
Constructor Details
This class inherits a constructor from DistributedPress::V1::Social::ReferencedObject
Instance Method Details
#each(&block) ⇒ Object
TODO:
Implement lazy loading
If the Collection has items, yield them and keep processing any pages forward until the end.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/distributed_press/v1/social/collection.rb', line 21 def each(&block) counter = 0 total_items = object['totalItems'] Enumerator.new do |enum| catch :total_items_reached do ATTRIBUTES.each do |attribute| next unless object.key?(attribute) referenced[attribute].each(&block).each do |item| counter += 1 enum << item if total_items throw :total_items_reached if total_items == counter throw :total_items_reached if counter > 1000 end end end RECURSIVE_ATTRIBUTES.each do |attribute| next unless object.key?(attribute) throw :total_items_reached if object['id'] && object['id'] == referenced[attribute]['id'] referenced[attribute].each(&block).each do |item| counter += 1 enum << item if total_items throw :total_items_reached if total_items == counter throw :total_items_reached if counter > 1000 end end end end end end |