Class: CyberCoach::AbstractResource

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/cybercoach/abstract_resource.rb

Overview

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

Direct Known Subclasses

Resource, ResourcePage

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAbstractResource

Creates an AbstractResource.



31
32
33
34
35
# File 'lib/cybercoach/abstract_resource.rb', line 31

def initialize
  super
  @uri = nil
  @options = {}
end

Instance Attribute Details

#uriObject

The URI, the global identifier of it.



22
23
24
# File 'lib/cybercoach/abstract_resource.rb', line 22

def uri
  @uri
end

Instance Method Details

#deserialize(serialization) ⇒ Object

:category: Serialization

Deserializes it from a text based representation.

serialization

A text based representation.



79
80
81
# File 'lib/cybercoach/abstract_resource.rb', line 79

def deserialize(serialization)
  from_serializable(serialization)
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.



91
92
93
# File 'lib/cybercoach/abstract_resource.rb', line 91

def from_serializable(serializable)
  @uri = serializable['uri']
end

#plural_nameObject

:category: Configuration

Returns the plural name of the resource. E.g. ‘users’ or ‘entries’. This is used for parsing. Must be overridden in a subclass.



129
130
131
# File 'lib/cybercoach/abstract_resource.rb', line 129

def plural_name
  raise SubclassResponsibilityError.new
end

#read(options = {}) ⇒ Object

:category: CRUD

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.



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cybercoach/abstract_resource.rb', line 45

def read(options = {})
  invalidate_uri
  invalidate_options
  options = @options.merge(options)
  response = self.class.get(@uri, options)
  if response.success?
    deserialize(response)
  else
    raise HttpError.new(response.response)
  end
end

#resource_base_uriObject

:category: Configuration

Returns the base URI relative to the server.



138
139
140
# File 'lib/cybercoach/abstract_resource.rb', line 138

def resource_base_uri
  "#{Settings::BASE_URI}/#{plural_name}/"
end

#serializeObject

:category: Serialization

Returns a text based representations.



62
63
64
65
66
67
68
69
70
71
# File 'lib/cybercoach/abstract_resource.rb', line 62

def serialize
  format = self.class.default_options[:format]
  if format.nil?
    to_serializable
  elsif format == :json
    to_serializable.to_json
  else
    raise FormatNotSupportedError.new
  end
end

#singular_nameObject

:category: Configuration

Returns the singular name of the resource. E.g. ‘user’ or ‘entry’. This is used for parsing. Must be overridden in a subclass.



117
118
119
# File 'lib/cybercoach/abstract_resource.rb', line 117

def singular_name
  raise SubclassResponsibilityError.new
end

#to_serializableObject

:category: Serialization

Returns a serializable representation, which only contains simple data types. The hash has the keys:

  • uri

    The URI.



103
104
105
106
107
# File 'lib/cybercoach/abstract_resource.rb', line 103

def to_serializable
  serializable = {}
  serializable['uri'] = @uri
  serializable
end