Class: LabClient::PaginatedResponse

Inherits:
Object
  • Object
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

#arrayObject

Returns the value of attribute array.



7
8
9
# File 'lib/labclient/paginated_response.rb', line 7

def array
  @array
end

#clientObject

Returns the value of attribute client.



7
8
9
# File 'lib/labclient/paginated_response.rb', line 7

def client
  @client
end

#responseObject

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

Yields:



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

Yields:



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

#helpObject



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

#inspectObject



19
20
21
# File 'lib/labclient/paginated_response.rb', line 19

def inspect
  @array
end


88
89
90
# File 'lib/labclient/paginated_response.rb', line 88

def link_regex
  /<([^>]+)>; rel="([^"]+)"/
end

#next_page {|results| ... } ⇒ Object

Yields:

  • (results)

Raises:



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

Returns:

  • (Boolean)


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

#pageObject



128
129
130
# File 'lib/labclient/paginated_response.rb', line 128

def page
  response.headers['x-page'].to_i
end

#paginate_with_limit(limit) {|@array| ... } ⇒ Object

Paginate to a limit

Yields:



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

#processObject



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

Returns:

  • (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

Returns:

  • (Boolean)


116
117
118
# File 'lib/labclient/paginated_response.rb', line 116

def success?
  @response.success?
end

#totalObject



124
125
126
# File 'lib/labclient/paginated_response.rb', line 124

def total
  response.headers['x-total'].to_i
end

#total_pagesObject



120
121
122
# File 'lib/labclient/paginated_response.rb', line 120

def total_pages
  response.headers['x-total-pages'].to_i
end