Class: Trolley::Utils::PaginatedArray

Inherits:
Array
  • Object
show all
Defined in:
lib/trolley/utils/PaginatedArray.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enum, page, pages, records, metadata = {}) ⇒ PaginatedArray

Returns a new instance of PaginatedArray.



18
19
20
21
22
23
24
25
26
27
# File 'lib/trolley/utils/PaginatedArray.rb', line 18

def initialize(enum, page, pages, records,  = {})
  super(enum)
  @page = page
  @pages = pages
  @records = records
  @ok = [:ok]

  collection_key = [:collection_key]
  define_singleton_method(collection_key) { self } unless collection_key.nil?
end

Instance Attribute Details

#okObject

Returns the value of attribute ok.



4
5
6
# File 'lib/trolley/utils/PaginatedArray.rb', line 4

def ok
  @ok
end

#pageObject

Returns the value of attribute page.



4
5
6
# File 'lib/trolley/utils/PaginatedArray.rb', line 4

def page
  @page
end

#pagesObject

Returns the value of attribute pages.



4
5
6
# File 'lib/trolley/utils/PaginatedArray.rb', line 4

def pages
  @pages
end

#recordsObject

Returns the value of attribute records.



4
5
6
# File 'lib/trolley/utils/PaginatedArray.rb', line 4

def records
  @records
end

Class Method Details

.from_response(response, klass) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/trolley/utils/PaginatedArray.rb', line 6

def self.from_response(response, klass)
  data = JSON.parse(response)

  enum = Utils::ResponseMapper.new(response, klass).build
  page = data.dig('meta', 'page')
  pages = data.dig('meta', 'pages')
  records = data.dig('meta', 'records')
  collection_key = data.keys.find { |key| data[key].is_a?(Array) }

  new(enum, page, pages, records, ok: data['ok'], collection_key:)
end