Class: Scorpio::ResourceBase
- Inherits:
-
Object
- Object
- Scorpio::ResourceBase
show all
- Includes:
- Containment
- Defined in:
- lib/scorpio/resource_base.rb,
lib/scorpio/resource_base.rb,
lib/scorpio/resource_base.rb,
lib/scorpio/resource_base.rb,
lib/scorpio/pickle_adapter.rb
Defined Under Namespace
Modules: Containment, PickleAdapter
Classes: Container
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#[], #[]=, #as_json, #inspect, #jsi_fingerprint, #pretty_print
Constructor Details
#initialize(attributes = {}, options = {}) ⇒ ResourceBase
Returns a new instance of ResourceBase.
453
454
455
456
457
458
459
460
|
# File 'lib/scorpio/resource_base.rb', line 453
def initialize(attributes = {}, options = {})
@attributes = JSI::Util.stringify_symbol_keys(attributes)
@options = JSI::Util.stringify_symbol_keys(options)
@persisted = !!@options['persisted']
@openapi_document_class = self.class.openapi_document_class
@subscript_memos = {}
end
|
Instance Attribute Details
#attributes ⇒ Object
Also known as:
contained_object
Returns the value of attribute attributes.
462
463
464
|
# File 'lib/scorpio/resource_base.rb', line 462
def attributes
@attributes
end
|
#options ⇒ Object
Returns the value of attribute options.
463
464
465
|
# File 'lib/scorpio/resource_base.rb', line 463
def options
@options
end
|
Class Method Details
.all_schema_properties ⇒ Object
146
147
148
|
# File 'lib/scorpio/resource_base.rb', line 146
def all_schema_properties
represented_schemas.map(&:described_object_property_names).inject(Set.new, &:merge)
end
|
.api_method_name_by_operation(operation) ⇒ String?
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
# File 'lib/scorpio/resource_base.rb', line 216
def api_method_name_by_operation(operation)
raise(ArgumentError, operation.pretty_inspect) unless operation.is_a?(Scorpio::OpenAPI::Operation)
tag_name_match = tag_name &&
operation.tags.respond_to?(:to_ary) && operation.tags.include?(tag_name) &&
operation.operationId &&
operation.operationId.match(/\A#{Regexp.escape(tag_name)}[\.\/\:](\w+)\z/)
if tag_name_match
tag_name_match[1]
else
operation.operationId
end
end
|
.call_operation(operation, call_params: nil, model_attributes: nil) ⇒ Object
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
# File 'lib/scorpio/resource_base.rb', line 262
def call_operation(operation, call_params: nil, model_attributes: nil)
call_params = JSI::Util.stringify_symbol_keys(call_params) if call_params.respond_to?(:to_hash)
model_attributes = JSI::Util.stringify_symbol_keys(model_attributes || {})
request = operation.build_request
accessor_overridden = -> (accessor) do
inheritable_accessor_defaults[accessor] != singleton_class.instance_method(accessor).owner.instance_method(accessor)
end
request.base_url = self.base_url if accessor_overridden.(:base_url)
request.server_variables = self.server_variables if accessor_overridden.(:server_variables)
request.server = self.server if accessor_overridden.(:server)
request.user_agent = self.user_agent if accessor_overridden.(:user_agent)
request.faraday_builder = self.faraday_builder if accessor_overridden.(:faraday_builder)
request.faraday_adapter = self.faraday_adapter if accessor_overridden.(:faraday_adapter)
request.path_params = request.path_template.variables.map do |var|
if call_params.respond_to?(:to_hash) && call_params.key?(var)
{var => call_params[var]}
elsif model_attributes.respond_to?(:to_hash) && model_attributes.key?(var)
{var => model_attributes[var]}
else
{}
end
end.inject({}, &:update)
if call_params.respond_to?(:to_hash)
unused_call_params = call_params.reject { |k, _| request.path_template.variables.include?(k) }
if !unused_call_params.empty?
other_params = unused_call_params
else
other_params = nil
end
else
other_params = call_params
end
if operation.request_schema
request_body_for_schema = -> (o) do
if o.is_a?(JSI::Base)
if o.jsi_schemas.include?(operation.request_schema)
jsi = o
else
jsi = operation.request_schema.new_jsi(o.jsi_node_content)
end
else
jsi = operation.request_schema.new_jsi(o)
end
jsi.jsi_select_descendents_leaf_first do |node|
node.jsi_schemas.empty? || !node.jsi_schemas.all? { |s| s.schema_content == false }
end
end
if call_params.nil?
request.body_object = request_body_for_schema.(model_attributes)
elsif call_params.respond_to?(:to_hash)
body = request_body_for_schema.(model_attributes)
request.body_object = body.merge(call_params) else
request.body_object = call_params
end
else
if other_params
if Request.method_with_body?(request.http_method)
request.body_object = other_params
else
if other_params.respond_to?(:to_hash)
request.query_params = other_params
else
raise
end
end
end
end
ur = request.run_ur
ur.raise_on_http_error
initialize_options = {
'persisted' => true,
'source' => {'operationId' => operation.operationId, 'call_params' => call_params, 'url' => ur.request.uri.to_s},
'ur' => ur,
}
response_object_to_instances(ur.response.body_object, initialize_options)
end
|
.define_inheritable_accessor(accessor, default_value: nil, default_getter: -> { default_value }, on_set: nil) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/scorpio/resource_base.rb', line 20
def define_inheritable_accessor(accessor, default_value: nil, default_getter: -> { default_value }, on_set: nil)
define_singleton_method(accessor, &default_getter)
inheritable_accessor_defaults[accessor] = singleton_class.instance_method(accessor)
define_singleton_method(:"#{accessor}=") do |value|
singleton_class.instance_exec(value, self) do |value_, klass|
begin
remove_method(accessor)
rescue NameError
end
define_method(accessor) { value_ }
if on_set
klass.instance_exec(&on_set)
end
end
end
end
|
.openapi_document ⇒ Object
86
87
88
|
# File 'lib/scorpio/resource_base.rb', line 86
def openapi_document
nil
end
|
.openapi_document=(openapi_document) ⇒ Object
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/scorpio/resource_base.rb', line 93
def openapi_document=(openapi_document)
openapi_document = OpenAPI::Document.from_instance(openapi_document)
begin
singleton_class.instance_exec { remove_method(:openapi_document) }
rescue NameError
end
begin
singleton_class.instance_exec { remove_method(:openapi_document_class) }
rescue NameError
end
openapi_document_class = self
define_singleton_method(:openapi_document) { openapi_document }
define_singleton_method(:openapi_document_class) { openapi_document_class }
define_singleton_method(:openapi_document=) do |_|
if self == openapi_document_class
raise(ArgumentError, "openapi_document may only be set once on #{inspect}")
else
raise(ArgumentError, "openapi_document may not be overridden on subclass #{inspect} after it was set on #{openapi_document_class.inspect}")
end
end
update_dynamic_methods
end
|
.openapi_document_class ⇒ Object
89
90
91
|
# File 'lib/scorpio/resource_base.rb', line 89
def openapi_document_class
nil
end
|
207
208
209
210
211
|
# File 'lib/scorpio/resource_base.rb', line 207
def operation_for_api_method_name(name)
openapi_document.operations.detect do |op|
operation_for_resource_class?(op) && api_method_name_by_operation(op) == name
end
end
|
.operation_for_resource_class?(operation) ⇒ Boolean
165
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/scorpio/resource_base.rb', line 165
def operation_for_resource_class?(operation)
return true if tag_name && operation.tags.respond_to?(:to_ary) && operation.tags.include?(tag_name)
request_response_schemas = operation.request_schemas | operation.response_schemas
all_request_response_schemas = request_response_schemas.each_inplace_applicator_schema(nil)
return true if all_request_response_schemas.any? { |s| represented_schemas.include?(s) }
return false
end
|
.operation_for_resource_instance?(operation) ⇒ Boolean
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
# File 'lib/scorpio/resource_base.rb', line 177
def operation_for_resource_instance?(operation)
return false unless operation_for_resource_class?(operation)
all_request_schemas = operation.request_schemas.each_inplace_applicator_schema(nil)
return true if all_request_schemas.any? { |s| represented_schemas.include?(s) }
return false unless tag_name && operation.tags.respond_to?(:to_ary) && operation.tags.include?(tag_name)
schema_attributes = represented_schemas.map(&:described_object_property_names).inject(Set.new, &:merge)
operation.inferred_parameters.each do |param|
if param['in'] == 'path' || param['in'] == 'query'
if schema_attributes.include?(param['name'])
return true
end
end
end
return false
end
|
.response_object_to_instances(object, initialize_options = {}) ⇒ Object
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
# File 'lib/scorpio/resource_base.rb', line 370
def response_object_to_instances(object, initialize_options = {})
if object.is_a?(JSI::Base)
models = object.jsi_schemas.map { |schema| models_by_schema[schema] }.compact
if models.size == 0
model = nil
elsif models.size == 1
model = models.first
else
raise(Scorpio::OpenAPI::Error, "multiple models indicated by response JSI. models: #{models.inspect}; object: #{object.pretty_inspect.chomp}")
end
if model && object.respond_to?(:to_hash)
model.new(object, initialize_options)
else
Container.new_container(object, openapi_document_class, initialize_options)
end
else
object
end
end
|
.tag_name ⇒ Object
118
119
120
|
# File 'lib/scorpio/resource_base.rb', line 118
def tag_name
nil
end
|
.tag_name=(tag_name) ⇒ Object
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/scorpio/resource_base.rb', line 122
def tag_name=(tag_name)
unless tag_name.respond_to?(:to_str)
raise(TypeError, "tag_name must be a string; got: #{tag_name.inspect}")
end
tag_name = tag_name.to_str
begin
singleton_class.instance_exec { remove_method(:tag_name) }
rescue NameError
end
define_singleton_method(:tag_name) { tag_name }
define_singleton_method(:tag_name=) do |tag_name|
unless tag_name == self.tag_name
raise(ArgumentError, "tag_name may not be overridden (to #{tag_name.inspect}). it is been set to #{self.tag_name.inspect}")
end
end
update_dynamic_methods
end
|
.update_class_and_instance_api_methods ⇒ Object
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
# File 'lib/scorpio/resource_base.rb', line 238
def update_class_and_instance_api_methods
openapi_document.paths.each do |path, path_item|
path_item.each do |http_method, operation|
next unless operation.is_a?(Scorpio::OpenAPI::Operation)
method_name = api_method_name_by_operation(operation)
if method_name
if operation_for_resource_class?(operation) && !respond_to?(method_name)
define_singleton_method(method_name) do |call_params = nil|
call_operation(operation, call_params: call_params)
end
end
if operation_for_resource_instance?(operation) && !method_defined?(method_name)
define_method(method_name) do |call_params = nil|
call_operation(operation, call_params: call_params)
end
end
end
end
end
end
|
.update_dynamic_methods ⇒ Object
141
142
143
144
|
# File 'lib/scorpio/resource_base.rb', line 141
def update_dynamic_methods
update_class_and_instance_api_methods
update_instance_accessors
end
|
.update_instance_accessors ⇒ Object
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
# File 'lib/scorpio/resource_base.rb', line 150
def update_instance_accessors
all_schema_properties.each do |property_name|
unless method_defined?(property_name)
define_method(property_name) do
self[property_name]
end
end
unless method_defined?(:"#{property_name}=")
define_method(:"#{property_name}=") do |value|
self[property_name] = value
end
end
end
end
|
Instance Method Details
#call_api_method(method_name, call_params: nil) ⇒ Object
471
472
473
474
|
# File 'lib/scorpio/resource_base.rb', line 471
def call_api_method(method_name, call_params: nil)
operation = self.class.operation_for_api_method_name(method_name) || raise(ArgumentError)
call_operation(operation, call_params: call_params)
end
|
#call_operation(operation, call_params: nil) ⇒ Object
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
|
# File 'lib/scorpio/resource_base.rb', line 476
def call_operation(operation, call_params: nil)
response = self.class.call_operation(operation, call_params: call_params, model_attributes: self.attributes)
request_resource_is_self = operation.request_schema && self.class.represented_schemas.include?(operation.request_schema)
if @options['ur'].is_a?(Scorpio::Ur)
response_schema = @options['ur'].response.response_schema
end
response_resource_is_self = response_schema && self.class.represented_schemas.include?(response_schema)
if request_resource_is_self && %w(put post).include?(operation.http_method.to_s.downcase)
@persisted = true
if response_resource_is_self
@attributes = response.attributes
end
end
response
end
|
#persisted? ⇒ Boolean
467
468
469
|
# File 'lib/scorpio/resource_base.rb', line 467
def persisted?
@persisted
end
|