Class: Scorpio::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/scorpio/model.rb,
lib/scorpio/pickle_adapter.rb

Defined Under Namespace

Modules: PickleAdapter

Constant Summary collapse

MODULES_FOR_JSON_SCHEMA_TYPES =
{
  'object' => [Hash],
  'array' => [Array, Set],
  'string' => [String],
  'integer' => [Integer],
  'number' => [Numeric],
  'boolean' => [TrueClass, FalseClass],
  'null' => [NilClass],
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}, options = {}) ⇒ Model

Returns a new instance of Model.



358
359
360
361
362
# File 'lib/scorpio/model.rb', line 358

def initialize(attributes = {}, options = {})
  @attributes = Scorpio.stringify_symbol_keys(attributes)
  @options = Scorpio.stringify_symbol_keys(options)
  @persisted = !!@options['persisted']
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



364
365
366
# File 'lib/scorpio/model.rb', line 364

def attributes
  @attributes
end

#optionsObject (readonly)

Returns the value of attribute options.



365
366
367
# File 'lib/scorpio/model.rb', line 365

def options
  @options
end

Class Method Details

.all_schema_propertiesObject



86
87
88
89
90
91
92
93
# File 'lib/scorpio/model.rb', line 86

def all_schema_properties
  schemas_by_key.select { |k, _| schema_keys.include?(k) }.map do |schema_key, schema|
    unless schema['type'] == 'object'
      raise "schema key #{schema_key} for #{self} is not of type object - type must be object for Scorpio Model to represent this schema" # TODO class
    end
    schema['properties'].keys
  end.inject([], &:|)
end

.api_description_schemaObject



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/scorpio/model.rb', line 56

def api_description_schema
  @api_description_schema ||= begin
    rest = YAML.load_file(Pathname.new(__FILE__).join('../../../getRest.yml'))
    rest['schemas'].each do |name, schema_hash|
      # URI hax because google doesn't put a URI in the id field properly
      schema = JSON::Schema.new(schema_hash, Addressable::URI.parse(''))
      JSON::Validator.add_schema(schema)
    end
    rest['schemas']['RestDescription']
  end
end

.call_api_method(method_name, call_params: nil, model_attributes: nil) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/scorpio/model.rb', line 191

def call_api_method(method_name, call_params: nil, model_attributes: nil)
  call_params = Scorpio.stringify_symbol_keys(call_params || {})
  model_attributes = Scorpio.stringify_symbol_keys(model_attributes || {})
  method_desc = api_description['resources'][self.resource_name]['methods'][method_name]
  http_method = method_desc['httpMethod'].downcase.to_sym
  path_template = Addressable::Template.new(method_desc['path'])
  template_params = model_attributes.merge(call_params)
  missing_variables = path_template.variables - call_params.keys - model_attributes.keys
  if missing_variables.any?
    raise(ArgumentError, "path #{method_desc['path']} for method #{method_name} requires attributes " +
      "which were missing: #{missing_variables.inspect}")
  end
  empty_variables = path_template.variables.select { |v| template_params[v].to_s.empty? }
  if empty_variables.any?
    raise(ArgumentError, "path #{method_desc['path']} for method #{method_name} requires attributes " +
      "which were empty: #{empty_variables.inspect}")
  end
  path = path_template.expand(template_params)
  url = Addressable::URI.parse(base_url) + path
  # assume that call_params must be included somewhere. model_attributes are a source of required things
  # but not required to be here.
  other_params = call_params.reject { |k, _| path_template.variables.include?(k) }

  method_desc = (((api_description['resources'] || {})[resource_name] || {})['methods'] || {})[method_name]
  request_schema = deref_schema(method_desc['request'])
  if request_schema
    # TODO deal with model_attributes / call_params better in nested whatever
    body = request_body_for_schema(model_attributes.merge(call_params), request_schema)
    body.update(call_params)
  else
    if other_params.any?
      if METHODS_WITH_BODIES.any? { |m| m == http_method.downcase }
        body = other_params
      else
        # TODO pay more attention to 'parameters' api method attribute
        url.query_values = other_params
      end
    end
  end

  response = connection.run_request(http_method, url, body, nil)

  error_class = Scorpio.error_classes_by_status[response.status]
  error_class ||= if (400..499).include?(response.status)
    ClientError
  elsif (500..599).include?(response.status)
    ServerError
  elsif !response.success?
    HTTPError
  end
  if error_class
    message = "Error calling #{method_name} on #{self}:\n" + (response.env[:raw_body] || response.env.body)
    raise error_class.new(message).tap { |e| e.response = response }
  end

  response_schema = method_desc['response']
  response_object_to_instances(response.body, response_schema, 'persisted' => true)
