class CyberCoach::Resource

A Resource can be created, read, updated and deleted.

Attributes

id[RW]

The identifier.

Public Instance Methods

initializable_with() click to toggle source
# File lib/cybercoach/resource.rb, line 99
def initializable_with
  super + [:id]
end

CRUD

↑ top

Public Instance Methods

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

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
delete(options = {}, invalidate = true) click to toggle source

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
update(options = {}, invalidate = true) click to toggle source

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

↑ top

Public Instance Methods

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.

  • id

    The identifier.

# File lib/cybercoach/resource.rb, line 79
def from_serializable(serializable)
  super(serializable)
  @id = serializable['id']
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.

  • id

    The identifier.

# File lib/cybercoach/resource.rb, line 93
def to_serializable
  serializable = super
  serializable['id'] = @id
  serializable
end