Class: Alula::ListObject
- Inherits:
-
Object
- Object
- Alula::ListObject
- Includes:
- Enumerable
- Defined in:
- lib/alula/list_object.rb
Instance Attribute Summary collapse
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#meta ⇒ Object
readonly
Returns the value of attribute meta.
-
#rate_limit ⇒ Object
Returns the value of attribute rate_limit.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
- .construct_from_array(klass, collection) ⇒ Object
- .construct_from_response(klass, response, _opts) ⇒ Object
Instance Method Summary collapse
- #<<(item) ⇒ Object
- #[](index) ⇒ Object
- #each ⇒ Object
- #first ⇒ Object
-
#initialize(list_type) ⇒ ListObject
constructor
A new instance of ListObject.
- #last ⇒ Object
- #length ⇒ Object
-
#merge!(list) ⇒ Object
Merge another list into this list Use only for lists of the same type and same pagination settings Should be used only when we are limited by URL length and need to fetch more than 20 items in the same page.
- #pagination ⇒ Object
- #set_meta(meta) ⇒ Object
Constructor Details
#initialize(list_type) ⇒ ListObject
Returns a new instance of ListObject.
10 11 12 13 14 |
# File 'lib/alula/list_object.rb', line 10 def initialize list_type @type = list_type @items = [] @meta = Meta.new({}) end |
Instance Attribute Details
#items ⇒ Object (readonly)
Returns the value of attribute items.
7 8 9 |
# File 'lib/alula/list_object.rb', line 7 def items @items end |
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
7 8 9 |
# File 'lib/alula/list_object.rb', line 7 def @meta end |
#rate_limit ⇒ Object
Returns the value of attribute rate_limit.
8 9 10 |
# File 'lib/alula/list_object.rb', line 8 def rate_limit @rate_limit end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
7 8 9 |
# File 'lib/alula/list_object.rb', line 7 def type @type end |
Class Method Details
.construct_from_array(klass, collection) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/alula/list_object.rb', line 75 def self.construct_from_array(klass, collection) list = ListObject.new(klass) collection.each do |item| list << klass.new(item['id']).construct_from(item) end list end |
.construct_from_response(klass, response, _opts) ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/alula/list_object.rb', line 64 def self.construct_from_response(klass, response, _opts) list = ListObject.new(klass) response.data['data'].each do |item| list << klass.new(item['id']).construct_from(item) end list. Meta.new(response.data['meta']) list.rate_limit = response.rate_limit list end |
Instance Method Details
#<<(item) ⇒ Object
36 37 38 |
# File 'lib/alula/list_object.rb', line 36 def <<(item) @items << item end |
#[](index) ⇒ Object
24 25 26 |
# File 'lib/alula/list_object.rb', line 24 def[] index @items[index] end |
#each ⇒ Object
20 21 22 |
# File 'lib/alula/list_object.rb', line 20 def each items.each { |i| yield i } end |
#first ⇒ Object
28 29 30 |
# File 'lib/alula/list_object.rb', line 28 def first @items.first end |
#last ⇒ Object
32 33 34 |
# File 'lib/alula/list_object.rb', line 32 def last @items.last end |
#length ⇒ Object
16 17 18 |
# File 'lib/alula/list_object.rb', line 16 def length items.length end |
#merge!(list) ⇒ Object
Merge another list into this list Use only for lists of the same type and same pagination settings Should be used only when we are limited by URL length and need to fetch more than 20 items in the same page
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/alula/list_object.rb', line 51 def merge!(list) @items += list.items = Meta.new( 'page' => { 'total' => list..page.total, 'number' => list..page.number, 'size' => list..page.size } ) () self end |
#pagination ⇒ Object
44 45 46 |
# File 'lib/alula/list_object.rb', line 44 def pagination .page end |
#set_meta(meta) ⇒ Object
40 41 42 |
# File 'lib/alula/list_object.rb', line 40 def () @meta = end |