Class: Trolley::Utils::PaginatedArray
- Inherits:
-
Array
- Object
- Array
- Trolley::Utils::PaginatedArray
- Defined in:
- lib/trolley/utils/PaginatedArray.rb
Instance Attribute Summary collapse
-
#ok ⇒ Object
Returns the value of attribute ok.
-
#page ⇒ Object
Returns the value of attribute page.
-
#pages ⇒ Object
Returns the value of attribute pages.
-
#records ⇒ Object
Returns the value of attribute records.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(enum, page, pages, records, metadata = {}) ⇒ PaginatedArray
constructor
A new instance of PaginatedArray.
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
#ok ⇒ Object
Returns the value of attribute ok.
4 5 6 |
# File 'lib/trolley/utils/PaginatedArray.rb', line 4 def ok @ok end |
#page ⇒ Object
Returns the value of attribute page.
4 5 6 |
# File 'lib/trolley/utils/PaginatedArray.rb', line 4 def page @page end |
#pages ⇒ Object
Returns the value of attribute pages.
4 5 6 |
# File 'lib/trolley/utils/PaginatedArray.rb', line 4 def pages @pages end |
#records ⇒ Object
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 |