Class: Niftycloud::PaginatedResponse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(array) ⇒ PaginatedResponse

Returns a new instance of PaginatedResponse.



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

def initialize(array)
  @array = array
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/niftycloud/paginated_response.rb', line 17

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

Instance Attribute Details

#clientObject

Returns the value of attribute client.



3
4
5
# File 'lib/niftycloud/paginated_response.rb', line 3

def client
  @client
end

Instance Method Details

#==(other) ⇒ Object



9
10
11
# File 'lib/niftycloud/paginated_response.rb', line 9

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

#auto_paginateObject



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/niftycloud/paginated_response.rb', line 42

def auto_paginate
  response = block_given? ? nil : []
  each_page do |page|
    if block_given?
      page.each do |item|
        yield item
      end
    else
      response += page
    end
  end
  response
end

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

Yields:

  • (current)


33
34
35
36
37
38
39
40
# File 'lib/niftycloud/paginated_response.rb', line 33

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

#first_pageObject



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

def first_page
  return nil if @client.nil? || !has_first_page?
  path = @links.first.sub(/#{@client.endpoint}/, '')
  @client.get(path)
end

#has_first_page?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/niftycloud/paginated_response.rb', line 66

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

#has_last_page?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/niftycloud/paginated_response.rb', line 56

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

#has_next_page?Boolean

Returns:

  • (Boolean)


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

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

#has_prev_page?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/niftycloud/paginated_response.rb', line 86

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

#inspectObject



13
14
15
# File 'lib/niftycloud/paginated_response.rb', line 13

def inspect
  @array.inspect
end

#last_pageObject



60
61
62
63
64
# File 'lib/niftycloud/paginated_response.rb', line 60

def last_page
  return nil if @client.nil? || !has_last_page?
  path = @links.last.sub(/#{@client.endpoint}/, '')
  @client.get(path)
end

#next_pageObject



80
81
82
83
84
# File 'lib/niftycloud/paginated_response.rb', line 80

def next_page
  return nil if @client.nil? || !has_next_page?
  path = @links.next.sub(/#{@client.endpoint}/, '')
  @client.get(path)
end

#parse_headers!(headers) ⇒ Object



29
30
31
# File 'lib/niftycloud/paginated_response.rb', line 29

def parse_headers!(headers)
  @links = PageLinks.new headers
end

#prev_pageObject



90
91
92
93
94
# File 'lib/niftycloud/paginated_response.rb', line 90

def prev_page
  return nil if @client.nil? || !has_prev_page?
  path = @links.prev.sub(/#{@client.endpoint}/, '')
  @client.get(path)
end

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

Returns:

  • (Boolean)


25
26
27
# File 'lib/niftycloud/paginated_response.rb', line 25

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