Class: Cubscout::List
- Inherits:
-
Object
- Object
- Cubscout::List
- Includes:
- Enumerable
- Defined in:
- lib/cubscout/list.rb
Overview
the List class is the base class for any collection of objects retrieved from the Helpscout API. it’s an Enumerable that can iterate over each of it’s items.
Helpscout API V2 is paginated and will return at max 50 items for each collection endpoint. The List class allows to query the page status.
Instance Method Summary collapse
- #each(&block) ⇒ Object
-
#initialize(raw_payload, collection_name, object_class) ⇒ List
constructor
A new instance of List.
-
#items ⇒ Object
array of objects Object retrieved from the API’s collection endpoint.
-
#number_of_pages ⇒ Object
total number of pages available.
-
#page ⇒ Object
current page number.
-
#page_size ⇒ Object
number of items in the current page.
-
#size ⇒ Object
number of
items. -
#total_size ⇒ Object
total number of items available.
Constructor Details
#initialize(raw_payload, collection_name, object_class) ⇒ List
Returns a new instance of List.
10 11 12 13 14 |
# File 'lib/cubscout/list.rb', line 10 def initialize(raw_payload, collection_name, object_class) @raw_payload = raw_payload @collection_name = collection_name @object_class = object_class end |
Instance Method Details
#each(&block) ⇒ Object
46 47 48 |
# File 'lib/cubscout/list.rb', line 46 def each(&block) items.each(&block) end |
#items ⇒ Object
array of objects Object retrieved from the API’s collection endpoint.
37 38 39 |
# File 'lib/cubscout/list.rb', line 37 def items Array(raw_payload.dig("_embedded", collection_name)).map { |item| object_class.new(item) } end |
#number_of_pages ⇒ Object
total number of pages available
27 28 29 |
# File 'lib/cubscout/list.rb', line 27 def number_of_pages raw_payload.dig("page", "totalPages") end |
#page ⇒ Object
current page number
17 18 19 |
# File 'lib/cubscout/list.rb', line 17 def page raw_payload.dig("page", "number") end |
#page_size ⇒ Object
number of items in the current page. Should be the same as number of items, but sometimes it’s not (assumption: it’s a bug in helpscout)
22 23 24 |
# File 'lib/cubscout/list.rb', line 22 def page_size raw_payload.dig("page", "size") end |
#size ⇒ Object
number of items
42 43 44 |
# File 'lib/cubscout/list.rb', line 42 def size items.size end |
#total_size ⇒ Object
total number of items available
32 33 34 |
# File 'lib/cubscout/list.rb', line 32 def total_size raw_payload.dig("page", "totalElements") end |