Debian Codegraph @

plugin/mastodon_sse_streaming/raw_server_sent_events_stream.rb

raw symbols SCIP
1 # frozen_string_literal: true
2
3 require_relative 'error'
4
5 # SSEに接続し、受け取った文字列を列挙するEnumeratorを生成する
6 module Plugin::MastodonSseStreaming
7 class RawServerSentEventsStream
8 # @params [Enumerable[String]] connection_type
9 def initialize(connection_type)
10 # type_strict connection_type => tcor(Plugin::Mastodon::SSEPublicType, Plugin::Mastodon::SSEAuthorizedType)
11 @connection_type = connection_type
12 end
13
14 def token
15 @connection_type.token
16 end
17
18 def headers
19 if token
20 { 'Authorization' => 'Bearer %{token}' % { token: } }
21 else
22 {}
23 end
24 end
25
26 # 接続する
27 def to_enum
28 Enumerator.new do |yielder|
29 client = HTTPClient.new
30 client.ssl_config.set_default_paths
31 notice "connect #{@connection_type.perma_link} (#{@connection_type})"
32 client.request(:get, @connection_type.perma_link.to_s, {}, {}, headers) do |response, part|
33 if response.ok?
34 yielder << part
35 else
36 raise ResponseError.new(
37 response.reason,
38 code: response.status_code,
39 response:
40 )
41 end
42 end
43 rescue SocketError,
44 HTTPClient::BadResponseError,
45 HTTPClient::TimeoutError => exception
46 raise ConnectionRefusedError, exception.to_s
47 end
48 end
49 end
50 end