Class: Scorpio::ResourceBase
- Inherits:
- 
      Object
      
        - Object
- Scorpio::ResourceBase
 
- Includes:
- JSI::FingerprintHash
- Defined in:
- lib/scorpio/resource_base.rb,
 lib/scorpio/pickle_adapter.rb
Defined Under Namespace
Modules: PickleAdapter
Instance Attribute Summary collapse
- 
  
    
      #attributes  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Returns the value of attribute attributes. 
- 
  
    
      #options  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Returns the value of attribute options. 
Class Method Summary collapse
- .all_schema_properties ⇒ Object
- .call_operation(operation, call_params: nil, model_attributes: nil) ⇒ Object
- .define_inheritable_accessor(accessor, options = {}) ⇒ Object
- .method_names_by_operation ⇒ Object
- 
  
    
      .openapi_document  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    the openapi document. 
- .openapi_document=(openapi_document) ⇒ Object
- .openapi_document_class ⇒ Object
- .operation_for_resource_class?(operation) ⇒ Boolean
- .operation_for_resource_instance?(operation) ⇒ Boolean
- .request_body_for_schema(object, schema) ⇒ Object
- .request_schema_fail(object, schema) ⇒ Object
- .response_object_to_instances(object, initialize_options = {}) ⇒ Object
- .tag_name ⇒ Object
- .tag_name=(tag_name) ⇒ Object
- .update_class_and_instance_api_methods ⇒ Object
- .update_dynamic_methods ⇒ Object
- .update_instance_accessors ⇒ Object
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #as_json(*opt) ⇒ Object
- #call_api_method(method_name, call_params: nil) ⇒ Object
- #call_operation(operation, call_params: nil) ⇒ Object
- #fingerprint ⇒ Object
- 
  
    
      #initialize(attributes = {}, options = {})  ⇒ ResourceBase 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    A new instance of ResourceBase. 
