Class: Yandex360::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/yandex360/collection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, items:, total:) ⇒ Collection

Returns a new instance of Collection.



18
19
20
21
22
# File 'lib/yandex360/collection.rb', line 18

def initialize(data:, items:, total:)
  @data   = data
  @items  = items
  @total  = total
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/yandex360/collection.rb', line 7

def data
  @data
end

#itemsObject (readonly)

Returns the value of attribute items.



7
8
9
# File 'lib/yandex360/collection.rb', line 7

def items
  @items
end

#totalObject (readonly)

Returns the value of attribute total.



7
8
9
# File 'lib/yandex360/collection.rb', line 7

def total
  @total
end

Class Method Details

.from_response(response, key:, type:) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/yandex360/collection.rb', line 9

def self.from_response(response, key:, type:)
  body = response.body
  new(
    data: body[key]&.map {|attrs| type.new(attrs) } || [],
    items: body["items"] || 0,
    total: body["total"] || 0
  )
end

Instance Method Details

#[](index) ⇒ Object



46
47
48
# File 'lib/yandex360/collection.rb', line 46

def [](index)
  data[index]
end

#each(&block) ⇒ Object



24
25
26
# File 'lib/yandex360/collection.rb', line 24

def each(&block)
  data.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/yandex360/collection.rb', line 34

def empty?
  data.empty?
end

#first(idx = nil) ⇒ Object



38
39
40
# File 'lib/yandex360/collection.rb', line 38

def first(idx=nil)
  n ? data.first(idx) : data.first
end

#last(idx = nil) ⇒ Object



42
43
44
# File 'lib/yandex360/collection.rb', line 42

def last(idx=nil)
  n ? data.last(idx) : data.last
end

#sizeObject Also known as: length, count



28
29
30
# File 'lib/yandex360/collection.rb', line 28

def size
  data.size
end