Class: Starwars::Base Abstract
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- Starwars::Base
- 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.
Abstract base class for the different resources. Provides some helper methods for the resources.
Constant Summary collapse
- BASE_URL =
'http://swapi.co/api'
- FORMAT =
'application/json'
Class Method Summary collapse
-
.fetch(param) ⇒ Person, ...
Fetch an object by id or URL.
-
.fetch_all ⇒ People, ...
Fetch all resource data page by page.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Overide the == method for any resource to check the ID and URL.
-
#created ⇒ Time
The time the resource was created.
-
#edited ⇒ Time
The time the resource was edited.
-
#fetch ⇒ Person, ...
Fetch a resource if the ID or URL is set.
-
#id ⇒ Integer
The resource unique id.
-
#request_attributes(opts = {}) ⇒ Hash
private
Generate the Request initialize params.
-
#url ⇒ String
The hypermedia URL of this resource.
Methods inherited from OpenStruct
Class Method Details
.fetch(param) ⇒ Person, ...
Fetch an object by id or URL
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_all ⇒ People, ...
Fetch all resource data page by page
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
112 113 114 |
# File 'lib/starwars/base.rb', line 112 def ==(other) id == other.id && url == other.url end |
#created ⇒ Time
The time the resource was created
78 |
# File 'lib/starwars/base.rb', line 78 property :created, type: Time |
#edited ⇒ Time
The time the resource was edited
85 |
# File 'lib/starwars/base.rb', line 85 property :edited, type: Time |
#fetch ⇒ Person, ...
Fetch a resource if the ID or URL is set
125 126 127 |
# File 'lib/starwars/base.rb', line 125 def fetch Starwars::Request.new(request_attributes).perform_request end |
#id ⇒ Integer
The resource unique id
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
137 138 139 |
# File 'lib/starwars/base.rb', line 137 def request_attributes(opts = {}) { resource: self, uri: link, params: {} }.merge(opts) end |
#url ⇒ String
The hypermedia URL of this resource
99 |
# File 'lib/starwars/base.rb', line 99 property :url |