class Starwars::Base

Base Class for fetching all the data from the remote api. All Classes should inhert from this class.

Abstract base class for the different resources. Provides some helper methods for the resources.

@abstract

Constants

BASE_URL
FORMAT

Public Class Methods

fetch(param) click to toggle source

Fetch an object by id or URL @param [String, Integer] param String for full resouce url or the resouce id @return [Person, Film, Planet, Specie, Starship, Vehicle] @example Fetch person by id

Starwars::Person.fetch(1)

@example Fetch person by url

Starwars::Person.fetch("http://swapi.co/api/people/2/")

@api public

# File lib/starwars/base.rb, line 38
def fetch(param)
  object = new(url: link(param))
  Starwars::Request.new(object.request_attributes).perform_request
end
fetch_all() click to toggle source

Fetch all resource data page by page @return [People, Films, Planets, Species, Starships, Vehicles] @example Fetch Planets page by page

Starwars::Planet.fetch_all.each do |page|
  puts page.items.size
end

@api public

# File lib/starwars/base.rb, line 51
def fetch_all
  klass_name =  Starwars.const_get(name.split('::').last).const_get('RESOURCE_NAME')
  object = Starwars.const_get("#{klass_name.capitalize}").new(url: "#{Starwars::Base::BASE_URL}/#{klass_name}/")
  Starwars::Request.new(resource: object, uri: object.url, params: {}).perform_request
end

Protected Class Methods

Public Instance Methods

==(other) click to toggle source

Overide the == method for any resource to check the ID and URL @param [People, Films, Planets, Species, Starships, Vehicles] other @return [Boolean] @example If id's and urls are equal

Starwars::Planet.new(id: 1, url: "/some") == Starwars::Planet.new(id: 1, url: "/some") #=> true

@example If id's and urls are equal

Starwars::Planet.new(id: 2, url: "/some") == Starwars::Planet.new(id: 1, url: "/some") #=> false

@example If id's and urls are equal

Starwars::Planet.new(id: 1, url: "/some1") == Starwars::Planet.new(id: 1, url: "/some") #=> false

@api public

# File lib/starwars/base.rb, line 112
def ==(other)
  id == other.id && url == other.url
end
fetch() click to toggle source

Fetch a resource if the ID or URL is set @return [Person, Film, Planet, Specie, Starship, Vehicle] @example Fetch person by id

Starwars::Person.new(id: 1).fetch

@example Fetch person by url

Starwars::Person.new(url: "http://swapi.co/api/people/2/").fetch

@api public

# File lib/starwars/base.rb, line 125
def fetch
  Starwars::Request.new(request_attributes).perform_request
end
request_attributes(opts = {}) click to toggle source

Generate the Request initialize params @param [Hash] opts the options to create a Request with. @option opts [Person, Film, Planet, Specie, Starship, Vehicle] :resource @option opts [String] :uri resource uri @option opts [Hash] :params other params @return [Hash] @api private

# File lib/starwars/base.rb, line 137
def request_attributes(opts = {})
  { resource: self, uri: link, params: {} }.merge(opts)
end

Protected Instance Methods