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:



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

Yields:



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

#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


90
91
92
# File 'lib/labclient/paginated_response.rb', line 90

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

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

Yields:

  • (results)

Raises:



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

Returns:

  • (Boolean)


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

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

Paginate to a limit

Yields:



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

#processObject



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

Returns:

  • (Boolean)


44
# File 'lib/labclient/paginated_response.rb', line 44

def respond_to_missing?(method_name, include_private = false); end

#success?Boolean

Forward response success

Returns:

  • (Boolean)


118
119
120
# File 'lib/labclient/paginated_response.rb', line 118

def success?
  @response.success?
end