Class: Pexels::PaginatedResponse
- Inherits:
-
Object
- Object
- Pexels::PaginatedResponse
- Includes:
- Enumerable
- Defined in:
- lib/pexels/paginated_response.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#page ⇒ Object
readonly
Returns the value of attribute page.
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
-
#total_results ⇒ Object
readonly
Returns the value of attribute total_results.
Instance Method Summary collapse
- #each(&block) ⇒ Object
-
#initialize(response) ⇒ PaginatedResponse
constructor
A new instance of PaginatedResponse.
- #next_page ⇒ Object
- #prev_page ⇒ Object
- #total_pages ⇒ Object
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
#page ⇒ Object (readonly)
Returns the value of attribute page.
7 8 9 |
# File 'lib/pexels/paginated_response.rb', line 7 def page @page end |
#per_page ⇒ Object (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_results ⇒ Object (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_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] = extract_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] = extract_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 |