Class: Ubiquity::MediaSilo::API::V3::Client::Paginator

Inherits:
Object
  • Object
show all
Defined in:
lib/ubiquity/mediasilo/api/v3/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/mediasilo/api/v3/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/mediasilo/api/v3/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/mediasilo/api/v3/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/mediasilo/api/v3/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/mediasilo/api/v3/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/mediasilo/api/v3/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/mediasilo/api/v3/client/paginator.rb', line 5

def total_results
  @total_results
end

Instance Method Details

#http_clientObject



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

def http_client
  api_client.http_client
end

#include_remaining_pagesObject



112
113
114
115
# File 'lib/ubiquity/mediasilo/api/v3/client/paginator.rb', line 112

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

#loggerObject



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

def logger
  api_client.logger
end

#next_page?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/ubiquity/mediasilo/api/v3/client/paginator.rb', line 79

def next_page?
  @has_next_page
end

#next_page_get(_next_page_number = @next_page_number) ⇒ Object



83
84
85
# File 'lib/ubiquity/mediasilo/api/v3/client/paginator.rb', line 83

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

#page_get(page_number) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/ubiquity/mediasilo/api/v3/client/paginator.rb', line 87

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



95
96
97
98
99
100
101
# File 'lib/ubiquity/mediasilo/api/v3/client/paginator.rb', line 95

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)


75
76
77
# File 'lib/ubiquity/mediasilo/api/v3/client/paginator.rb', line 75

def paginated?
  @paginated
end

#prev_page?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/ubiquity/mediasilo/api/v3/client/paginator.rb', line 103

def prev_page?
  @has_prev_page
end

#prev_page_get(_prev_page_number = @prev_page_number) ⇒ Object



107
108
109
110
# File 'lib/ubiquity/mediasilo/api/v3/client/paginator.rb', line 107

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
# File 'lib/ubiquity/mediasilo/api/v3/client/paginator.rb', line 30

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

  links.map! do |l|
    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 ]
  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/mediasilo/api/v3/client/paginator.rb', line 21

def process_response
  http_response = http_client.response

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

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

#remaining_pages_getObject



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

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



63
64
65
# File 'lib/ubiquity/mediasilo/api/v3/client/paginator.rb', line 63

def request
  @request ||= api_client.request
end

#request_args_outObject



67
68
69
# File 'lib/ubiquity/mediasilo/api/v3/client/paginator.rb', line 67

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

#request_options_outObject



71
72
73
# File 'lib/ubiquity/mediasilo/api/v3/client/paginator.rb', line 71

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