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.
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
- #close ⇒ Object
-
#initialize(resource, metadata: {}, value: nil, wrapper: Wrapper::JSON.new) ⇒ Representation
constructor
A new instance of Representation.
- #inspect ⇒ Object
- #prepare_request(verb, payload) ⇒ Object
- #process_response(request, response) ⇒ Object
- #value ⇒ Object
- #value! ⇒ Object
- #value=(value) ⇒ Object
- #with(**parameters) ⇒ Object
Constructor Details
#initialize(resource, metadata: {}, value: nil, wrapper: Wrapper::JSON.new) ⇒ Representation
Returns a new instance of Representation.
41 42 43 44 45 46 47 |
# File 'lib/async/rest/representation.rb', line 41 def initialize(resource, metadata: {}, value: nil, wrapper: Wrapper::JSON.new) @resource = resource @wrapper = wrapper = @value = value end |
Instance Attribute Details
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
80 81 82 |
# File 'lib/async/rest/representation.rb', line 80 def end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
57 58 59 |
# File 'lib/async/rest/representation.rb', line 57 def resource @resource end |
#wrapper ⇒ Object (readonly)
Returns the value of attribute wrapper.
58 59 60 |
# File 'lib/async/rest/representation.rb', line 58 def wrapper @wrapper end |
Class Method Details
.for(*args, **options) ⇒ Object
33 34 35 |
# File 'lib/async/rest/representation.rb', line 33 def self.for(*args, **) self.new(Resource.for(*args), **) end |
Instance Method Details
#close ⇒ Object
53 54 55 |
# File 'lib/async/rest/representation.rb', line 53 def close @resource.close end |
#inspect ⇒ Object
105 106 107 |
# File 'lib/async/rest/representation.rb', line 105 def inspect "\#<#{self.class} #{@resource.inspect}: value=#{@value.inspect}>" end |
#prepare_request(verb, payload) ⇒ Object
60 61 62 |
# File 'lib/async/rest/representation.rb', line 60 def prepare_request(verb, payload) @resource.prepare_request(verb, payload, &@wrapper.method(:prepare_request)) end |
#process_response(request, response) ⇒ Object
64 65 66 |
# File 'lib/async/rest/representation.rb', line 64 def process_response(request, response) @wrapper.process_response(request, response) end |
#value ⇒ Object
93 94 95 |
# File 'lib/async/rest/representation.rb', line 93 def value @value ||= value! end |
#value! ⇒ Object
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/async/rest/representation.rb', line 82 def value! response = self.get if response.success? = response.headers @value = response.read else raise RequestFailure, "Could not fetch remote resource #{@resource}: #{response.status}!" end end |
#value=(value) ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/async/rest/representation.rb', line 97 def value= value if @value = value self.post(value) else self.delete end end |
#with(**parameters) ⇒ Object
49 50 51 |
# File 'lib/async/rest/representation.rb', line 49 def with(**parameters) self.class.new(@resource.with(parameters: parameters), wrapper: @wrapper) end |