Class: Starwars::Base

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

Overview

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

Direct Known Subclasses

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

Class Method Details

.fetch(resources, id) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/starwars/base.rb', line 41

def fetch(resources, id)
  #
  # Special case for person and people, instead of removing the s
  #
  if (resources == 'people')
    resources = 'person'
  else
    resources.slice!(-1, 1)
  end
  Starwars.const_get(resources.capitalize).new(id: id).fetch
end

Instance Method Details

#==(other) ⇒ Object



21
22
23
# File 'lib/starwars/base.rb', line 21

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

#fetch(resource_name) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/starwars/base.rb', line 25

def fetch(resource_name)
  #
  # If the url is set, then fetch using the resource url
  #
  if url
    get(uri: url, as: FORMAT)
  #
  # Try to fetch using the resource name and ID
  #
  elsif id
    get(uri: "#{BASE_URL}/#{resource_name}/#{id}/", as: FORMAT)
  else
    fail 'error'
  end
end