Class: Easyship::Pagination::Cursor

Inherits:
Object
  • Object
show all
Defined in:
lib/easyship/pagination/cursor.rb

Overview

Represents a pagination object

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, path, params) ⇒ Cursor

Returns a new instance of Cursor.



9
10
11
12
13
14
# File 'lib/easyship/pagination/cursor.rb', line 9

def initialize(client, path, params)
  @client = client
  @path = path
  @params = params
  @per_page = params[:per_page] || Easyship.configuration.per_page
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



7
8
9
# File 'lib/easyship/pagination/cursor.rb', line 7

def client
  @client
end

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/easyship/pagination/cursor.rb', line 7

def key
  @key
end

#paramsObject (readonly)

Returns the value of attribute params.



7
8
9
# File 'lib/easyship/pagination/cursor.rb', line 7

def params
  @params
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/easyship/pagination/cursor.rb', line 7

def path
  @path
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



7
8
9
# File 'lib/easyship/pagination/cursor.rb', line 7

def per_page
  @per_page
end

Instance Method Details

#allObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/easyship/pagination/cursor.rb', line 16

def all
  page = 1

  loop do
    body = client.get(path, params.merge(page: page, per_page: per_page))

    break if body.nil? || body.empty?

    yield body

    break if body.dig(:meta, :pagination, :next).nil?

    page += 1
  end
end