Class: Twitter::Streaming::Connection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Connection

Returns a new instance of Connection



10
11
12
13
14
# File 'lib/twitter/streaming/connection.rb', line 10

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

Instance Attribute Details

#ssl_socket_classObject (readonly)

Returns the value of attribute ssl_socket_class



8
9
10
# File 'lib/twitter/streaming/connection.rb', line 8

def ssl_socket_class
  @ssl_socket_class
end

#tcp_socket_classObject (readonly)

Returns the value of attribute tcp_socket_class



8
9
10
# File 'lib/twitter/streaming/connection.rb', line 8

def tcp_socket_class
  @tcp_socket_class
end

Instance Method Details

#connect(request) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/twitter/streaming/connection.rb', line 24

def connect(request)
  client = new_tcp_socket(request.socket_host, request.socket_port)
  return client if !@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



16
17
18
19
20
21
22
# File 'lib/twitter/streaming/connection.rb', line 16

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