Class: Bifrost::Client
- Inherits:
-
Object
- Object
- Bifrost::Client
- Defined in:
- lib/bifrost/client.rb,
lib/bifrost/client/version.rb
Constant Summary collapse
- VERSION =
"0.1.1"
Instance Attribute Summary collapse
-
#bifrost_server_url ⇒ Object
readonly
Returns the value of attribute bifrost_server_url.
-
#jwt_secret ⇒ Object
readonly
Returns the value of attribute jwt_secret.
Instance Method Summary collapse
- #broadcast(channel, event:, data: nil) ⇒ Object
-
#initialize(jwt_secret: ENV["JWT_SECRET"], bifrost_server_url: ENV["BIFROST_URL"]) ⇒ Client
constructor
A new instance of Client.
- #token_for(channels:) ⇒ Object
Constructor Details
#initialize(jwt_secret: ENV["JWT_SECRET"], bifrost_server_url: ENV["BIFROST_URL"]) ⇒ Client
Returns a new instance of Client.
10 11 12 13 |
# File 'lib/bifrost/client.rb', line 10 def initialize(jwt_secret: ENV["JWT_SECRET"], bifrost_server_url: ENV["BIFROST_URL"]) @jwt_secret = jwt_secret @bifrost_server_url = bifrost_server_url end |
Instance Attribute Details
#bifrost_server_url ⇒ Object (readonly)
Returns the value of attribute bifrost_server_url.
8 9 10 |
# File 'lib/bifrost/client.rb', line 8 def bifrost_server_url @bifrost_server_url end |
#jwt_secret ⇒ Object (readonly)
Returns the value of attribute jwt_secret.
8 9 10 |
# File 'lib/bifrost/client.rb', line 8 def jwt_secret @jwt_secret end |
Instance Method Details
#broadcast(channel, event:, data: nil) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/bifrost/client.rb', line 15 def broadcast(channel, event:, data: nil) data = { channel: channel, message: { event: event, data: data }, exp: Time.now.to_i + 60 } jwt = JWT.encode(data, jwt_secret, "HS512") uri = URI.parse(bifrost_server_url) uri.path = "/broadcast" ssl = uri.is_a?(URI::HTTPS) res = Net::HTTP.start(uri.host, uri.port, use_ssl: ssl) do |http| request = Net::HTTP::Post.new(uri.path) request.body = JSON.dump(token: jwt) request["Content-Type"] = "application/json" http.request(request) end if Integer(res.code) > 206 raise ServerError.new("Bifrost server responded with #{res.code}: #{res.body}") else JSON.parse(res.body, symbolize_names: true) end end |
#token_for(channels:) ⇒ Object
43 44 45 46 |
# File 'lib/bifrost/client.rb', line 43 def token_for(channels:) payload = { channels: channels, exp: Time.now.to_i + 60 } JWT.encode(payload, jwt_secret, "HS512") end |