Class: Ubiquity::Iconik::API::Client::Paginator
- Inherits:
-
Object
- Object
- Ubiquity::Iconik::API::Client::Paginator
- Defined in:
- lib/ubiquity/iconik/api/client/paginator.rb
Instance Attribute Summary collapse
-
#api_client ⇒ Object
Returns the value of attribute api_client.
-
#logger ⇒ Object
Returns the value of attribute logger.
Instance Method Summary collapse
- #current_page ⇒ Object
- #http_client ⇒ Object
- #include_remaining_pages ⇒ Object
-
#initialize(api_client, options = { }) ⇒ Paginator
constructor
A new instance of Paginator.
- #initialize_logger(args = {}) ⇒ Object
- #last_response ⇒ Object
- #last_results ⇒ Object
- #next_page? ⇒ Boolean
- #next_page_get(_next_page_number = nil) ⇒ Object
- #next_page_number ⇒ Object
- #page_get(page_number) ⇒ Object
- #page_size ⇒ Object
- #pages_get(pages, options = { }) ⇒ Object
- #paginated? ⇒ Boolean
- #process_request ⇒ Object
- #process_response ⇒ Object
- #remaining_pages_get ⇒ Object
- #request ⇒ Object
- #request_args_out ⇒ Object
- #request_options_out ⇒ Object
- #total_pages ⇒ Object
- #total_results ⇒ Object
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, = { }) initialize_logger() @api_client = api_client end |
Instance Attribute Details
#api_client ⇒ Object
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 |
#logger ⇒ Object
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_page ⇒ Object
21 22 23 |
# File 'lib/ubiquity/iconik/api/client/paginator.rb', line 21 def current_page @current_page ||= last_response['page'] end |
#http_client ⇒ Object
25 26 27 |
# File 'lib/ubiquity/iconik/api/client/paginator.rb', line 25 def http_client api_client.http_client end |
#include_remaining_pages ⇒ Object
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_response ⇒ Object
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_results ⇒ Object
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
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_number ⇒ Object
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), ) _response = new_request.execute process_response end |
#page_size ⇒ Object
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, = { }) consolidate = .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
78 79 80 |
# File 'lib/ubiquity/iconik/api/client/paginator.rb', line 78 def paginated? @paginated = !!total_pages end |
#process_request ⇒ Object
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_response ⇒ Object
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_get ⇒ Object
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 |
#request ⇒ Object
102 103 104 |
# File 'lib/ubiquity/iconik/api/client/paginator.rb', line 102 def request api_client.request end |
#request_args_out ⇒ Object
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_out ⇒ Object
110 111 112 |
# File 'lib/ubiquity/iconik/api/client/paginator.rb', line 110 def @request_options_out ||= { :client => api_client }.merge request..dup end |
#total_pages ⇒ Object
114 115 116 |
# File 'lib/ubiquity/iconik/api/client/paginator.rb', line 114 def total_pages last_response['pages'] end |
#total_results ⇒ Object
118 119 120 |
# File 'lib/ubiquity/iconik/api/client/paginator.rb', line 118 def total_results @total_results ||= last_response['total'] end |