class CyberCoach::Resource
A Resource
can be created, read, updated and deleted.
Attributes
The identifier.
Public Instance Methods
CyberCoach::AbstractResource#initializable_with
# File lib/cybercoach/resource.rb, line 99 def initializable_with super + [:id] end
CRUD
↑ topPublic Instance Methods
Creates it. Must be overridden in a subclass. May use PutCreateable
or PostCreateable
.
- options
-
A hash of options to send with the request.
- invalidate
-
Invalidates it when true, skips invalidation when false.
# File lib/cybercoach/resource.rb, line 20 def create(options = {}, invalidate = true) fail SubclassResponsibilityError end
Deletes it. Reads itself from the response. Raises HttpError
if the request is unsuccessful.
- options
-
A hash of options to send with the request.
- invalidate
-
Invalidates it when true, skips invalidation when false.
# File lib/cybercoach/resource.rb, line 57 def delete(options = {}, invalidate = true) if invalidate self.invalidate end options = @options.merge(options) response = self.class.delete(@uri, options) if response.success? deserialize(response) else fail HttpError, response.response end end
Updates it. Reads itself from the response. Raises HttpError
if the request is unsuccessful.
- options
-
A hash of options to send with the request.
- invalidate
-
Invalidates it when true, skips invalidation when false.
# File lib/cybercoach/resource.rb, line 33 def update(options = {}, invalidate = true) if invalidate self.invalidate end options = @options.merge(options).merge( body: serialize ) response = self.class.put(@uri, options) if response.success? deserialize(response) else fail HttpError, response.response end end
Serialization
↑ topPublic Instance Methods
Creates itself from a serializable representation, which only contains simple data types.
- serializable
-
A hash with the keys:
- uri
-
The URI.
- id
-
The identifier.
CyberCoach::AbstractResource#from_serializable
# File lib/cybercoach/resource.rb, line 79 def from_serializable(serializable) super(serializable) @id = serializable['id'] end
Returns a serializable representation, which only contains simple data types. The hash has the keys:
- uri
-
The URI.
- id
-
The identifier.
CyberCoach::AbstractResource#to_serializable
# File lib/cybercoach/resource.rb, line 93 def to_serializable serializable = super serializable['id'] = @id serializable end