Class: Ubiquity::Wiredrive::API::V3::Client::Paginator

Inherits:
Object
  • Object
show all
Defined in:
lib/ubiquity/wiredrive/api/v2/client/paginator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_client) ⇒ Paginator

Returns a new instance of Paginator.



7
8
9
10
11
# File 'lib/ubiquity/wiredrive/api/v2/client/paginator.rb', line 7

def initialize(api_client)
  @api_client = api_client

  process_response
end

Instance Attribute Details

#api_clientObject

Returns the value of attribute api_client.



3
4
5
# File 'lib/ubiquity/wiredrive/api/v2/client/paginator.rb', line 3

def api_client
  @api_client
end

#last_page_numberObject (readonly)

Returns the value of attribute last_page_number.



5
6
7
# File 'lib/ubiquity/wiredrive/api/v2/client/paginator.rb', line 5

def last_page_number
  @last_page_number
end

#next_page_numberObject (readonly)

Returns the value of attribute next_page_number.



5
6
7
# File 'lib/ubiquity/wiredrive/api/v2/client/paginator.rb', line 5

def next_page_number
  @next_page_number
end

#page_sizeObject (readonly)

Returns the value of attribute page_size.



5
6
7
# File 'lib/ubiquity/wiredrive/api/v2/client/paginator.rb', line 5

def page_size
  @page_size
end

#prev_page_numberObject (readonly)

Returns the value of attribute prev_page_number.



5
6
7
# File 'lib/ubiquity/wiredrive/api/v2/client/paginator.rb', line 5

def prev_page_number
  @prev_page_number
end

#total_resultsObject (readonly)

Returns the value of attribute total_results.



5
6
7
# File 'lib/ubiquity/wiredrive/api/v2/client/paginator.rb', line 5

def total_results
  @total_results
end

Instance Method Details

#http_clientObject



17
18
19
# File 'lib/ubiquity/wiredrive/api/v2/client/paginator.rb', line 17

def http_client
  api_client.http_client
end

#include_remaining_pagesObject



113
114
115
116
# File 'lib/ubiquity/wiredrive/api/v2/client/paginator.rb', line 113

def include_remaining_pages
  response = api_client.response.dup
  response.concat(remaining_pages_get)
end

#loggerObject



13
14
15
# File 'lib/ubiquity/wiredrive/api/v2/client/paginator.rb', line 13

def logger
  api_client.logger
end

#next_page?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/ubiquity/wiredrive/api/v2/client/paginator.rb', line 80

def next_page?
  @has_next_page
end

#next_page_get(_next_page_number = @next_page_number) ⇒ Object



84
85
86
# File 'lib/ubiquity/wiredrive/api/v2/client/paginator.rb', line 84

def next_page_get(_next_page_number = @next_page_number)
  page_get(_next_page_number)
end

#page_get(page_number) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/ubiquity/wiredrive/api/v2/client/paginator.rb', line 88

def page_get(page_number)
  logger.debug { "Getting Page #{page_number} of #{last_page_number}" }
  new_request = request.class.new(request_args_out.merge('_page' => page_number), request_options_out)
  _response = new_request.execute
  process_response
  _response
end

#pages_get(pages, options = { }) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/ubiquity/wiredrive/api/v2/client/paginator.rb', line 96

def pages_get(pages, options = { })
  consolidate = options.fetch(:consolidate, true)
  pages = pages.to_a if pages.respond_to?(:to_a)
  pages_out = pages.map { |v| page_get(v) }
  pages_out.flatten! if consolidate
  pages_out
end

#paginated?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/ubiquity/wiredrive/api/v2/client/paginator.rb', line 76

def paginated?
  @paginated
end

#prev_page?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/ubiquity/wiredrive/api/v2/client/paginator.rb', line 104

def prev_page?
  @has_prev_page
end

#prev_page_get(_prev_page_number = @prev_page_number) ⇒ Object



108
109
110
111
# File 'lib/ubiquity/wiredrive/api/v2/client/paginator.rb', line 108

def prev_page_get(_prev_page_number = @prev_page_number)
  return [ ] unless paginated?
  page_get(_prev_page_number)
end


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ubiquity/wiredrive/api/v2/client/paginator.rb', line 30

def process_link_header(link)
  links = link.split(',')
  @next_page_number = nil
  @prev_page_number = nil

  links.map! do |k, v|
    # href, rel = l.split('; ')
    # rel = rel.match(/"(\w*)/)[1]
    # href = href.match(/<(.*)>/)[1]
    # _, query = href.split('?')
    # query_as_hash = Hash[ query.split('&').map { |v| v.split('=') } ]
    # @page_size = query_as_hash['_pageSize']
    #
    # case rel
    #   when 'next'
    #     @next_page_number = query_as_hash['_page']
    #     @next_page_href = href
    #   when 'last'
    #     @last_page_number = query_as_hash['_page']
    #     @last_page_href = href
    #   when 'prev'
    #     @prev_page_number = query_as_hash['_page']
    #     @prev_page_href = href
    # end
    #
    # [ rel, href ]
    [ k, v ]
  end
  @has_next_page = !!@next_page_number
  @has_prev_page = !!@prev_page_number

  Hash[links]
end

#process_responseObject



21
22
23
24
25
26
27
28
# File 'lib/ubiquity/wiredrive/api/v2/client/paginator.rb', line 21

def process_response
  http_response = http_client.response

  @total_results = http_response['total']
  @paginated = !!total_results

  process_link_header(http_response['links']) if paginated?
end

#remaining_pages_getObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/ubiquity/wiredrive/api/v2/client/paginator.rb', line 118

def remaining_pages_get
  return [ ] unless paginated? && next_page?
  _next_page_number = @next_page_number
  remaining_results = [ ]

  loop do
    response = next_page_get(_next_page_number)
    break unless response.is_a?(Array)
    remaining_results.concat(response)

    break unless next_page?
    _next_page_number = next_page_number
  end
  remaining_results
end

#requestObject



64
65
66
# File 'lib/ubiquity/wiredrive/api/v2/client/paginator.rb', line 64

def request
  @request ||= api_client.request
end

#request_args_outObject



68
69
70
# File 'lib/ubiquity/wiredrive/api/v2/client/paginator.rb', line 68

def request_args_out
  @request_args_out ||= request.initial_arguments.dup
end

#request_options_outObject



72
73
74
# File 'lib/ubiquity/wiredrive/api/v2/client/paginator.rb', line 72

def request_options_out
  @request_options_out ||= { :client => api_client }.merge request.initial_options.dup
end