Class: Async::REST::Representation

Inherits:
Object
  • Object
show all
Defined in:
lib/async/rest/representation.rb

Overview

A representation of a resource has a value at the time the representation was created or fetched. It is usually generated by performing an HTTP request.

REST components perform actions on a resource by using a representation to capture the current or intended state of that resource and transferring that representation between components. A representation is a sequence of bytes, plus representation metadata to describe those bytes. Other commonly used but less precise names for a representation include: document, file, and HTTP message entity, instance, or variant.

A representation consists of data, metadata describing the data, and, on occasion, metadata to describe the metadata (usually for the purpose of verifying message integrity). Metadata is in the form of name-value pairs, where the name corresponds to a standard that defines the value's structure and semantics. Response messages may include both representation metadata and resource metadata: information about the resource that is not specific to the supplied representation.

Defined Under Namespace

Modules: Mutable

Constant Summary collapse

WRAPPER =
Wrapper::JSON.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, value: nil, metadata: {}) ⇒ Representation

Returns a new instance of Representation.

Parameters:

  • resource (Resource)

    the RESTful resource that this representation is of.

  • metadata (Hash | HTTP::Headers) (defaults to: {})

    the metadata associated with the representation.

  • value (Object) (defaults to: nil)

    the value of the representation.



59
60
61
62
63
64
# File 'lib/async/rest/representation.rb', line 59

def initialize(resource, value: nil, metadata: {})
  @resource = resource
  
  @value = value
   = 
end

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.



83
84
85
# File 'lib/async/rest/representation.rb', line 83

def 
  
end

#resourceObject (readonly)

Returns the value of attribute resource.



82
83
84
# File 'lib/async/rest/representation.rb', line 82

def resource
  @resource
end

Class Method Details

.[](wrapper) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/async/rest/representation.rb', line 21

def self.[] wrapper
  klass = Class.new(self)
  
  if wrapper.is_a?(Class)
    wrapper = wrapper.new
  end
  
  klass.const_set(:WRAPPER, wrapper)
  
  return klass
end

.for(resource, response, &block) ⇒ Object

Instantiate a new representation from a resource and response.

If a block is given, it is called with the resource and response, and the return value is used as the representation.



48
49
50
51
52
53
54
# File 'lib/async/rest/representation.rb', line 48

def self.for(resource, response, &block)
  if block_given?
    return yield(resource, response, self)
  else
    return self.new(resource, value: response.read, metadata: response.headers)
  end
end

Instance Method Details

#[](**parameters) ⇒ Object



74
75
76
# File 'lib/async/rest/representation.rb', line 74

def [] **parameters
  self.with(parameters: parameters)
end

#closeObject



78
79
80
# File 'lib/async/rest/representation.rb', line 78

def close
  @resource.close
end

#inspectObject



127
128
129
# File 'lib/async/rest/representation.rb', line 127

def inspect
  "\#<#{self.class} #{@resource.path.inspect} value=#{@value.inspect}>"
end

#valueObject



100
101
102
# File 'lib/async/rest/representation.rb', line 100

def value
  @value ||= self.get
end

#value?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/async/rest/representation.rb', line 96

def value?
  !@value.nil?
end

#with(klass = nil, **options) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/async/rest/representation.rb', line 66

def with(klass = nil, **options)
  if klass
    klass.new(@resource.with(**options))
  else
    self.class.new(@resource.with(**options))
  end
end