Class: CyberCoach::Resource
- Inherits:
-
AbstractResource
- Object
- AbstractResource
- CyberCoach::Resource
- Defined in:
- lib/cybercoach/resource.rb
Overview
A Resource can be created, read, updated and deleted.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#id ⇒ Object
The identifier.
Attributes inherited from AbstractResource
Instance Method Summary collapse
-
#create(_options = {}) ⇒ Object
:category: CRUD.
-
#delete(options = {}) ⇒ Object
:category: CRUD.
-
#from_serializable(serializable) ⇒ Object
:category: Serialization.
-
#to_serializable ⇒ Object
:category: Serialization.
-
#update(options = {}) ⇒ Object
:category: CRUD.
Methods inherited from AbstractResource
#deserialize, #initialize, #plural_name, #read, #resource_base_uri, #serialize, #singular_name
Constructor Details
This class inherits a constructor from CyberCoach::AbstractResource
Instance Attribute Details
#id ⇒ Object
The identifier.
9 10 11 |
# File 'lib/cybercoach/resource.rb', line 9 def id @id end |
Instance Method Details
#create(_options = {}) ⇒ Object
:category: CRUD
Creates it. Must be overridden in a subclass. May use PutCreateable or PostCreateable.
- options
-
A hash of options to send with the request.
19 20 21 |
# File 'lib/cybercoach/resource.rb', line 19 def create( = {}) raise SubclassResponsibilityError.new end |
#delete(options = {}) ⇒ Object
:category: CRUD
Deletes it. Reads itself from the response. Raises HttpError if the request is unsuccessful.
- options
-
A hash of options to send with the request.
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/cybercoach/resource.rb', line 53 def delete( = {}) invalidate_uri = .merge() response = self.class.delete(@uri, ) if response.success? deserialize(response) else raise HttpError.new(response.response) end end |
#from_serializable(serializable) ⇒ Object
:category: Serialization
Creates itself from a serializable representation, which only contains simple data types.
- serializable
-
A hash with the keys:
- uri
-
The URI.
- id
-
The identifier.
74 75 76 77 |
# File 'lib/cybercoach/resource.rb', line 74 def from_serializable(serializable) super(serializable) @id = serializable['id'] end |
#to_serializable ⇒ Object
:category: Serialization
Returns a serializable representation, which only contains simple data types. The hash has the keys:
- uri
-
The URI.
- id
-
The identifier.
88 89 90 91 92 |
# File 'lib/cybercoach/resource.rb', line 88 def to_serializable serializable = super serializable['id'] = @id serializable end |
#update(options = {}) ⇒ Object
:category: CRUD
Updates it. Reads itself from the response. Raises HttpError if the request is unsuccessful.
- options
-
A hash of options to send with the request.
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/cybercoach/resource.rb', line 31 def update( = {}) invalidate_uri = .merge().merge( body: serialize ) response = self.class.put(@uri, ) if response.success? deserialize(response) else raise HttpError.new(response.response) end end |