Class: LabClient::PaginatedResponse
- Inherits:
-
Object
- Object
- LabClient::PaginatedResponse
show all
- 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
Instance Method Summary
collapse
Methods included from CurlHelper
#curl
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
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 " Pagination Helper Methods\n auto_paginate\n Automatically collect and return all results for a query. Accepts block\n paginate_with_limit\n Iterate through pages, but end when a certain limit is reached\n each_page\n Similar with auto_paginate, you can call each_page directly\n DOC\nend\n"
|
#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.['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
128
129
130
|
# File 'lib/labclient/paginated_response.rb', line 128
def page
response.['x-page'].to_i
end
|
#paginate_with_limit(limit) {|@array| ... } ⇒ Object
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
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
116
117
118
|
# File 'lib/labclient/paginated_response.rb', line 116
def success?
@response.success?
end
|
#total ⇒ Object
124
125
126
|
# File 'lib/labclient/paginated_response.rb', line 124
def total
response.['x-total'].to_i
end
|
#total_pages ⇒ Object
120
121
122
|
# File 'lib/labclient/paginated_response.rb', line 120
def total_pages
response.['x-total-pages'].to_i
end
|