Class: AnyCable::Rack::BroadcastSubscribers::HTTPSubscriber

Inherits:
BaseSubscriber
  • Object
show all
Defined in:
lib/anycable/rack/broadcast_subscribers/http_subscriber.rb

Overview

HTTP Pub/Sub subscriber

Constant Summary

Constants included from Logging

Logging::PREFIX

Instance Attribute Summary collapse

Attributes inherited from BaseSubscriber

#coder, #hub

Instance Method Summary collapse

Methods inherited from BaseSubscriber

#stop

Constructor Details

#initialize(**options) ⇒ HTTPSubscriber

Returns a new instance of HTTPSubscriber.



12
13
14
15
16
# File 'lib/anycable/rack/broadcast_subscribers/http_subscriber.rb', line 12

def initialize(**options)
  super
  @token = options[:token]
  @path = options[:path]
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/anycable/rack/broadcast_subscribers/http_subscriber.rb', line 10

def path
  @path
end

#tokenObject (readonly)

Returns the value of attribute token.



10
11
12
# File 'lib/anycable/rack/broadcast_subscribers/http_subscriber.rb', line 10

def token
  @token
end

Instance Method Details

#call(env) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/anycable/rack/broadcast_subscribers/http_subscriber.rb', line 22

def call(env)
  req = ::Rack::Request.new(env)

  return invalid_request unless req.post?

  if token && req.get_header("HTTP_AUTHORIZATION") != "Bearer #{token}"
    return invalid_request(401)
  end

  handle_message req.body.read

  [201, {"Content-Type" => "text/plain"}, ["OK"]]
end

#startObject



18
19
20
# File 'lib/anycable/rack/broadcast_subscribers/http_subscriber.rb', line 18

def start
  log(:info) { "Accepting pub/sub request at #{path}" }
end