Class: Cerca::Internal::AgentsCursorPage

Inherits:
Object
  • Object
show all
Includes:
Type::BasePage
Defined in:
lib/cerca/internal/agents_cursor_page.rb

Overview

Examples:

if agents_cursor_page.has_next?
  agents_cursor_page = agents_cursor_page.next_page
end
agents_cursor_page.auto_paging_each do |agent|
  puts(agent)
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Type::BasePage

#to_enum

Constructor Details

#initialize(client:, req:, headers:, page_data:) ⇒ AgentsCursorPage

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of AgentsCursorPage.

Parameters:



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/cerca/internal/agents_cursor_page.rb', line 68

def initialize(client:, req:, headers:, page_data:)
  super

  case page_data
  in {agents: Array => agents}
    @agents = agents.map { Cerca::Internal::Type::Converter.coerce(@model, _1) }
  else
  end
  @cursor = page_data[:cursor]
  @has_more = page_data[:hasMore]
end

Instance Attribute Details

#agentsArray<generic<Elem>>?

Returns:

  • (Array<generic<Elem>>, nil)


20
21
22
# File 'lib/cerca/internal/agents_cursor_page.rb', line 20

def agents
  @agents
end

#cursorString?

Returns:

  • (String, nil)


23
24
25
# File 'lib/cerca/internal/agents_cursor_page.rb', line 23

def cursor
  @cursor
end

#has_moreBoolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/cerca/internal/agents_cursor_page.rb', line 26

def has_more
  @has_more
end

Instance Method Details

#auto_paging_each(&blk) {|| ... } ⇒ Object

Parameters:

  • blk (Proc)

Yield Parameters:

  • (generic<Elem>)


48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cerca/internal/agents_cursor_page.rb', line 48

def auto_paging_each(&blk)
  unless block_given?
    raise ArgumentError.new("A block must be given to ##{__method__}")
  end

  page = self
  loop do
    page.agents&.each(&blk)

    break unless page.next_page?
    page = page.next_page
  end
end

#inspectString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (String)


83
84
85
86
87
88
89
# File 'lib/cerca/internal/agents_cursor_page.rb', line 83

def inspect
  # rubocop:disable Layout/LineLength
  model = Cerca::Internal::Type::Converter.inspect(@model, depth: 1)

  "#<#{self.class}[#{model}]:0x#{object_id.to_s(16)} cursor=#{cursor.inspect} has_more=#{has_more.inspect}>"
  # rubocop:enable Layout/LineLength
end

#next_pageself

Returns:

  • (self)

Raises:

  • (Cerca::HTTP::Error)


35
36
37
38
39
40
41
42
43
# File 'lib/cerca/internal/agents_cursor_page.rb', line 35

def next_page
  unless next_page?
    message = "No more pages available. Please check #next_page? before calling ##{__method__}"
    raise RuntimeError.new(message)
  end

  req = Cerca::Internal::Util.deep_merge(@req, {query: {cursor: cursor}})
  @client.request(req)
end

#next_page?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/cerca/internal/agents_cursor_page.rb', line 29

def next_page?
  has_more
end