Class: Pobo::DTO::PaginatedResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/pobo/dto/paginated_response.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, current_page:, per_page:, total:) ⇒ PaginatedResponse

Returns a new instance of PaginatedResponse.



8
9
10
11
12
13
# File 'lib/pobo/dto/paginated_response.rb', line 8

def initialize(data:, current_page:, per_page:, total:)
  @data = data
  @current_page = current_page
  @per_page = per_page
  @total = total
end

Instance Attribute Details

#current_page ⇒ Object (readonly)

Returns the value of attribute current_page.



6
7
8
# File 'lib/pobo/dto/paginated_response.rb', line 6

def current_page
  @current_page
end

#data ⇒ Object (readonly)

Returns the value of attribute data.



6
7
8
# File 'lib/pobo/dto/paginated_response.rb', line 6

def data
  @data
end

#per_page ⇒ Object (readonly)

Returns the value of attribute per_page.



6
7
8
# File 'lib/pobo/dto/paginated_response.rb', line 6

def per_page
  @per_page
end

#total ⇒ Object (readonly)

Returns the value of attribute total.



6
7
8
# File 'lib/pobo/dto/paginated_response.rb', line 6

def total
  @total
end

Class Method Details

.from_hash(hash, item_class) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/pobo/dto/paginated_response.rb', line 15

def self.from_hash(hash, item_class)
  items = (hash["data"] || hash[:data] || []).map do |item|
    item_class.from_hash(item)
  end

  meta = hash["meta"] || hash[:meta] || {}

  new(
    data: items,
    current_page: meta["current_page"] || meta[:current_page] || 1,
    per_page: meta["per_page"] || meta[:per_page] || 100,
    total: meta["total"] || meta[:total] || 0
  )
end

Instance Method Details

#more_pages? ⇒ Boolean Also known as: has_more_pages?

Returns:

  • (Boolean)


36
37
38
# File 'lib/pobo/dto/paginated_response.rb', line 36

def more_pages?
  @current_page < total_pages
end

#total_pages ⇒ Object



30
31
32
33
34
# File 'lib/pobo/dto/paginated_response.rb', line 30

def total_pages
  return 0 if @per_page.zero?

  (@total.to_f / @per_page).ceil
end