Class: Async::REST::Representation
- Inherits:
-
Object
- Object
- Async::REST::Representation
- Defined in:
- lib/async/rest/representation.rb
Overview
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.
Constant Summary collapse
Instance Attribute Summary collapse
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
-
#wrapper ⇒ Object
readonly
Returns the value of attribute wrapper.
Class Method Summary collapse
Instance Method Summary collapse
- #[](**parameters) ⇒ Object
- #assign(value) ⇒ Object
- #call(value) ⇒ Object
- #close ⇒ Object
-
#initialize(resource, metadata: {}, value: nil, wrapper: self.class::WRAPPER.new) ⇒ Representation
constructor
A new instance of Representation.
- #inspect ⇒ Object
- #prepare_request(verb, payload) ⇒ Object
-
#process_response(request, response) ⇒ Object
If an exception propagates out of this method, the response will be closed.
- #update ⇒ Object
- #value ⇒ Object
- #value! ⇒ Object
- #value=(value) ⇒ Object
- #value? ⇒ Boolean
- #with(klass = self.class, **options) ⇒ Object
Constructor Details
#initialize(resource, metadata: {}, value: nil, wrapper: self.class::WRAPPER.new) ⇒ Representation
Returns a new instance of Representation.
61 62 63 64 65 66 67 |
# File 'lib/async/rest/representation.rb', line 61 def initialize(resource, metadata: {}, value: nil, wrapper: self.class::WRAPPER.new) @resource = resource @wrapper = wrapper @metadata = @value = value end |
Instance Attribute Details
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
112 113 114 |
# File 'lib/async/rest/representation.rb', line 112 def @metadata end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
81 82 83 |
# File 'lib/async/rest/representation.rb', line 81 def resource @resource end |
#wrapper ⇒ Object (readonly)
Returns the value of attribute wrapper.
82 83 84 |
# File 'lib/async/rest/representation.rb', line 82 def wrapper @wrapper end |
Class Method Details
.[](wrapper) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/async/rest/representation.rb', line 33 def self.[] wrapper klass = Class.new(Representation) klass.const_set(:WRAPPER, wrapper) return klass end |
.for(*args, **options) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/async/rest/representation.rb', line 41 def self.for(*args, **) representation = self.new(Resource.for(*args), **) return representation unless block_given? Async do begin yield representation ensure representation.close end end end |
Instance Method Details
#[](**parameters) ⇒ Object
73 74 75 |
# File 'lib/async/rest/representation.rb', line 73 def [] **parameters self.with(parameters: parameters) end |
#assign(value) ⇒ Object
145 146 147 148 149 150 151 |
# File 'lib/async/rest/representation.rb', line 145 def assign(value) response = self.call(value) response.read return @value end |
#call(value) ⇒ Object
137 138 139 140 141 142 143 |
# File 'lib/async/rest/representation.rb', line 137 def call(value) if value self.post(value) else self.delete end end |
#close ⇒ Object
77 78 79 |
# File 'lib/async/rest/representation.rb', line 77 def close @resource.close end |
#inspect ⇒ Object
157 158 159 |
# File 'lib/async/rest/representation.rb', line 157 def inspect "\#<#{self.class} #{@resource.inspect}: value=#{@value.inspect}>" end |
#prepare_request(verb, payload) ⇒ Object
84 85 86 |
# File 'lib/async/rest/representation.rb', line 84 def prepare_request(verb, payload) @resource.prepare_request(verb, payload, &@wrapper.method(:prepare_request)) end |
#process_response(request, response) ⇒ Object
If an exception propagates out of this method, the response will be closed.
89 90 91 |
# File 'lib/async/rest/representation.rb', line 89 def process_response(request, response) @wrapper.process_response(request, response) end |
#update ⇒ Object
153 154 155 |
# File 'lib/async/rest/representation.rb', line 153 def update @value = assign(@value) end |
#value ⇒ Object
129 130 131 |
# File 'lib/async/rest/representation.rb', line 129 def value @value ||= value! end |
#value! ⇒ Object
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/async/rest/representation.rb', line 114 def value! response = self.get if response.success? @metadata = response.headers @value = response.read else raise ResponseError, response end end |
#value=(value) ⇒ Object
133 134 135 |
# File 'lib/async/rest/representation.rb', line 133 def value= value @value = self.assign(value) end |
#value? ⇒ Boolean
125 126 127 |
# File 'lib/async/rest/representation.rb', line 125 def value? !@value.nil? end |
#with(klass = self.class, **options) ⇒ Object
69 70 71 |
# File 'lib/async/rest/representation.rb', line 69 def with(klass = self.class, **) klass.new(@resource.with(**), wrapper: @wrapper) end |