class CyberCoach::AbstractResource

An AbstractResource can be read. Sets up serialization and deserialization using JSON.

Attributes

uri[RW]

The URI, the global identifier of it.

Public Class Methods

new(hash = {}) click to toggle source

Creates an AbstractResource.

hash

A hash of values to set on instance variables.

Calls superclass method
# File lib/cybercoach/abstract_resource.rb, line 37
def initialize(hash = {})
  super()
  @options = {}
  initialize_with(hash)
end

Protected Instance Methods

initializable_with() click to toggle source
# File lib/cybercoach/abstract_resource.rb, line 180
def initializable_with
  [:uri]
end

CRUD

↑ top

Public Instance Methods

read(options = {}, invalidate = true) click to toggle source

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

↑ top

Public Instance Methods

plural_name() click to toggle source

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
resource_base_uri() click to toggle source

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
singular_name() click to toggle source

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

↑ top

Public Instance Methods

invalidate() click to toggle source

Invalidates the uri and the options.

# File lib/cybercoach/abstract_resource.rb, line 48
def invalidate
  invalidate_uri
  invalidate_options
end

Protected Instance Methods

invalidate_options() click to toggle source

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
invalidate_uri() click to toggle source

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

↑ top

Public Instance Methods

deserialize(serialization) click to toggle source

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
from_serializable(serializable) click to toggle source

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
serialize() click to toggle source

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
to_serializable() click to toggle source

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