Class: Turbo::Train::FanoutServer

Inherits:
BaseServer show all
Defined in:
lib/turbo/train/fanout_server.rb

Instance Attribute Summary

Attributes inherited from BaseServer

#configuration

Instance Method Summary collapse

Methods inherited from BaseServer

#initialize, #listen_url, #publish_url

Constructor Details

This class inherits a constructor from Turbo::Train::BaseServer

Instance Method Details

#publish(topics:, data:) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/turbo/train/fanout_server.rb', line 4

def publish(topics:, data:)
  uri = URI(server_config.publish_url)
  req = Net::HTTP::Post.new(uri)
  req['Fastly-Key'] = server_config.fastly_key

  message = data[:data].gsub("\n", "")
  payload = {items: []}

  Array(topics).each do |topic|
    payload[:items] << {channel: topic, formats: { 'http-stream': { content: "event: message\ndata: #{message}\n\n" } } }
  end

  req.body = payload.to_json

  opts = {
    use_ssl: uri.scheme == 'https'
  }

  if configuration.skip_ssl_verification
    opts[:verify_mode] = OpenSSL::SSL::VERIFY_NONE
  end

  Net::HTTP.start(uri.host, uri.port, opts) do |http|
    http.request(req)
  end
end

#server_configObject



31
32
33
# File 'lib/turbo/train/fanout_server.rb', line 31

def server_config
  configuration.fanout
end