Class: Cubscout::List

Inherits:
Object
  • Object
show all
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

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

#itemsObject

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_pagesObject

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

#pageObject

current page number



17
18
19
# File 'lib/cubscout/list.rb', line 17

def page
  raw_payload.dig("page", "number")
end

#page_sizeObject

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

#sizeObject

number of items



42
43
44
# File 'lib/cubscout/list.rb', line 42

def size
  items.size
end

#total_sizeObject

total number of items available



32
33
34
# File 'lib/cubscout/list.rb', line 32

def total_size
  raw_payload.dig("page", "totalElements")
end