- #inspect ⇒ Object
- #persisted? ⇒ Boolean
- #pretty_print(q) ⇒ Object
Constructor Details
#initialize(attributes = {}, options = {}) ⇒ ResourceBase
Returns a new instance of ResourceBase.
| 451 452 453 454 455 | # File 'lib/scorpio/resource_base.rb', line 451 def initialize(attributes = {}, = {}) @attributes = JSI.stringify_symbol_keys(attributes) @options = JSI.stringify_symbol_keys() @persisted = !!@options['persisted'] end | 
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
| 457 458 459 | # File 'lib/scorpio/resource_base.rb', line 457 def attributes @attributes end | 
#options ⇒ Object (readonly)
Returns the value of attribute options.
| 458 459 460 | # File 'lib/scorpio/resource_base.rb', line 458 def @options end | 
Class Method Details
.all_schema_properties ⇒ Object
| 163 164 165 | # File 'lib/scorpio/resource_base.rb', line 163 def all_schema_properties represented_schemas.map(&:described_object_property_names).inject(Set.new, &:|) end | 
.call_operation(operation, call_params: nil, model_attributes: nil) ⇒ Object
| 257 258 259 260 261 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 | # File 'lib/scorpio/resource_base.rb', line 257 def call_operation(operation, call_params: nil, model_attributes: nil) call_params = JSI.stringify_symbol_keys(call_params) if call_params.is_a?(Hash) model_attributes = JSI.stringify_symbol_keys(model_attributes || {}) request = Scorpio::Request.new(operation) accessor_overridden = -> (accessor) do # an accessor is overridden if the default accessor getter (UnboundMethod) is the same # as the UnboundMethod returned from instance_method on the owner of that instance method. # gotta be the owner since different classes return different UnboundMethod instances for # the same method. for example, referring to models of scorpio/test/blog_scorpio_models.rb # with the server_variables instance method: # Article.instance_method(:server_variables) # => #<UnboundMethod: #<Class:Article>#server_variables> # returns a different UnboundMethod than # Scorpio::ResourceBase.instance_method(:server_variables) # => #<UnboundMethod: #<Class:Scorpio::ResourceBase>#server_variables> # even though they are really the same method (the #owner for both is Scorpio::ResourceBase) inheritable_accessor_defaults[accessor] != self.singleton_class.instance_method(accessor).owner.instance_method(accessor) end # pretty ugly... may find a better way to do this. 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) # assume that call_params must be included somewhere. model_attributes are a source of required things # but not required to be here. 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 # TODO deal with model_attributes / call_params better in nested whatever if call_params.nil? request.body_object = request_body_for_schema(model_attributes, operation.request_schema) elsif call_params.is_a?(Hash) body = request_body_for_schema(model_attributes.merge(call_params), operation.request_schema) request.body_object = body.merge(call_params) # TODO else request.body_object = call_params end else if other_params if METHODS_WITH_BODIES.any? { |m| m.to_s == operation.http_method.downcase.to_s } request.body_object = other_params else if other_params.is_a?(Hash) # TODO pay more attention to 'parameters' api method attribute request.query_params = other_params else raise end end end end ur = request.run_ur ur.raise_on_http_error = { '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, ) end | 
.define_inheritable_accessor(accessor, options = {}) ⇒ Object
| 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | # File 'lib/scorpio/resource_base.rb', line 12 def define_inheritable_accessor(accessor, = {}) if [:default_getter] # the value before the field is set (overwritten) is the result of the default_getter proc define_singleton_method(accessor, &[:default_getter]) else # the value before the field is set (overwritten) is the default_value (which is nil if not specified) default_value = [:default_value] define_singleton_method(accessor) { default_value } end inheritable_accessor_defaults[accessor] = self.singleton_class.instance_method(accessor) # field setter method. redefines the getter, replacing the method with one that returns the # setter's argument (that being inherited to the scope of the define_method(accessor) block define_singleton_method(:"#{accessor}=") do |value| # the setter operates on the singleton class of the receiver (self) singleton_class.instance_exec(value, self) do |value_, klass| # remove a previous getter. NameError is raised if a getter is not defined on this class; # this may be ignored. begin remove_method(accessor) rescue NameError end # getter method define_method(accessor) { value_ } # invoke on_set callback defined on the class if [:on_set] klass.instance_exec(&[:on_set]) end end end end | 
.method_names_by_operation ⇒ Object
| 219 220 221 222 223 224 225 226 227 228 229 230 231 | # File 'lib/scorpio/resource_base.rb', line 219 def method_names_by_operation @method_names_by_operation ||= Hash.new do |h, operation| h[operation] = begin raise(ArgumentError, operation.pretty_inspect) unless operation.is_a?(Scorpio::OpenAPI::Operation) if operation..respond_to?(:to_ary) && operation..include?(tag_name) && operation.operationId =~ /\A#{Regexp.escape(tag_name)}\.(\w+)\z/ method_name = $1 else method_name = operation.operationId end end end end | 
.openapi_document ⇒ Object
the openapi document
| 91 92 93 | # File 'lib/scorpio/resource_base.rb', line 91 def openapi_document nil end | 
.openapi_document=(openapi_document) ⇒ Object
| 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | # File 'lib/scorpio/resource_base.rb', line 98 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 #{self.inspect}") else raise(ArgumentError, "openapi_document may not be overridden on subclass #{self.inspect} after it was set on #{openapi_document_class.inspect}") end end update_dynamic_methods openapi_document.paths.each do |path, path_item| path_item.each do |http_method, operation| unless operation.is_a?(Scorpio::OpenAPI::Operation) next end end end # TODO blame validate openapi_document update_dynamic_methods end | 
.openapi_document_class ⇒ Object
| 94 95 96 | # File 'lib/scorpio/resource_base.rb', line 94 def openapi_document_class nil end | 
.operation_for_resource_class?(operation) ⇒ Boolean
| 182 183 184 185 186 187 188 189 190 191 192 | # File 'lib/scorpio/resource_base.rb', line 182 def operation_for_resource_class?(operation) return false unless tag_name return true if operation..respond_to?(:to_ary) && operation..include?(tag_name) if (operation.request_schemas || []).any? { |s| represented_schemas.include?(s) } return true end return false end | 
.operation_for_resource_instance?(operation) ⇒ Boolean
| 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | # File 'lib/scorpio/resource_base.rb', line 194 def operation_for_resource_instance?(operation) return false unless operation_for_resource_class?(operation) # define an instance method if the request schema is for this model request_resource_is_self = operation.request_schema && represented_schemas.include?(operation.request_schema) # also define an instance method depending on certain attributes the request description # might have in common with the model's schema attributes request_attributes = [] # if the path has attributes in common with model schema attributes, we'll define on # instance method request_attributes |= Addressable::Template.new(operation.path).variables # TODO if the method request schema has attributes in common with the model schema attributes, # should we define an instance method? #request_attributes |= request_schema && request_schema['type'] == 'object' && request_schema['properties'] ? # request_schema['properties'].keys : [] # TODO if the method parameters have attributes in common with the model schema attributes, # should we define an instance method? #request_attributes |= method_desc['parameters'] ? method_desc['parameters'].keys : [] schema_attributes = represented_schemas.map(&:described_object_property_names).inject(Set.new, &:|) return request_resource_is_self || (request_attributes & schema_attributes.to_a).any? end | 
.request_body_for_schema(object, schema) ⇒ Object
| 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | # File 'lib/scorpio/resource_base.rb', line 346 def request_body_for_schema(object, schema) if object.is_a?(Scorpio::ResourceBase) # TODO request_schema_fail unless schema is for given model type request_body_for_schema(object.attributes, schema) elsif object.is_a?(JSI::Base) request_body_for_schema(object.instance, schema) elsif object.is_a?(JSI::JSON::Node) request_body_for_schema(object.content, schema) else if object.is_a?(Hash) object.map do |key, value| if schema if schema['type'] == 'object' # TODO code dup with response_object_to_instances if schema['properties'] && schema['properties'][key] subschema = schema['properties'][key] include_pair = true else if schema['patternProperties'] _, pattern_schema = schema['patternProperties'].detect do |pattern, _| key =~ Regexp.new(pattern) # TODO map pattern to ruby syntax end end if pattern_schema subschema = pattern_schema include_pair = true else if schema['additionalProperties'] == false include_pair = false elsif schema['additionalProperties'] == nil # TODO decide on this (can combine with `else` if treating nil same as schema present) include_pair = true subschema = nil else include_pair = true subschema = schema['additionalProperties'] end end end elsif schema['type'] request_schema_fail(object, schema) else # TODO not sure include_pair = true subschema = nil end end if include_pair {key => request_body_for_schema(value, subschema)} else {} end end.inject({}, &:update) elsif object.is_a?(Array) || object.is_a?(Set) object.map do |el| if schema if schema['type'] == 'array' # TODO index based subschema or whatever else works for array subschema = schema['items'] elsif schema['type'] request_schema_fail(object, schema) end end request_body_for_schema(el, subschema) end else # TODO maybe raise on anything not serializable # TODO check conformance to schema, request_schema_fail if not object end end end | 
.request_schema_fail(object, schema) ⇒ Object
| 419 420 421 | # File 'lib/scorpio/resource_base.rb', line 419 def request_schema_fail(object, schema) # TODO blame end | 
.response_object_to_instances(object, initialize_options = {}) ⇒ Object
| 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 | # File 'lib/scorpio/resource_base.rb', line 423 def response_object_to_instances(object, = {}) if object.is_a?(JSI::Base) model = models_by_schema[object.schema] end if object.respond_to?(:to_hash) out = JSI::Typelike.modified_copy(object) do object.map do |key, value| {key => response_object_to_instances(value, )} end.inject({}, &:update) end if model model.new(out, ) else out end elsif object.respond_to?(:to_ary) JSI::Typelike.modified_copy(object) do object.map do |element| response_object_to_instances(element, ) end end else object end end | 
.tag_name ⇒ Object
| 134 135 136 | # File 'lib/scorpio/resource_base.rb', line 134 def tag_name nil end | 
.tag_name=(tag_name) ⇒ Object
| 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | # File 'lib/scorpio/resource_base.rb', line 138 def tag_name=(tag_name) unless tag_name.respond_to?(:to_str) raise(TypeError) end set_on_class = self 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
| 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | # File 'lib/scorpio/resource_base.rb', line 233 def update_class_and_instance_api_methods openapi_document.paths.each do |path, path_item| path_item.each do |http_method, operation| next if http_method == 'parameters' # parameters is not an operation. TOOD maybe just select the keys that are http methods? method_name = method_names_by_operation[operation] if method_name # class method 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 # instance method 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
| 158 159 160 161 | # File 'lib/scorpio/resource_base.rb', line 158 def update_dynamic_methods update_class_and_instance_api_methods update_instance_accessors end | 
.update_instance_accessors ⇒ Object
| 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | # File 'lib/scorpio/resource_base.rb', line 167 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
#[](key) ⇒ Object
| 464 465 466 | # File 'lib/scorpio/resource_base.rb', line 464 def [](key) @attributes[key] end | 
#[]=(key, value) ⇒ Object
| 468 469 470 | # File 'lib/scorpio/resource_base.rb', line 468 def []=(key, value) @attributes[key] = value end | 
#as_json(*opt) ⇒ Object
| 498 499 500 | # File 'lib/scorpio/resource_base.rb', line 498 def as_json(*opt) JSI::Typelike.as_json(@attributes, *opt) end | 
#call_api_method(method_name, call_params: nil) ⇒ Object
| 472 473 474 475 | # File 'lib/scorpio/resource_base.rb', line 472 def call_api_method(method_name, call_params: nil) operation = self.class.method_names_by_operation.invert[method_name] || raise(ArgumentError) call_operation(operation, call_params: call_params) end | 
#call_operation(operation, call_params: nil) ⇒ Object
| 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 | # File 'lib/scorpio/resource_base.rb', line 477 def call_operation(operation, call_params: nil) response = self.class.call_operation(operation, call_params: call_params, model_attributes: self.attributes) # if we're making a POST or PUT and the request schema is this resource, we'll assume that # the request is persisting this resource 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 | 
#fingerprint ⇒ Object
| 519 520 521 | # File 'lib/scorpio/resource_base.rb', line 519 def fingerprint {class: self.class, attributes: JSI::Typelike.as_json(@attributes)} end | 
#inspect ⇒ Object
| 502 503 504 | # File 'lib/scorpio/resource_base.rb', line 502 def inspect "\#<#{self.class.inspect} #{attributes.inspect}>" end | 
#persisted? ⇒ Boolean
| 460 461 462 | # File 'lib/scorpio/resource_base.rb', line 460 def persisted? @persisted end | 
#pretty_print(q) ⇒ Object
| 505 506 507 508 509 510 511 512 513 514 515 516 517 | # File 'lib/scorpio/resource_base.rb', line 505 def pretty_print(q) q.instance_exec(self) do |obj| text "\#<#{obj.class.inspect}" group_sub { nest(2) { breakable ' ' pp obj.attributes } } breakable '' text '>' end end |