Class: AsyncapiCable::Core::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/asyncapi_cable/core/document.rb

Constant Summary collapse

ASYNCAPI_VERSION =
"3.0.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(info:, servers: {}, cable_components: {}) ⇒ Document

Returns a new instance of Document.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/asyncapi_cable/core/document.rb', line 10

def initialize(info:, servers: {}, cable_components: {})
  @data = {
    "asyncapi" => ASYNCAPI_VERSION,
    "info" => deep_stringify(info),
    "channels" => {},
    "operations" => {},
    "components" => {
      "schemas" => deep_stringify(cable_components["schemas"] || {}),
      "messages" => {}
    }
  }
  @data["servers"] = deep_stringify(servers) unless servers.nil? || servers.empty?
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



8
9
10
# File 'lib/asyncapi_cable/core/document.rb', line 8

def data
  @data
end

Instance Method Details

#add_channel(channel_context) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/asyncapi_cable/core/document.rb', line 24

def add_channel(channel_context)
  channel_name = channel_name_for(channel_context)
  channel_entry = {"address" => channel_context.stream_template}
  # The exact Rails channel class name, so a generated client can use it
  # as the ActionCable subscription identifier without re-deriving it.
  if (klass = channel_context.channel_class) && klass.name
    channel_entry["x-actioncable-channel"] = klass.name
  end
  channel_entry["parameters"] = parameters_for(channel_context)
  channel_entry["messages"] = {}
  channel_entry.delete("parameters") if channel_entry["parameters"].empty?

  channel_context.operations.each do |op|
    next unless op.kind == :broadcast || op.kind == :publish

    op.messages.each do |component_class|
      message_name = component_class.component_name
      channel_entry["messages"][message_name] = {
        "$ref" => "#/components/messages/#{message_name}"
      }
      add_message_component(component_class)
    end

    add_operation(channel_name, op)
  end

  @data["channels"][channel_name] = channel_entry
end

#to_hObject



53
54
55
56
57
58
59
60
# File 'lib/asyncapi_cable/core/document.rb', line 53

def to_h
  result = @data.dup
  result["channels"] = sort_hash(result["channels"])
  result["operations"] = sort_hash(result["operations"])
  result["components"] = result["components"].transform_values { |v| sort_hash(v) }
  result.delete("components") if result["components"].values.all?(&:empty?)
  result
end

#to_json(*_args) ⇒ Object



64
65
66
67
# File 'lib/asyncapi_cable/core/document.rb', line 64

def to_json(*_args)
  require "json"
  JSON.pretty_generate(to_h)
end

#to_yamlObject



62
# File 'lib/asyncapi_cable/core/document.rb', line 62

def to_yaml = to_h.to_yaml