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
- #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.
-
#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
-
#success? ⇒ Boolean
Forward response success.
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
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/labclient/paginated_response.rb', line 50 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
45 46 47 48 |
# File 'lib/labclient/paginated_response.rb', line 45 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
88 89 90 |
# File 'lib/labclient/paginated_response.rb', line 88 def link_regex /<([^>]+)>; rel=\"([^\"]+)\"/ end |
#next_page {|results| ... } ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/labclient/paginated_response.rb', line 92 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
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/labclient/paginated_response.rb', line 77 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 |
#paginate_with_limit(limit) {|@array| ... } ⇒ Object
Paginate to a limit
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/labclient/paginated_response.rb', line 63 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
109 110 111 112 113 |
# File 'lib/labclient/paginated_response.rb', line 109 def process @response.data.map do |entry| process_entry(entry, @response) end end |
#process_entry(entry, entry_response) ⇒ Object
Create Class Objects
105 106 107 |
# File 'lib/labclient/paginated_response.rb', line 105 def process_entry(entry, entry_response) @klass ? @klass.new(entry, entry_response, client) : entry end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
43 |
# File 'lib/labclient/paginated_response.rb', line 43 def respond_to_missing?(method_name, include_private = false); end |
#success? ⇒ Boolean
Forward response success
116 117 118 |
# File 'lib/labclient/paginated_response.rb', line 116 def success? @response.success? end |