Class: Camper::PaginatedResponse

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Logging
Defined in:
lib/camper/paginated_response.rb

Overview

Wrapper class of paginated response.

Instance Attribute Summary collapse

Attributes included from Logging

#logger

Instance Method Summary collapse

Constructor Details

#initialize(array) ⇒ PaginatedResponse

Returns a new instance of PaginatedResponse.



13
14
15
# File 'lib/camper/paginated_response.rb', line 13

def initialize(array)
  @array = array
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



11
12
13
# File 'lib/camper/paginated_response.rb', line 11

def client
  @client
end

Instance Method Details

#==(other) ⇒ Object



17
18
19
# File 'lib/camper/paginated_response.rb', line 17

def ==(other)
  @array == other
end

#auto_paginate(limit = nil, &block) ⇒ Object



34
35
36
37
38
39
# File 'lib/camper/paginated_response.rb', line 34

def auto_paginate(limit = nil, &block)
  limit = count if limit.nil?
  return lazy_paginate.take(limit).to_a unless block_given?

  lazy_paginate.take(limit).each(&block)
end

#countObject



21
22
23
# File 'lib/camper/paginated_response.rb', line 21

def count
  @pagination_data.total_count
end

#inspectObject



25
26
27
# File 'lib/camper/paginated_response.rb', line 25

def inspect
  @array.inspect
end

#next_pageObject



46
47
48
49
50
# File 'lib/camper/paginated_response.rb', line 46

def next_page
  return nil if @client.nil? || !has_next_page?

  @client.get(@pagination_data.next, override_path: true)
end

#next_page?Boolean Also known as: has_next_page?

Returns:

  • (Boolean)


41
42
43
# File 'lib/camper/paginated_response.rb', line 41

def next_page?
  !(@pagination_data.nil? || @pagination_data.next.nil?)
end

#parse_headers!(headers) ⇒ Object



29
30
31
32
# File 'lib/camper/paginated_response.rb', line 29

def parse_headers!(headers)
  @pagination_data = PaginationData.new headers
  logger.debug("Pagination data: #{@pagination_data.inspect}")
end