Class: SurgeAPI::Internal::Cursor

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

Overview

Examples:

if cursor.has_next?
  cursor = cursor.next_page
end
cursor.auto_paging_each do |contact|
  puts(contact)
end

Defined Under Namespace

Classes: Pagination

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Type::BasePage

#to_enum

Constructor Details

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

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 Cursor.

Parameters:



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/surge_api/internal/cursor.rb', line 70

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

  case page_data
  in {data: Array => data}
    @data = data.map { SurgeAPI::Internal::Type::Converter.coerce(@model, _1) }
  else
  end
  case page_data
  in {pagination: Hash | nil => pagination}
    @pagination =
      SurgeAPI::Internal::Type::Converter.coerce(SurgeAPI::Internal::Cursor::Pagination, pagination)
  else
  end
end

Instance Attribute Details

#dataArray<generic<Elem>>?

Returns:

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


20
21
22
# File 'lib/surge_api/internal/cursor.rb', line 20

def data
  @data
end

#paginationPagination

Returns:



23
24
25
# File 'lib/surge_api/internal/cursor.rb', line 23

def pagination
  @pagination
end

Instance Method Details

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

Parameters:

  • blk (Proc)

Yield Parameters:

  • (generic<Elem>)


50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/surge_api/internal/cursor.rb', line 50

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

  page = self
  loop do
    page.data&.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)


89
90
91
92
93
# File 'lib/surge_api/internal/cursor.rb', line 89

def inspect
  model = SurgeAPI::Internal::Type::Converter.inspect(@model, depth: 1)

  "#<#{self.class}[#{model}]:0x#{object_id.to_s(16)}>"
end

#next_pageself

Returns:

  • (self)

Raises:

  • (SurgeAPI::HTTP::Error)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/surge_api/internal/cursor.rb', line 32

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

  req = SurgeAPI::Internal::Util.deep_merge(
    @req,
    {
      query: pagination&.previous_cursor.nil? ? {after: pagination&.next_cursor} : {before: pagination&.previous_cursor}
    }
  )
  @client.request(req)
end

#next_page?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/surge_api/internal/cursor.rb', line 26

def next_page?
  !data.to_a.empty? && (!pagination&.previous_cursor.to_s.empty? || !pagination&.next_cursor.to_s.empty?)
end