Class: Async::REST::Representation

Inherits:
Object
  • Object
show all
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

WRAPPER =
Wrapper::JSON

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, metadata: {}, value: nil, wrapper: self.class::WRAPPER.new) ⇒ 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 wtih teh representation.

  • value (Object) (defaults to: nil)

    the value of the representation.

  • wrapper (#prepare_request, #process_response) (defaults to: self.class::WRAPPER.new)

    the wrapper for encoding/decoding the request/response body.



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

#metadataObject (readonly)

Returns the value of attribute metadata.



112
113
114
# File 'lib/async/rest/representation.rb', line 112

def 
  @metadata
end

#resourceObject (readonly)

Returns the value of attribute resource.



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

def resource
  @resource
end

#wrapperObject (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, **options)
	representation = self.new(Resource.for(*args), **options)
	
	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

#closeObject



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

def close
	@resource.close
end

#inspectObject



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

#updateObject



153
154
155
# File 'lib/async/rest/representation.rb', line 153

def update
	@value = assign(@value)
end

#valueObject



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

Returns:

  • (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, **options)
	klass.new(@resource.with(**options), wrapper: @wrapper)
end