Class: Lacerda::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/lacerda/service.rb,
lib/lacerda/service/error.rb

Overview

Models a service and its published objects as well as consumed objects. The app itself is part of an Infrastructure

Defined Under Namespace

Classes: InvalidObjectTypeError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(infrastructure, data_dir) ⇒ Service

Returns a new instance of Service.



12
13
14
15
16
17
# File 'lib/lacerda/service.rb', line 12

def initialize(infrastructure, data_dir)
  @infrastructure = infrastructure
  @data_dir = data_dir
  @name = File.basename(data_dir).underscore
  load_contracts
end

Instance Attribute Details

#consumeObject (readonly)

Returns the value of attribute consume.



10
11
12
# File 'lib/lacerda/service.rb', line 10

def consume
  @consume
end

#errorsObject (readonly)

Returns the value of attribute errors.



10
11
12
# File 'lib/lacerda/service.rb', line 10

def errors
  @errors
end

#infrastructureObject (readonly)

Returns the value of attribute infrastructure.



10
11
12
# File 'lib/lacerda/service.rb', line 10

def infrastructure
  @infrastructure
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/lacerda/service.rb', line 10

def name
  @name
end

#publishObject (readonly)

Returns the value of attribute publish.



10
11
12
# File 'lib/lacerda/service.rb', line 10

def publish
  @publish
end

Instance Method Details

#consume_object(type, data) ⇒ Object



102
103
104
105
# File 'lib/lacerda/service.rb', line 102

def consume_object(type, data)
  object_description = @consume.object(type)
  Blumquist.new(schema: object_description.schema, data: data)
end

#consume_object_from(service_name, type, data) ⇒ Object



98
99
100
# File 'lib/lacerda/service.rb', line 98

def consume_object_from(service_name, type, data)
  consume_object([service_name, type].join(Lacerda::SCOPE_SEPARATOR), data)
end

#consumed_objects(publisher = nil) ⇒ Object



29
30
31
32
33
34
# File 'lib/lacerda/service.rb', line 29

def consumed_objects(publisher = nil)
  @consume.objects.select do |o|
    next if o.publisher_name.blank?
    publisher.blank? or o.publisher == publisher
  end
end

#consumersObject



23
24
25
26
27
# File 'lib/lacerda/service.rb', line 23

def consumers
  infrastructure.services.values.select do |service|
    service.consuming_from.include?(self)
  end
end

#consumes?(object_name) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/lacerda/service.rb', line 40

def consumes?(object_name)
  @consume.object?(object_name.to_s)
end

#consumes_from?(service_name, object_name) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/lacerda/service.rb', line 44

def consumes_from?(service_name, object_name)
  @consume.object?([service_name, object_name].join(Lacerda::SCOPE_SEPARATOR))
end

#consuming_fromObject



19
20
21
# File 'lib/lacerda/service.rb', line 19

def consuming_from
  consumed_objects.map(&:publisher).uniq
end

#published_objectsObject



48
49
50
# File 'lib/lacerda/service.rb', line 48

def published_objects
  @publish.objects
end

#publishes?(object_name) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/lacerda/service.rb', line 36

def publishes?(object_name)
  @publish.object?(object_name.to_s)
end

#satisfies?(service, reporter = nil) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/lacerda/service.rb', line 52

def satisfies?(service, reporter = nil)
  Lacerda.validate_reporter(reporter)
  @publish.satisfies?(service, reporter)
end

#satisfies_consumers?(options = {}) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
66
67
# File 'lib/lacerda/service.rb', line 57

def satisfies_consumers?(options = {})
  reporter = Lacerda.validate_reporter(options.fetch(:reporter, nil))

  @errors = {}
  consumers.each do |consumer|
    @publish.satisfies?(consumer, reporter)
    next if @publish.errors.empty?
    @errors["#{name} -> #{consumer.name}"] = @publish.errors
  end
  @errors.empty?
end

#validate_internal_publish_object!(type, data) ⇒ Object



81
82
83
84
# File 'lib/lacerda/service.rb', line 81

def validate_internal_publish_object!(type, data)
  object_description = @publish.object(type, scoped: false)
  object_description.validate_data!(data)
end

#validate_object_to_consume(type, data) ⇒ Object



86
87
88
89
90
91
# File 'lib/lacerda/service.rb', line 86

def validate_object_to_consume(type, data)
  validate_object_to_consume!(type, data)
  true
rescue
  false
end

#validate_object_to_consume!(type, data) ⇒ Object



93
94
95
96
# File 'lib/lacerda/service.rb', line 93

def validate_object_to_consume!(type, data)
  object_description = @consume.object(type)
  object_description.validate_data!(data)
end

#validate_object_to_publish(type, data) ⇒ Object



69
70
71
72
73
74
# File 'lib/lacerda/service.rb', line 69

def validate_object_to_publish(type, data)
  validate_object_to_publish!(type, data)
  true
rescue
  false
end

#validate_object_to_publish!(type, data) ⇒ Object



76
77
78
79
# File 'lib/lacerda/service.rb', line 76

def validate_object_to_publish!(type, data)
  object_description = @publish.object(type)
  object_description.validate_data!(data)
end