Class: FulfilApi::Resource

Inherits:
Object
  • Object
show all
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

CustomerShipment

Defined Under Namespace

Modules: AttributeAssignable, Comparable, Persistable, Serializable Classes: AttributeType, Errors, ModelNameMissing, NotFound

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Serializable

#as_json, #to_json

Methods included from Persistable

#create, #create!, #save, #save!, #update, #update!

Methods included from Comparable

#==, #eql?, #hash

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

.relationFulfilApi::Relation

Note:

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.

Examples:

forwarding of the .where class method

FulfilApi::Resource.set(model_name: "sale.sale").find_by(["id", "=", 100])

Returns:



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.

Parameters:

  • attribute_name (String, Symbol)

    The name of the attribute

Returns:

  • (Any, nil)


55
56
57
# File 'lib/fulfil_api/resource.rb', line 55

def [](attribute_name)
  @attributes[attribute_name]
end

#errorsFulfilApi::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

#idInteger?

The #id is a shorthand to easily grab the ID of an API resource.

Returns:

  • (Integer, nil)


70
71
72
# File 'lib/fulfil_api/resource.rb', line 70

def id
  @attributes["id"]
end

#to_hHash

Returns all currently assigned attributes for a FulfilApi::Resource.

Returns:

  • (Hash)


77
78
79
# File 'lib/fulfil_api/resource.rb', line 77

def to_h
  @attributes
end