Class: Enginn::Resource Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/enginn/resource.rb

Overview

This class is abstract.

Override the Resource.path method to implement.

A Resource can be a Character, a Take, or anything described in the Enginn API doc (app.enginn.tech/api/docs).

A Resource depends on a Client that will be used for actual HTTP operations and is relative to a parent Project (see #initialize parameters). When a Resource is fetched through #fetch or #save, any received attributes from the API is synced with the object such as it is available as an instance method.

A Resource whose attributes include an ID will be considered as already existing and as such, subsequent calls to #save will issue a PATCH request. Otherwise, a POST request will be issued instead, allowing the creation of a new Resource.

Examples:

character = Enginn::Character(project, { id: "00000000-0000-0000-0000-000000000000" })
character.name # NoMethodError
character.fetch!
character.name # => 'Rocky'
scene = Enginn::Scene.new(project, { name: 'Grand Finale' })
scene.save # POST request (i.e. a new scene created)
scene.id # => "00000000-0000-0000-0000-000000000000"
scene.name = 'The End'
scene.save # PATCH request (i.e. the scene is updated)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, attributes = {}) ⇒ Resource

Returns a new instance of Resource.

Parameters:

  • project (Enginn::Project)

    The parent project of this resource

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

    The attributes to initialize the resource with



48
49
50
51
52
53
# File 'lib/enginn/resource.rb', line 48

def initialize(project, attributes = {})
  @project = project
  @attributes = {}
  @errors = []
  sync_attributes_with(attributes || {})
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



44
45
46
# File 'lib/enginn/resource.rb', line 44

def attributes
  @attributes
end

#errorsObject (readonly)

Returns the value of attribute errors.



43
44
45
# File 'lib/enginn/resource.rb', line 43

def errors
  @errors
end

#projectObject (readonly)

Returns the value of attribute project.



43
44
45
# File 'lib/enginn/resource.rb', line 43

def project
  @project
end

Class Method Details

.pathString

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.

Get the path to use for this kind of Enginn::Resource

Returns:

  • (String)


39
40
41
# File 'lib/enginn/resource.rb', line 39

def self.path
  raise "path is not overriden for #{self}"
end

Instance Method Details

#destroyBoolean

Same as #destroy! but return false instead of raising an exception. Also fill in #errors with the server response.

Returns:

  • (Boolean)

See Also:



106
107
108
109
110
111
112
# File 'lib/enginn/resource.rb', line 106

def destroy
  destroy!
  true
rescue Faraday::Error => e
  @errors << e.response
  false
end

#destroy!true

Returns if the requested has succeeded.

Returns:

  • (true)

    if the requested has succeeded

Raises:

  • (Faraday::Error)

    if something goes wrong during the request



97
98
99
100
# File 'lib/enginn/resource.rb', line 97

def destroy!
  request(:delete)
  true
end

#fetchBoolean

Same as #fetch! but return false instead of raising an exception. Also fill in #errors with the server response.

Returns:

  • (Boolean)

See Also:



67
68
69
70
71
72
73
# File 'lib/enginn/resource.rb', line 67

def fetch
  fetch!
  true
rescue Faraday::Error => e
  @errors << e.response
  false
end

#fetch!true

Returns if the requested has succeeded.

Returns:

  • (true)

    if the requested has succeeded

Raises:

  • (Faraday::Error)

    if something goes wrong during the request



57
58
59
60
61
# File 'lib/enginn/resource.rb', line 57

def fetch!
  result = request(:get)[:result]
  sync_attributes_with(result)
  true
end

#inspectString

Returns:

  • (String)


115
116
117
# File 'lib/enginn/resource.rb', line 115

def inspect
  "#<#{self.class} #{@attributes.map { |name, value| "@#{name}=#{value.inspect}" }.join(', ')}>"
end

#routeString

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.

Returns:

  • (String)


121
122
123
# File 'lib/enginn/resource.rb', line 121

def route
  "#{@project.route}/#{self.class.path}/#{@attributes[:id]}"
end

#saveBoolean

Same as #save! but return false instead of raising an exception. Also fill in #errors with the server response.

Returns:

  • (Boolean)

See Also:



87
88
89
90
91
92
93
# File 'lib/enginn/resource.rb', line 87

def save
  save!
  true
rescue Faraday::Error => e
  @errors << e.response
  false
end

#save!true

Returns if the requested has succeeded.

Returns:

  • (true)

    if the requested has succeeded

Raises:

  • (Faraday::Error)

    if something goes wrong during the request



77
78
79
80
81
# File 'lib/enginn/resource.rb', line 77

def save!
  response = request(@attributes[:id].nil? ? :post : :patch)
  sync_attributes_with(response[:result])
  true
end