Class: CyberCoach::Resource

Inherits:
AbstractResource show all
Defined in:
lib/cybercoach/resource.rb

Overview

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

Direct Known Subclasses

Entry, Partnership, Sport, Subscription, User

Instance Attribute Summary collapse

Attributes inherited from AbstractResource

#uri

Instance Method Summary collapse

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

#idObject

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(_options = {})
  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(options = {})
  invalidate_uri
  invalidate_options
  options = @options.merge(options)
  response = self.class.delete(@uri, options)
  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_serializableObject

: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(options = {})
  invalidate_uri
  invalidate_options
  options = @options.merge(options).merge(
    body: serialize
  )
  response = self.class.put(@uri, options)
  if response.success?
    deserialize(response)
  else
    raise HttpError.new(response.response)
  end
end