Class: Easyship::Pagination::Cursor

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

Overview

Represents a pagination object

Constant Summary collapse

CONFIGURATION_VARIABLES =
i[requests_per_second requests_per_minute].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, path, params) ⇒ Cursor



11
12
13
14
15
16
17
18
# File 'lib/easyship/pagination/cursor.rb', line 11

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

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#keyObject (readonly)

Returns the value of attribute key.



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

def key
  @key
end

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



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

def per_page
  @per_page
end

#requests_per_minuteObject (readonly)

Returns the value of attribute requests_per_minute.



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

def requests_per_minute
  @requests_per_minute
end

#requests_per_secondObject (readonly)

Returns the value of attribute requests_per_second.



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

def requests_per_second
  @requests_per_second
end

Instance Method Details

#allObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/easyship/pagination/cursor.rb', line 20

def all
  page = 1

  loop do
    limiter.throttle!

    body = client.get(path, build_request_params(page: page))

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

    yield body

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

    page += 1
  end
end