Class: Pexels::PaginatedResponse

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

Direct Known Subclasses

CollectionMediaSet, CollectionSet, PhotoSet, VideoSet

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ PaginatedResponse

Returns a new instance of PaginatedResponse.


14
15
16
17
18
19
20
21
22
23
# File 'lib/pexels/paginated_response.rb', line 14

def initialize(response)
  @response = response
  @attrs = @response.body

  @total_results = attrs.fetch('total_results', nil)
  @page = attrs.fetch('page')
  @per_page = attrs.fetch('per_page')
  @prev_page = attrs.fetch('prev_page', nil)
  @next_page = attrs.fetch('next_page', nil)
end

Instance Attribute Details

#pageObject (readonly)

Returns the value of attribute page.


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

def page
  @page
end

#per_pageObject (readonly)

Returns the value of attribute per_page.


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

def per_page
  @per_page
end

#total_resultsObject (readonly)

Returns the value of attribute total_results.


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

def total_results
  @total_results
end

Instance Method Details

#each(&block) ⇒ Object


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

def each(&block)
  if block_given?
    data.each(&block)
  else
    to_enum(:each)
  end
end

#next_pageObject


37
38
39
40
41
42
# File 'lib/pexels/paginated_response.rb', line 37

def next_page
  return unless @next_page

  request.params[:page] = extract_page(@next_page)
  self.class.new(request.call)
end

#prev_pageObject


44
45
46
47
48
49
# File 'lib/pexels/paginated_response.rb', line 44

def prev_page
  return unless @prev_page

  request.params[:page] = extract_page(@next_page)
  self.class.new(request.call)
end

#total_pagesObject


25
26
27
# File 'lib/pexels/paginated_response.rb', line 25

def total_pages
  total_results.fdiv(per_page).ceil
end