Class: EasyBroker::PaginatedResponse

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/easy_broker/paginated_response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query) ⇒ PaginatedResponse

Returns a new instance of PaginatedResponse.



6
7
8
9
# File 'lib/easy_broker/paginated_response.rb', line 6

def initialize(query)
  @query = query
  @response = query.get
end

Instance Attribute Details

#queryObject (readonly)

Returns the value of attribute query.



4
5
6
# File 'lib/easy_broker/paginated_response.rb', line 4

def query
  @query
end

#responseObject (readonly)

Returns the value of attribute response.



4
5
6
# File 'lib/easy_broker/paginated_response.rb', line 4

def response
  @response
end

Instance Method Details

#eachObject



39
40
41
42
43
44
45
# File 'lib/easy_broker/paginated_response.rb', line 39

def each
  return response.content.to_enum(:each) unless block_given?

  response.content.each do |object|
    yield object
  end
end

#find_eachObject



29
30
31
32
33
34
35
36
37
# File 'lib/easy_broker/paginated_response.rb', line 29

def find_each
  return self.to_enum(:find_each) unless block_given?
  loop do
    each do |object|
      yield object
    end
    break unless next_page
  end 
end

#limitObject



15
16
17
# File 'lib/easy_broker/paginated_response.rb', line 15

def limit
  pagination&.limit
end

#next_pageObject



23
24
25
26
27
# File 'lib/easy_broker/paginated_response.rb', line 23

def next_page
  if page * limit < total
    @response = query.get(page + 1)
  end
end

#pageObject



19
20
21
# File 'lib/easy_broker/paginated_response.rb', line 19

def page
  pagination&.page || 1
end

#totalObject



11
12
13
# File 'lib/easy_broker/paginated_response.rb', line 11

def total
  pagination&.total
end