Class: Starwars::Cursor

Inherits:
Base
  • Object
show all
Includes:
Enumerable
Defined in:
lib/starwars/cursor.rb

Overview

A pagination container for starwars

Direct Known Subclasses

Films, People, Planets, Species, Starships, Vehicles

Constant Summary

Constants inherited from Base

Base::BASE_URL, Base::FORMAT

Instance Method Summary collapse

Methods inherited from Base

#==, #created, #edited, fetch, #fetch, fetch_all, #id, #request_attributes, #url

Instance Method Details

#each_page {|response| ... } ⇒ Enumerable? Also known as: each

Yields the subsequent page responses to a given block

Examples:

fetch the next page

Starwars::Planet.fetch_all.each_page{|page| p page.items.map(&:name)}
Starwars::Planet.fetch_all.each{|page| p page.items.map(&:name)}
Starwars::Planet.fetch_all.each_with_index{|page, index|
  p page.items.map(&:name)
}

Yield Parameters:


93
94
95
96
# File 'lib/starwars/cursor.rb', line 93

def each_page
  return to_enum(__callee__) unless block_given?
  yield(next_page) until last_page?
end

#last_page?Boolean

Checks wheather this is the last page

Examples:

are we at the last page?

Starwars::Planet.fetch_all.last_page?

59
60
61
# File 'lib/starwars/cursor.rb', line 59

def last_page?
  !next_page?
end

#next_pageStarwars::Base

Returns the next page

Examples:

fetch the next page

page = Starwars::Planet.fetch_all
page.next_page

78
79
80
81
# File 'lib/starwars/cursor.rb', line 78

def next_page
  return if last_page?
  perform_request
end

#next_page?Boolean

Checks wheather this there is more pages

Examples:

do we have a next page?

Starwars::Planet.fetch_all.next_page?

68
69
70
# File 'lib/starwars/cursor.rb', line 68

def next_page?
  !self.next.nil?
end

#number_of_pagesInteger

A wrapper to get the pages count from the api

Examples:

get the total number of pages

Starwars::Planet.fetch_all.number_of_pages

get the total number of pages

Starwars::Starship.fetch_all.number_of_pages

39
40
41
# File 'lib/starwars/cursor.rb', line 39

def number_of_pages
  self['count']
end

#resultsArray Also known as: items

Note:

This wrapper is needed to define the items alias method

A wrapper to get the results within a page

Examples:

Return the items array within the page

Starwars::Planet.fetch_all.results
Starwars::Planet.fetch_all.items

50
51
52
# File 'lib/starwars/cursor.rb', line 50

def results
  self['results']
end