class Starwars::Cursor

A pagination container for starwars

Public Instance Methods

each()
Alias for: each_page
each_page() { |next_page| ... } click to toggle source

Yields the subsequent page responses to a given block @yieldparam [Starwars::Base] response @return [Enumerable, nil] @example 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)
}

@api public

# File lib/starwars/cursor.rb, line 93
def each_page
  return to_enum(__callee__) unless block_given?
  yield(next_page) until last_page?
end
Also aliased as: each
items()
Alias for: results
last_page?() click to toggle source

Checks wheather this is the last page @return [Boolean] @example are we at the last page?

Starwars::Planet.fetch_all.last_page?

@api public

# File lib/starwars/cursor.rb, line 59
def last_page?
  !next_page?
end
next_page() click to toggle source

Returns the next page @return [Starwars::Base] @example fetch the next page

page = Starwars::Planet.fetch_all
page.next_page

@api public

# File lib/starwars/cursor.rb, line 78
def next_page
  return if last_page?
  perform_request
end
next_page?() click to toggle source

Checks wheather this there is more pages @return [Boolean] @example do we have a next page?

Starwars::Planet.fetch_all.next_page?

@api public

# File lib/starwars/cursor.rb, line 68
def next_page?
  !self.next.nil?
end
number_of_pages() click to toggle source

A wrapper to get the pages count from the api @return [Integer] @example get the total number of pages

Starwars::Planet.fetch_all.number_of_pages

@example get the total number of pages

Starwars::Starship.fetch_all.number_of_pages

@api public

# File lib/starwars/cursor.rb, line 39
def number_of_pages
  self[:count]
end
results() click to toggle source

A wrapper to get the results within a page @note This wrapper is needed to define the items alias method @return [Array] @example Return the items array within the page

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

@api public

# File lib/starwars/cursor.rb, line 50
def results
  self[:results]
end
Also aliased as: items

Private Instance Methods

perform_request() click to toggle source

Delegate to the request class to perform fetch data from remote server @return [People, Films, Planets, Species, Starships, Vehicles] @api private

# File lib/starwars/cursor.rb, line 106
def perform_request
  Request.new(request_attributes(uri: self.next)).perform_request
end