class CyberCoach::AbstractResource
An AbstractResource
can be read. Sets up serialization and deserialization using JSON.
Attributes
The URI, the global identifier of it.
Public Class Methods
Creates an AbstractResource
.
- hash
-
A hash of values to set on instance variables.
# File lib/cybercoach/abstract_resource.rb, line 37 def initialize(hash = {}) super() @options = {} initialize_with(hash) end
Protected Instance Methods
# File lib/cybercoach/abstract_resource.rb, line 180 def initializable_with [:uri] end
CRUD
↑ topPublic Instance Methods
Reads it. Gets the URI from the response and reads itself again. 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/abstract_resource.rb, line 62 def read(options = {}, invalidate = true) if invalidate self.invalidate end options = @options.merge(options) response = self.class.get(@uri, options) if response.success? deserialize(response) else fail HttpError, response.response end end
Configuration
↑ topPublic Instance Methods
Returns the plural name of the resource. E.g. ‘users’ or ‘entries’. This is used for parsing. Must be overridden in a subclass.
# File lib/cybercoach/abstract_resource.rb, line 147 def plural_name fail SubclassResponsibilityError end
Returns the base URI relative to the server.
# File lib/cybercoach/abstract_resource.rb, line 156 def resource_base_uri "#{Settings::BASE_URI}/#{plural_name}/" end
Returns the singular name of the resource. E.g. ‘user’ or ‘entry’. This is used for parsing. Must be overridden in a subclass.
# File lib/cybercoach/abstract_resource.rb, line 135 def singular_name fail SubclassResponsibilityError end
Invalidation
↑ topPublic Instance Methods
Invalidates the uri and the options.
# File lib/cybercoach/abstract_resource.rb, line 48 def invalidate invalidate_uri invalidate_options end
Protected Instance Methods
Recalculates the request options from the attributes. A template method called before a request is made.
# File lib/cybercoach/abstract_resource.rb, line 177 def invalidate_options end
Recalculates the URI from the attributes. A template method called before a request is made.
# File lib/cybercoach/abstract_resource.rb, line 168 def invalidate_uri end
Serialization
↑ topPublic Instance Methods
Deserializes it from a text based representation.
- serialization
-
A text based representation.
# File lib/cybercoach/abstract_resource.rb, line 97 def deserialize(serialization) from_serializable(serialization) end
Creates itself from a serializable representation, which only contains simple data types.
- serializable
-
A hash with the keys:
- uri
-
The URI.
# File lib/cybercoach/abstract_resource.rb, line 109 def from_serializable(serializable) @uri = serializable['uri'] end
Returns a text based representations.
# File lib/cybercoach/abstract_resource.rb, line 80 def serialize format = self.class.default_options[:format] if format.nil? to_serializable elsif format == :json to_serializable.to_json else fail FormatNotSupportedError end end
Returns a serializable representation, which only contains simple data types. The hash has the keys:
- uri
-
The URI.
# File lib/cybercoach/abstract_resource.rb, line 121 def to_serializable serializable = {} serializable['uri'] = @uri serializable end