Class: Gitlab::PaginatedResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/paginated_response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(array) ⇒ PaginatedResponse

Returns a new instance of PaginatedResponse.



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

def initialize(array)
  @array = array
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/gitlab/paginated_response.rb', line 19

def method_missing(name, *, &)
  if @array.respond_to?(name)
    @array.send(name, *, &)
  else
    super
  end
end

Instance Attribute Details

#client ⇒ Object

Returns the value of attribute client.



5
6
7
# File 'lib/gitlab/paginated_response.rb', line 5

def client
  @client
end

Instance Method Details

#==(other) ⇒ Object



11
12
13
# File 'lib/gitlab/paginated_response.rb', line 11

def ==(other)
  @array == other
end

#auto_paginate(&block) ⇒ Object



49
50
51
52
53
# File 'lib/gitlab/paginated_response.rb', line 49

def auto_paginate(&block)
  return lazy_paginate.to_a unless block

  lazy_paginate.each(&block)
end

#client_relative_path(link) ⇒ Object



109
110
111
112
# File 'lib/gitlab/paginated_response.rb', line 109

def client_relative_path(link)
  client_endpoint_path = URI.parse(@client.endpoint).request_uri
  URI.parse(link).request_uri.sub(client_endpoint_path, '')
end

#each_page {|current| ... } ⇒ Object

Yields:

  • (current)


36
37
38
39
40
41
42
43
# File 'lib/gitlab/paginated_response.rb', line 36

def each_page
  current = self
  yield current
  while current.has_next_page?
    current = current.next_page
    yield current
  end
end

#first_page ⇒ Object



81
82
83
84
85
# File 'lib/gitlab/paginated_response.rb', line 81

def first_page
  return nil if @client.nil? || !has_first_page?

  @client.get(client_relative_path(@links.first))
end

#first_page? ⇒ Boolean Also known as: has_first_page?

Returns:

  • (Boolean)


76
77
78
# File 'lib/gitlab/paginated_response.rb', line 76

def first_page?
  !(@links.nil? || @links.first.nil?)
end

#inspect ⇒ Object



15
16
17
# File 'lib/gitlab/paginated_response.rb', line 15

def inspect
  @array.inspect
end

#last_page ⇒ Object



70
71
72
73
74
# File 'lib/gitlab/paginated_response.rb', line 70

def last_page
  return nil if @client.nil? || !has_last_page?

  @client.get(client_relative_path(@links.last))
end

#last_page? ⇒ Boolean Also known as: has_last_page?

Returns:

  • (Boolean)


65
66
67
# File 'lib/gitlab/paginated_response.rb', line 65

def last_page?
  !(@links.nil? || @links.last.nil?)
end

#lazy_paginate ⇒ Object



45
46
47
# File 'lib/gitlab/paginated_response.rb', line 45

def lazy_paginate
  to_enum(:each_page).lazy.flat_map(&:to_ary)
end

#next_page ⇒ Object



92
93
94
95
96
# File 'lib/gitlab/paginated_response.rb', line 92

def next_page
  return nil if @client.nil? || !has_next_page?

  @client.get(client_relative_path(@links.next))
end

#next_page? ⇒ Boolean Also known as: has_next_page?

Returns:

  • (Boolean)


87
88
89
# File 'lib/gitlab/paginated_response.rb', line 87

def next_page?
  !(@links.nil? || @links.next.nil?)
end

#paginate_with_limit(limit, &block) ⇒ Object



55
56
57
58
59
# File 'lib/gitlab/paginated_response.rb', line 55

def paginate_with_limit(limit, &block)
  return lazy_paginate.take(limit).to_a unless block

  lazy_paginate.take(limit).each(&block)
end

#parse_headers!(headers) ⇒ Object



31
32
33
34
# File 'lib/gitlab/paginated_response.rb', line 31

def parse_headers!(headers)
  @links = Headers::PageLinks.new(headers)
  @total = Headers::Total.new(headers)
end

#prev_page ⇒ Object



103
104
105
106
107
# File 'lib/gitlab/paginated_response.rb', line 103

def prev_page
  return nil if @client.nil? || !has_prev_page?

  @client.get(client_relative_path(@links.prev))
end

#prev_page? ⇒ Boolean Also known as: has_prev_page?

Returns:

  • (Boolean)


98
99
100
# File 'lib/gitlab/paginated_response.rb', line 98

def prev_page?
  !(@links.nil? || @links.prev.nil?)
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/gitlab/paginated_response.rb', line 27

def respond_to_missing?(method_name, include_private = false)
  super || @array.respond_to?(method_name, include_private)
end

#total ⇒ Object



61
62
63
# File 'lib/gitlab/paginated_response.rb', line 61

def total
  @total.total
end