Class: Pexels::PaginatedResponse
- Inherits:
-
Object
- Object
- Pexels::PaginatedResponse
show all
- Includes:
- Enumerable
- Defined in:
- lib/pexels/paginated_response.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
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
#page ⇒ Object
Returns the value of attribute page.
7
8
9
|
# File 'lib/pexels/paginated_response.rb', line 7
def page
@page
end
|
#per_page ⇒ Object
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_results ⇒ Object
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_page ⇒ Object
37
38
39
40
41
42
|
# File 'lib/pexels/paginated_response.rb', line 37
def next_page
return unless @next_page
request.params[:page] = (@next_page)
self.class.new(request.call)
end
|
#prev_page ⇒ Object
44
45
46
47
48
49
|
# File 'lib/pexels/paginated_response.rb', line 44
def prev_page
return unless @prev_page
request.params[:page] = (@next_page)
self.class.new(request.call)
end
|
#total_pages ⇒ Object
25
26
27
|
# File 'lib/pexels/paginated_response.rb', line 25
def total_pages
total_results.fdiv(per_page).ceil
end
|