end

.connectionObject



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/scorpio/model.rb', line 173

def connection
  Faraday.new do |c|
    unless faraday_request_middleware.any? { |m| [*m].first == :json }
      c.request :json
    end
    faraday_request_middleware.each do |m|
      c.request(*m)
    end
    c.adapter(*faraday_adapter)
    faraday_response_middleware.each do |m|
      c.response(*m)
    end
    unless faraday_response_middleware.any? { |m| [*m].first == :json }
      c.response :json, :content_type => /\bjson$/, :preserve_raw => true
    end
  end
end

.define_inheritable_accessor(accessor, options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/scorpio/model.rb', line 13

def define_inheritable_accessor(accessor, options = {})
  if options[:default_getter]
    define_singleton_method(accessor, &options[:default_getter])
  else
    default_value = options[:default_value]
    define_singleton_method(accessor) { default_value }
  end
  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 options[:on_set]
        klass.instance_exec(&options[:on_set])
      end
    end
    if options[:update_methods]
      update_dynamic_methods
    end
  end
end

.deref_schema(schema) ⇒ Object



159
160
161
# File 'lib/scorpio/model.rb', line 159

def deref_schema(schema)
  schema && schemas_by_id[schema['$ref']] || schema
end

.request_body_for_schema(object, schema) ⇒ Object



250
251
252
253
254
255
256
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
# File 'lib/scorpio/model.rb', line 250

def request_body_for_schema(object, schema)
  schema = deref_schema(schema)
  if object.is_a?(Scorpio::Model)
    # TODO request_schema_fail unless schema is for given model type 
    request_body_for_schema(object.represent_for_schema(schema), 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']
          else
            request_schema_fail(object, schema)
          end
        end
        request_body_for_schema(el, subschema)
      end
    else
      # TODO maybe raise on anything not jsonifiable 
      # TODO check conformance to schema, request_schema_fail if not
      object
    end
  end
end

.request_schema_fail(object, schema) ⇒ Object



320
321
322
# File 'lib/scorpio/model.rb', line 320

def request_schema_fail(object, schema)
  raise(RequestSchemaFailure, "object does not conform to schema.\nobject = #{object.inspect}\nschema = #{JSON.pretty_generate(schema, quirks_mode: true)}")
end

.response_object_to_instances(object, schema, initialize_options = {}) ⇒ Object



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
# File 'lib/scorpio/model.rb', line 324

def response_object_to_instances(object, schema, initialize_options = {})
  schema = deref_schema(schema)
  if schema
    if schema['type'] == 'object' && MODULES_FOR_JSON_SCHEMA_TYPES['object'].any? { |m| object.is_a?(m) }
      out = object.map do |key, value|
        schema_for_value = schema['properties'] && schema['properties'][key] ||
          if schema['patternProperties']
            _, pattern_schema = schema['patternProperties'].detect do |pattern, _|
              key =~ Regexp.new(pattern)
            end
            pattern_schema
          end ||
          schema['additionalProperties']
        {key => response_object_to_instances(value, schema_for_value, initialize_options)}
      end.inject(object.class.new, &:update)
      model = models_by_schema_id[schema['id']]
      if model
        model.new(out, initialize_options)
      else
        out
      end
    elsif schema['type'] == 'array' && MODULES_FOR_JSON_SCHEMA_TYPES['array'].any? { |m| object.is_a?(m) }
      object.map do |element|
        response_object_to_instances(element, schema['items'], initialize_options)
      end
    else
      object
    end
  else
    object
  end
end

.set_api_description(api_description) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/scorpio/model.rb', line 68

def set_api_description(api_description)
  JSON::Validator.validate!(api_description_schema, api_description)
  self.api_description = api_description
  (api_description['schemas'] || {}).each do |schema_key, schema|
    unless schema['id']
      raise ArgumentError, "schema #{schema_key} did not contain an id"
    end
    schemas_by_id[schema['id']] = schema
    schemas_by_key[schema_key] = schema
  end
  update_dynamic_methods
end

.update_class_and_instance_api_methodsObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/scorpio/model.rb', line 110

def update_class_and_instance_api_methods
  if self.resource_name && api_description
    resource_api_methods = ((api_description['resources'] || {})[resource_name] || {})['methods'] || {}
    resource_api_methods.each do |method_name, method_desc|
      # class method
      unless respond_to?(method_name)
        define_singleton_method(method_name) do |call_params = nil|
          call_api_method(method_name, call_params: call_params)
        end
      end

      # instance method
      unless method_defined?(method_name)
        request_schema = deref_schema(method_desc['request'])

        # define an instance method if the request schema is for this model 
        request_resource_is_self = request_schema &&
          request_schema['id'] &&
          schemas_by_key.any? { |key, as| as['id'] == request_schema['id'] && schema_keys.include?(key) }

        # 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(method_desc['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 = schema_keys.map do |schema_key|
          schema = schemas_by_key[schema_key]
          schema['type'] == 'object' && schema['properties'] ? schema['properties'].keys : []
        end.inject([], &:|)

        if request_resource_is_self || (request_attributes & schema_attributes).any?
          define_method(method_name) do |call_params = nil|
            call_api_method(method_name, call_params: call_params)
          end
        end
      end
    end
  end
end

.update_dynamic_methodsObject



81
82
83
84
# File 'lib/scorpio/model.rb', line 81

def update_dynamic_methods
  update_class_and_instance_api_methods
  update_instance_accessors
end

.update_instance_accessorsObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/scorpio/model.rb', line 95

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

#==(other) ⇒ Object Also known as: eql?



379
380
381
# File 'lib/scorpio/model.rb', line 379

def ==(other)
  @attributes == other.instance_eval { @attributes }
end

#[](key) ⇒ Object



371
372
373
# File 'lib/scorpio/model.rb', line 371

def [](key)
  @attributes[key]
end

#[]=(key, value) ⇒ Object



375
376
377
# File 'lib/scorpio/model.rb', line 375

def []=(key, value)
  @attributes[key] = value
end

#call_api_method(method_name, call_params: nil) ⇒ Object



383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/scorpio/model.rb', line 383

def call_api_method(method_name, call_params: nil)
  response = self.class.call_api_method(method_name, 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
  api_method = self.class.api_description['resources'][self.class.resource_name]['methods'][method_name]
  request_schema = self.class.deref_schema(api_method['request'])
  request_resource_is_self = request_schema &&
    request_schema['id'] &&
    self.class.schemas_by_key.any? { |key, as| as['id'] == request_schema['id'] && self.class.schema_keys.include?(key) }
  response_schema = self.class.deref_schema(api_method['response'])
  response_resource_is_self = response_schema &&
    response_schema['id'] &&
    self.class.schemas_by_key.any? { |key, as| as['id'] == response_schema['id'] && self.class.schema_keys.include?(key) }
  if request_resource_is_self && %w(PUT POST).include?(api_method['httpMethod'])
    @persisted = true

    if response_resource_is_self
      @attributes = response.attributes
    end
  end

  response
end

#hashObject



415
416
417
# File 'lib/scorpio/model.rb', line 415

def hash
  @attributes.hash
end

#persisted?Boolean

Returns:

  • (Boolean)


367
368
369
# File 'lib/scorpio/model.rb', line 367

def persisted?
  @persisted
end

#represent_for_schema(schema) ⇒ Object

TODO



409
410
411
# File 'lib/scorpio/model.rb', line 409

def represent_for_schema(schema)
  @attributes
end