Class: Turbo::Train::MercureServer

Inherits:
BaseServer show all
Defined in:
lib/turbo/train/mercure_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
# File 'lib/turbo/train/mercure_server.rb', line 4

def publish(topics:, data:)
  payload = { mercure: { publish: topics } }
  token = JWT.encode payload, server_config.publisher_key, ALGORITHM

  uri = URI(publish_url)

  req = Net::HTTP::Post.new(uri)
  req['Content-Type'] = 'application/x-www-form-urlencoded'
  req['Authorization'] = "Bearer #{token}"

  req.body = URI.encode_www_form(data)
  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



28
29
30
# File 'lib/turbo/train/mercure_server.rb', line 28

def server_config
  configuration.mercure
end