Class: Enginn::Resource Abstract
- Inherits:
-
Object
- Object
- Enginn::Resource
- Defined in:
- lib/enginn/resource.rb
Overview
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.
Direct Known Subclasses
Character, DictionaryEntry, Line, LineTag, Project, Scene, SynthesisExport, Take, TakeBatch, TestSynthesis
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#project ⇒ Object
readonly
Returns the value of attribute project.
Class Method Summary collapse
-
.path ⇒ String
private
Get the path to use for this kind of Resource.
Instance Method Summary collapse
-
#destroy ⇒ Boolean
Same as #destroy! but return false instead of raising an exception.
-
#destroy! ⇒ true
If the requested has succeeded.
-
#fetch ⇒ Boolean
Same as #fetch! but return false instead of raising an exception.
-
#fetch! ⇒ true
If the requested has succeeded.
-
#initialize(project, attributes = {}) ⇒ Resource
constructor
A new instance of Resource.
- #inspect ⇒ String
- #route ⇒ String private
-
#save ⇒ Boolean
Same as #save! but return false instead of raising an exception.
-
#save! ⇒ true
If the requested has succeeded.
Constructor Details
#initialize(project, attributes = {}) ⇒ Resource
Returns a new instance of Resource.
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
#attributes ⇒ Object
Returns the value of attribute attributes.
44 45 46 |
# File 'lib/enginn/resource.rb', line 44 def attributes @attributes end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
43 44 45 |
# File 'lib/enginn/resource.rb', line 43 def errors @errors end |
#project ⇒ Object (readonly)
Returns the value of attribute project.
43 44 45 |
# File 'lib/enginn/resource.rb', line 43 def project @project end |
Class Method Details
.path ⇒ String
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
39 40 41 |
# File 'lib/enginn/resource.rb', line 39 def self.path raise "path is not overriden for #{self}" end |
Instance Method Details
#destroy ⇒ Boolean
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.
97 98 99 100 |
# File 'lib/enginn/resource.rb', line 97 def destroy! request(:delete) true end |
#fetch ⇒ Boolean
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.
57 58 59 60 61 |
# File 'lib/enginn/resource.rb', line 57 def fetch! result = request(:get)[:result] sync_attributes_with(result) true end |
#inspect ⇒ String
115 116 117 |
# File 'lib/enginn/resource.rb', line 115 def inspect "#<#{self.class} #{@attributes.map { |name, value| "@#{name}=#{value.inspect}" }.join(', ')}>" end |
#route ⇒ String
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.
121 122 123 |
# File 'lib/enginn/resource.rb', line 121 def route "#{@project.route}/#{self.class.path}/#{@attributes[:id]}" end |
#save ⇒ Boolean
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.
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 |