Class: Starwars::Base Abstract

Inherits:
OpenStruct show all
Includes:
Roar::Client, Roar::Coercion, Roar::JSON
Defined in:
lib/starwars/base.rb

Overview

This class is abstract.

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.

Direct Known Subclasses

Cursor, Film, Person, Planet, Specie, Starship, Vehicle

Constant Summary collapse

BASE_URL =
'http://swapi.co/api'
FORMAT =
'application/json'

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from OpenStruct

#[]

Class Method Details

.fetch(param) ⇒ Person, ...

Fetch an object by id or URL

Examples:

Fetch person by id

Starwars::Person.fetch(1)

Fetch person by url

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

Parameters:

  • param (String, Integer)

    String for full resouce url or the resouce id

Returns:



38
39
40
41
# 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_allPeople, ...

Fetch all resource data page by page

Examples:

Fetch Planets page by page

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

Returns:



51
52
53
54
55
# 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

Instance Method Details

#==(other) ⇒ Boolean

Overide the == method for any resource to check the ID and URL

Examples:

If id’s and urls are equal

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

If id’s and urls are equal

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

If id’s and urls are equal

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

Parameters:

Returns:

  • (Boolean)


112
113
114
# File 'lib/starwars/base.rb', line 112

def ==(other)
  id == other.id && url == other.url
end

#createdTime

The time the resource was created

Examples:

film.created

Returns:

  • (Time)


78
# File 'lib/starwars/base.rb', line 78

property :created, type: Time

#editedTime

The time the resource was edited

Examples:

film.edited

Returns:

  • (Time)


85
# File 'lib/starwars/base.rb', line 85

property :edited, type: Time

#fetchPerson, ...

Fetch a resource if the ID or URL is set

Examples:

Fetch person by id

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

Fetch person by url

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

Returns:



125
126
127
# File 'lib/starwars/base.rb', line 125

def fetch
  Starwars::Request.new(request_attributes).perform_request
end

#idInteger

The resource unique id

Examples:

film.id #=> 1

Returns:

  • (Integer)


92
# File 'lib/starwars/base.rb', line 92

property :id, type: Integer

#request_attributes(opts = {}) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generate the Request initialize params

Parameters:

  • opts (Hash) (defaults to: {})

    the options to create a Request with.

Options Hash (opts):

Returns:

  • (Hash)


137
138
139
# File 'lib/starwars/base.rb', line 137

def request_attributes(opts = {})
  { resource: self, uri: link, params: {} }.merge(opts)
end

#urlString

The hypermedia URL of this resource

Examples:

film.url #=> 'http://swapi.co/api/films/1/'

Returns:

  • (String)


99
# File 'lib/starwars/base.rb', line 99

property :url