Class: Angelo::RSpec::WebsocketHelper

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Celluloid::Logger
Defined in:
lib/angelo/rspec/helpers.rb

Constant Summary collapse

WS_URL =
'ws://%s:%d'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(addr, port, path) ⇒ WebsocketHelper

Returns a new instance of WebsocketHelper.



90
91
92
# File 'lib/angelo/rspec/helpers.rb', line 90

def initialize addr, port, path
  @addr, @port, @path = addr, port, path
end

Instance Attribute Details

#addr=(value) ⇒ Object (writeonly)

Sets the attribute addr

Parameters:

  • value

    the value to set the attribute addr to.



88
89
90
# File 'lib/angelo/rspec/helpers.rb', line 88

def addr=(value)
  @addr = value
end

#driverObject (readonly)

Returns the value of attribute driver.



87
88
89
# File 'lib/angelo/rspec/helpers.rb', line 87

def driver
  @driver
end

#on_close=(value) ⇒ Object (writeonly)

Sets the attribute on_close

Parameters:

  • value

    the value to set the attribute on_close to.



88
89
90
# File 'lib/angelo/rspec/helpers.rb', line 88

def on_close=(value)
  @on_close = value
end

#on_message=(value) ⇒ Object (writeonly)

Sets the attribute on_message

Parameters:

  • value

    the value to set the attribute on_message to.



88
89
90
# File 'lib/angelo/rspec/helpers.rb', line 88

def on_message=(value)
  @on_message = value
end

#on_open=(value) ⇒ Object (writeonly)

Sets the attribute on_open

Parameters:

  • value

    the value to set the attribute on_open to.



88
89
90
# File 'lib/angelo/rspec/helpers.rb', line 88

def on_open=(value)
  @on_open = value
end

#path=(value) ⇒ Object (writeonly)

Sets the attribute path

Parameters:

  • value

    the value to set the attribute path to.



88
89
90
# File 'lib/angelo/rspec/helpers.rb', line 88

def path=(value)
  @path = value
end

#port=(value) ⇒ Object (writeonly)

Sets the attribute port

Parameters:

  • value

    the value to set the attribute port to.



88
89
90
# File 'lib/angelo/rspec/helpers.rb', line 88

def port=(value)
  @port = value
end

#socketObject (readonly)

Returns the value of attribute socket.



87
88
89
# File 'lib/angelo/rspec/helpers.rb', line 87

def socket
  @socket
end

Instance Method Details

#goObject



125
126
127
128
129
130
# File 'lib/angelo/rspec/helpers.rb', line 125

def go
  @driver.start
  while msg = @socket.readpartial(4096)
    @driver.parse msg
  end
end

#initObject



94
95
96
97
# File 'lib/angelo/rspec/helpers.rb', line 94

def init
  init_socket
  init_driver
end

#init_driverObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/angelo/rspec/helpers.rb', line 105

def init_driver
  @driver = WebSocket::Driver.client self

  @driver.on :open do |e|
    @on_open.call(e) if Proc === @on_open
  end

  @driver.on :message do |e|
    @on_message.call(e) if Proc === @on_message
  end

  @driver.on :close do |e|
    @on_close.call(e) if Proc === @on_close
  end
end

#init_socketObject



99
100
101
102
103
# File 'lib/angelo/rspec/helpers.rb', line 99

def init_socket
  ip = @addr
  ip = Socket.getaddrinfo(@addr, 'http')[0][3] unless @addr =~ /\d+\.\d+\.\d+\.\d+/
  @socket = Celluloid::IO::TCPSocket.new ip, @port
end

#urlObject



121
122
123
# File 'lib/angelo/rspec/helpers.rb', line 121

def url
  WS_URL % [@addr, @port] + @path
end