Class: KOSapiClient::Entity::ResultPage

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/kosapi_client/entity/result_page.rb

Overview

ResultPage instance is returned from requests to all paginated resources. It wraps returned objects, stores additional feed metadata and it also helps to do things like auto pagination and next / previous page callbacks.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items, links, auto_paginate = true) ⇒ ResultPage

Returns a new instance of ResultPage.



14
15
16
17
18
# File 'lib/kosapi_client/entity/result_page.rb', line 14

def initialize(items, links, auto_paginate = true)
  @items = items
  @links = links
  @auto_paginate = auto_paginate
end

Instance Attribute Details

#auto_paginateObject

Returns the value of attribute auto_paginate.



12
13
14
# File 'lib/kosapi_client/entity/result_page.rb', line 12

def auto_paginate
  @auto_paginate
end

#itemsObject (readonly)

Returns the value of attribute items.



11
12
13
# File 'lib/kosapi_client/entity/result_page.rb', line 11

def items
  @items
end

Instance Method Details

#countObject



20
21
22
# File 'lib/kosapi_client/entity/result_page.rb', line 20

def count
  @items.count
end

#each(&block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/kosapi_client/entity/result_page.rb', line 32

def each(&block)
  return to_enum(__method__) unless block_given?
  items.each(&block)
  return unless @auto_paginate
  next_link = self.next
  while next_link
    next_page = next_link.follow
    next_link = next_page.next
    next_page.items.each(&block)
  end
end

#nextObject



24
25
26
# File 'lib/kosapi_client/entity/result_page.rb', line 24

def next
  @links.next
end

#prevObject



28
29
30
# File 'lib/kosapi_client/entity/result_page.rb', line 28

def prev
  @links.prev
end