Class: Ubiquity::Iconik::API::Client::Paginator

Inherits:
Object
  • Object
show all
Defined in:
lib/ubiquity/iconik/api/client/paginator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_client, options = { }) ⇒ Paginator

Returns a new instance of Paginator.



5
6
7
8
# File 'lib/ubiquity/iconik/api/client/paginator.rb', line 5

def initialize(api_client, options = { })
  initialize_logger(options)
  @api_client = api_client
end

Instance Attribute Details

#api_clientObject

Returns the value of attribute api_client.



3
4
5
# File 'lib/ubiquity/iconik/api/client/paginator.rb', line 3

def api_client
  @api_client
end

#loggerObject

Returns the value of attribute logger.



3
4
5
# File 'lib/ubiquity/iconik/api/client/paginator.rb', line 3

def logger
  @logger
end

Instance Method Details

#current_pageObject



21
22
23
# File 'lib/ubiquity/iconik/api/client/paginator.rb', line 21

def current_page
  @current_page ||= last_response['page']
end

#http_clientObject



25
26
27
# File 'lib/ubiquity/iconik/api/client/paginator.rb', line 25

def http_client
  api_client.http_client
end

#include_remaining_pagesObject



29
30
31
32
33
34
35
36
# File 'lib/ubiquity/iconik/api/client/paginator.rb', line 29

def include_remaining_pages
  # response = api_client.response.dup
  @first_page = current_page
  _results = last_results
  _results.concat(remaining_pages_get)
  _per_page = _results.length
  last_response.merge({ 'page' => @first_page, 'per_page' => _per_page, 'objects' => _results })
end

#initialize_logger(args = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/ubiquity/iconik/api/client/paginator.rb', line 10

def initialize_logger(args = {})
  @logger   = args[:logger] ||= Logger.new(args[:log_to] || STDOUT)
  log_level = args[:log_level]
  if log_level
    _logger = @logger.dup
    _logger.level = log_level
    @logger = _logger
  end
  @logger
end

#last_responseObject



38
39
40
# File 'lib/ubiquity/iconik/api/client/paginator.rb', line 38

def last_response
  @last_response ||= api_client.http_client.response_parsed
end

#last_resultsObject



42
43
44
# File 'lib/ubiquity/iconik/api/client/paginator.rb', line 42

def last_results
  @last_results ||= last_response['objects']
end

#next_page?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/ubiquity/iconik/api/client/paginator.rb', line 46

def next_page?
  current_page < total_pages
end

#next_page_get(_next_page_number = nil) ⇒ Object



50
51
52
53
# File 'lib/ubiquity/iconik/api/client/paginator.rb', line 50

def next_page_get(_next_page_number = nil)
  _next_page_number ||= next_page_number
  page_get(_next_page_number)
end

#next_page_numberObject



55
56
57
# File 'lib/ubiquity/iconik/api/client/paginator.rb', line 55

def next_page_number
  @next_page_number ||= current_page + 1
end

#page_get(page_number) ⇒ Object



63
64
65
66
67
68
# File 'lib/ubiquity/iconik/api/client/paginator.rb', line 63

def page_get(page_number)
  logger.debug { "Getting Page #{page_number} of #{total_pages}" }
  new_request = request.class.new(request_args_out.merge(:page => page_number), request_options_out)
  _response = new_request.execute
  process_response
end

#page_sizeObject



59
60
61
# File 'lib/ubiquity/iconik/api/client/paginator.rb', line 59

def page_size
  @page_size ||= last_response['per_page']
end

#pages_get(pages, options = { }) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/ubiquity/iconik/api/client/paginator.rb', line 70

def pages_get(pages, options = { })
  consolidate = options.fetch(:consolidate, true)
  pages = pages.to_a if pages.respond_to?(:to_a)
  pages_out = pages.map { |v| page_get(v) }
  pages_out.flatten! if consolidate
  pages_out
end

#paginated?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/ubiquity/iconik/api/client/paginator.rb', line 78

def paginated?
  @paginated = !!total_pages
end

#process_requestObject



82
83
84
85
86
# File 'lib/ubiquity/iconik/api/client/paginator.rb', line 82

def process_request
  @current_page = nil
  @total_results = nil
  @has_next_page = nil
end

#process_responseObject



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ubiquity/iconik/api/client/paginator.rb', line 88

def process_response
  @current_page = nil
  @last_response = nil
  @next_page_number = nil
  @page_size = nil
  @total_pages = nil
  @total_results = nil
  @paginated = paginated?

  next_page_number

  @last_results = last_response['objects']
end

#remaining_pages_getObject



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/ubiquity/iconik/api/client/paginator.rb', line 122

def remaining_pages_get
  return [ ] unless paginated? && next_page?
  remaining_results = [ ]
  loop do
    response = next_page_get
    remaining_results.concat(response)

    break unless next_page?
  end
  remaining_results
end

#requestObject



102
103
104
# File 'lib/ubiquity/iconik/api/client/paginator.rb', line 102

def request
  api_client.request
end

#request_args_outObject



106
107
108
# File 'lib/ubiquity/iconik/api/client/paginator.rb', line 106

def request_args_out
  @request_args_out ||= request.initial_arguments.dup
end

#request_options_outObject



110
111
112
# File 'lib/ubiquity/iconik/api/client/paginator.rb', line 110

def request_options_out
  @request_options_out ||= { :client => api_client }.merge request.initial_options.dup
end

#total_pagesObject



114
115
116
# File 'lib/ubiquity/iconik/api/client/paginator.rb', line 114

def total_pages
  last_response['pages']
end

#total_resultsObject



118
119
120
# File 'lib/ubiquity/iconik/api/client/paginator.rb', line 118

def total_results
  @total_results ||= last_response['total']
end