Class: Lacerda::ConsumeSpecification

Inherits:
Specification show all
Defined in:
lib/lacerda/consume_specification.rb

Instance Attribute Summary

Attributes inherited from Specification

#schema, #service

Instance Method Summary collapse

Methods inherited from Specification

#objects

Constructor Details

#initialize(service, schema_or_file) ⇒ ConsumeSpecification

We will remove any properties from the schema that are not referencing a published object from another service, but that are just local definitions i.e.

{
  "type": "object",
  "definitions": {
    "message_properties": { ...},
    "service::message": {...}
    "
  }
  "properties": {
    "message_properties": { "$ref": "#/message_properties }, <- remove
    "service::message": { "$ref": "#/service::message" } <- keep
  }
}


24
25
26
27
28
29
30
# File 'lib/lacerda/consume_specification.rb', line 24

def initialize(service, schema_or_file)
  super
  return unless @schema['properties']
  @schema['properties'] = @schema['properties'].keep_if do |k, v|
    !!k.index(Lacerda::SCOPE_SEPARATOR)
  end
end

Instance Method Details

#object(name) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/lacerda/consume_specification.rb', line 51

def object(name)
  underscored_name = Lacerda.underscore(name)
  schema_dup = Lacerda.deep_copy(@schema)

  # It's critical to delete this object from the definitions
  # or else the json validator gem will go into an endless loop
  object_schema = schema_dup['definitions'].delete underscored_name.to_s

  msg = "Unknown object type: #{underscored_name.to_s.to_json} not in #{schema['definitions'].keys.to_json} - did you specify it in consume.mson?"
  raise Lacerda::Service::InvalidObjectTypeError.new(msg) unless object_schema

  # Copy the definitions of our schema into the schema for the
  # object in case its properties include json pointers
  object_schema['definitions'] = schema_dup['definitions']

  Lacerda::ConsumedObject.new(service, name, object_schema)
end

#object?(name) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
# File 'lib/lacerda/consume_specification.rb', line 46

def object?(name)
  underscored_name = Lacerda.underscore(name)
  !!@schema[:definitions][underscored_name]
end

#object_description_classObject



32
33
34
# File 'lib/lacerda/consume_specification.rb', line 32

def object_description_class
  Lacerda::ConsumedObject
end

#scoped_schema(service) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/lacerda/consume_specification.rb', line 36

def scoped_schema(service)
  service_name_prefix = Lacerda.underscore(service.name + Lacerda::SCOPE_SEPARATOR)

  filtered_schema = Lacerda.deep_copy(schema)
  filtered_schema['properties'].select! do |k|
    Lacerda.underscore(k).start_with?(service_name_prefix)
  end
  filtered_schema
end