Class: Mastodon::Streaming::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/mastodon/streaming/connection.rb

Overview

Socket/Connection handler.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Connection

Returns a new instance of Connection.



13
14
15
16
17
18
19
# File 'lib/mastodon/streaming/connection.rb', line 13

def initialize(options = {})
  @tcp_socket_class = options.fetch(:tcp_socket_class) { TCPSocket }
  @ssl_socket_class = options.fetch(:ssl_socket_class) do
    OpenSSL::SSL::SSLSocket
  end
  @using_ssl = options.fetch(:using_ssl) { false }
end

Instance Attribute Details

#ssl_socket_classObject (readonly)

Returns the value of attribute ssl_socket_class.



11
12
13
# File 'lib/mastodon/streaming/connection.rb', line 11

def ssl_socket_class
  @ssl_socket_class
end

#tcp_socket_classObject (readonly)

Returns the value of attribute tcp_socket_class.



11
12
13
# File 'lib/mastodon/streaming/connection.rb', line 11

def tcp_socket_class
  @tcp_socket_class
end

Instance Method Details

#connect(request) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/mastodon/streaming/connection.rb', line 31

def connect(request)
  client = new_tcp_socket(request.socket_host, request.socket_port)
  return client if !@using_ssl || (!@using_ssl && request.using_proxy?)

  client_context = OpenSSL::SSL::SSLContext.new
  ssl_client     = @ssl_socket_class.new(client, client_context)
  ssl_client.connect
end

#stream(request, response) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/mastodon/streaming/connection.rb', line 21

def stream(request, response)
  client = connect(request)
  request.stream(client)
  # rubocop:disable AssignmentInCondition
  while body = client.readpartial(1024)
    response << body
  end
  # rubocop:enable AssignmentInCondition
end