Class: LabClient::PaginatedResponse
- Inherits:
-
Object
- Object
- LabClient::PaginatedResponse
- Includes:
- CurlHelper
- Defined in:
- lib/labclient/paginated_response.rb
Overview
Additional Pagination Support First page is always request prior to this class being instantiated
Instance Attribute Summary collapse
-
#array ⇒ Object
Returns the value of attribute array.
-
#client ⇒ Object
Returns the value of attribute client.
-
#response ⇒ Object
Returns the value of attribute response.
Instance Method Summary collapse
- #auto_paginate {|@array| ... } ⇒ Object
-
#each_page {|@array| ... } ⇒ Object
rubocop:enable Lint/MissingSuper.
- #help ⇒ Object
-
#initialize(klass, response, client) ⇒ PaginatedResponse
constructor
A new instance of PaginatedResponse.
- #inspect ⇒ Object
- #link_regex ⇒ Object
- #method_missing(name, *args, &block) ⇒ Object
- #next_page {|results| ... } ⇒ Object
-
#next_page? ⇒ Boolean
Check for and store next page.
- #page ⇒ Object
-
#paginate_with_limit(limit) {|@array| ... } ⇒ Object
Paginate to a limit.
- #process ⇒ Object
-
#process_entry(entry, entry_response) ⇒ Object
Create Class Objects.
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
rubocop:disable Lint/MissingSuper.
-
#success? ⇒ Boolean
Forward response success.
- #total ⇒ Object
- #total_pages ⇒ Object
Methods included from CurlHelper
Constructor Details
#initialize(klass, response, client) ⇒ PaginatedResponse
Returns a new instance of PaginatedResponse.
9 10 11 12 13 14 15 16 17 |
# File 'lib/labclient/paginated_response.rb', line 9 def initialize(klass, response, client) @klass = klass @response = response @client = client @array = response.data.map { |entry| process_entry(entry, response) } auto_paginate if client.settings[:paginate] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/labclient/paginated_response.rb', line 23 def method_missing(name, *args, &block) if @array.respond_to?(name) @array.send(name, *args, &block) else super end end |
Instance Attribute Details
#array ⇒ Object
Returns the value of attribute array.
7 8 9 |
# File 'lib/labclient/paginated_response.rb', line 7 def array @array end |
#client ⇒ Object
Returns the value of attribute client.
7 8 9 |
# File 'lib/labclient/paginated_response.rb', line 7 def client @client end |
#response ⇒ Object
Returns the value of attribute response.
7 8 9 |
# File 'lib/labclient/paginated_response.rb', line 7 def response @response end |
Instance Method Details
#auto_paginate {|@array| ... } ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/labclient/paginated_response.rb', line 52 def auto_paginate(&block) yield @array if block_given? loop do break unless next_page? next_page(&block) end @array end |
#each_page {|@array| ... } ⇒ Object
rubocop:enable Lint/MissingSuper
47 48 49 50 |
# File 'lib/labclient/paginated_response.rb', line 47 def each_page(&block) yield @array # This will eventually be the whole list auto_paginate(&block) end |
#help ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/labclient/paginated_response.rb', line 31 def help puts <<~DOC Pagination Helper Methods auto_paginate Automatically collect and return all results for a query. Accepts block paginate_with_limit Iterate through pages, but end when a certain limit is reached each_page Similar with auto_paginate, you can call each_page directly DOC end |
#inspect ⇒ Object
19 20 21 |
# File 'lib/labclient/paginated_response.rb', line 19 def inspect @array end |
#link_regex ⇒ Object
90 91 92 |
# File 'lib/labclient/paginated_response.rb', line 90 def link_regex /<([^>]+)>; rel="([^"]+)"/ end |
#next_page {|results| ... } ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/labclient/paginated_response.rb', line 94 def next_page return false unless next_page? @response = client.http.request(:get, @link) raise LabClient::Error.new(@response), @response.friendly_error unless @response.success? results = process @array.concat results yield results if block_given? end |
#next_page? ⇒ Boolean
Check for and store next page
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/labclient/paginated_response.rb', line 79 def next_page? text = @response.headers['link']&.split(',')&.find { |x| x.include? 'next' } return nil if text.nil? @link = link_regex.match(text.strip)[1] return false if @link.nil? true end |
#page ⇒ Object
130 131 132 |
# File 'lib/labclient/paginated_response.rb', line 130 def page response.headers['x-page'].to_i end |
#paginate_with_limit(limit) {|@array| ... } ⇒ Object
Paginate to a limit
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/labclient/paginated_response.rb', line 65 def paginate_with_limit(limit, &block) yield @array if block_given? loop do break unless next_page? break if @array.length >= limit next_page(&block) end @array end |
#process ⇒ Object
111 112 113 114 115 |
# File 'lib/labclient/paginated_response.rb', line 111 def process @response.data.map do |entry| process_entry(entry, @response) end end |
#process_entry(entry, entry_response) ⇒ Object
Create Class Objects
107 108 109 |
# File 'lib/labclient/paginated_response.rb', line 107 def process_entry(entry, entry_response) @klass ? @klass.new(entry, entry_response, client) : entry end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
rubocop:disable Lint/MissingSuper
44 |
# File 'lib/labclient/paginated_response.rb', line 44 def respond_to_missing?(method_name, include_private = false); end |
#success? ⇒ Boolean
Forward response success
118 119 120 |
# File 'lib/labclient/paginated_response.rb', line 118 def success? @response.success? end |
#total ⇒ Object
126 127 128 |
# File 'lib/labclient/paginated_response.rb', line 126 def total response.headers['x-total'].to_i end |
#total_pages ⇒ Object
122 123 124 |
# File 'lib/labclient/paginated_response.rb', line 122 def total_pages response.headers['x-total-pages'].to_i end |