Class: FulfilApi::Resource
- Inherits:
-
Object
- Object
- FulfilApi::Resource
- Includes:
- AttributeAssignable, Comparable, Persistable, Serializable
- Defined in:
- lib/fulfil_api/resource.rb,
lib/fulfil_api/resource/errors.rb,
lib/fulfil_api/resource/comparable.rb,
lib/fulfil_api/resource/persistable.rb,
lib/fulfil_api/resource/serializable.rb,
lib/fulfil_api/resource/attribute_type.rb,
lib/fulfil_api/resource/attribute_assignable.rb
Overview
The Resource represents a single resource returned by the API
endpoints of Fulfil.
Direct Known Subclasses
Defined Under Namespace
Modules: AttributeAssignable, Comparable, Persistable, Serializable Classes: AttributeType, Errors, ModelNameMissing, NotFound
Class Method Summary collapse
-
.relation ⇒ FulfilApi::Relation
Builds a new Fulfil::Resource::Relation based on the current class to enable us to chain requests to Fulfil without querying their API endpoints multiple times in a row.
Instance Method Summary collapse
-
#[](attribute_name) ⇒ Any?
Looks up the value for the given attribute name.
-
#errors ⇒ FulfilApi::Resource::Errors
Builds a structure for keeping track of any errors when trying to use the persistance methods for the API resource.
-
#id ⇒ Integer?
The #id is a shorthand to easily grab the ID of an API resource.
-
#initialize(attributes = {}) ⇒ Resource
constructor
A new instance of Resource.
-
#to_h ⇒ Hash
Returns all currently assigned attributes for a Resource.
Methods included from Serializable
Methods included from Persistable
#create, #create!, #save, #save!, #update, #update!
Methods included from Comparable
Methods included from AttributeAssignable
#assign_attribute, #assign_attributes
Constructor Details
#initialize(attributes = {}) ⇒ Resource
Returns a new instance of Resource.
22 23 24 25 26 27 28 29 |
# File 'lib/fulfil_api/resource.rb', line 22 def initialize(attributes = {}) attributes.deep_stringify_keys! @attributes = {}.with_indifferent_access @model_name = attributes.delete("model_name").presence || raise(ModelNameMissing) assign_attributes(attributes) end |
Class Method Details
.relation ⇒ FulfilApi::Relation
it makes use of the delegate_missing_to method from ActiveSupport to ensure that all unknown class methods for the FulfilApi::Resource are forwarded to the relation.
Builds a new Fulfil::Resource::Relation based on the current class to
enable us to chain requests to Fulfil without querying their API endpoints
multiple times in a row.
46 47 48 |
# File 'lib/fulfil_api/resource.rb', line 46 def relation Relation.new(self) end |
Instance Method Details
#[](attribute_name) ⇒ Any?
Looks up the value for the given attribute name.
55 56 57 |
# File 'lib/fulfil_api/resource.rb', line 55 def [](attribute_name) @attributes[attribute_name] end |
#errors ⇒ FulfilApi::Resource::Errors
Builds a structure for keeping track of any errors when trying to use the
persistance methods for the API resource.
63 64 65 |
# File 'lib/fulfil_api/resource.rb', line 63 def errors @errors ||= Errors.new(self) end |
#id ⇒ Integer?
The #id is a shorthand to easily grab the ID of an API resource.
70 71 72 |
# File 'lib/fulfil_api/resource.rb', line 70 def id @attributes["id"] end |
#to_h ⇒ Hash
Returns all currently assigned attributes for a FulfilApi::Resource.
77 78 79 |
# File 'lib/fulfil_api/resource.rb', line 77 def to_h @attributes end |