Class: RenderRuby::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/render_ruby/collection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, total:, next_cursor:, prev_cursor:) ⇒ Collection

Returns a new instance of Collection.



20
21
22
23
24
25
# File 'lib/render_ruby/collection.rb', line 20

def initialize(data:, total:, next_cursor:, prev_cursor:)
  @data = data
  @total = total
  @next_cursor = next_cursor
  @prev_cursor = prev_cursor
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/render_ruby/collection.rb', line 5

def data
  @data
end

#next_cursorObject (readonly)

Returns the value of attribute next_cursor.



5
6
7
# File 'lib/render_ruby/collection.rb', line 5

def next_cursor
  @next_cursor
end

#prev_cursorObject (readonly)

Returns the value of attribute prev_cursor.



5
6
7
# File 'lib/render_ruby/collection.rb', line 5

def prev_cursor
  @prev_cursor
end

#totalObject (readonly)

Returns the value of attribute total.



5
6
7
# File 'lib/render_ruby/collection.rb', line 5

def total
  @total
end

Class Method Details

.from_response(response, type:) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/render_ruby/collection.rb', line 7

def self.from_response(response, type:)
  body = response.body

  return new(data: [], total: 0, next_cursor: '', prev_cursor: '') if body.empty?

  new(
    data: body.map { |attrs| type.new(attrs) },
    total: body.count,
    next_cursor: body.last['cursor'],
    prev_cursor: body.first['cursor']
  )
end