module SlangerHelperMethods

Public Instance Methods

em_stream(opts = {}) { |websocket, messages| ... } click to toggle source
# File spec/slanger_helper_methods.rb, line 47
def em_stream opts = {}
  messages = []

  em_thread do
    websocket = new_websocket opts

    stream(websocket, messages) do |message|
      yield websocket, messages
    end
  end

  return messages
end
em_thread() { || ... } click to toggle source
# File spec/slanger_helper_methods.rb, line 61
def em_thread
  Thread.new do
    EM.run do
      yield
    end
  end.join
end
have_attributes(attributes) click to toggle source
# File spec/have_attributes.rb, line 61
def have_attributes attributes
  HaveAttributes.new attributes
end
new_websocket(opts = {}) click to toggle source
# File spec/slanger_helper_methods.rb, line 40
def new_websocket opts = {}
  opts = { key: Pusher.key, protocol: 7 }.update opts
  uri = "ws://0.0.0.0:8080/app/#{opts[:key]}?client=js&version=1.8.5&protocol=#{opts[:protocol]}"

  EM::HttpRequest.new(uri).get.tap { |ws| ws.errback &errback }
end
private_channel(websocket, message) click to toggle source
# File spec/slanger_helper_methods.rb, line 94
def private_channel websocket, message
  socket_id = JSON.parse(message['data'])['socket_id']
  to_sign   = [socket_id, 'private-channel'].join ':'

  digest = OpenSSL::Digest::SHA256.new

  websocket.send({
    event: 'pusher:subscribe',
    data: {
      auth: [Pusher.key, OpenSSL::HMAC.hexdigest(digest, Pusher.secret, to_sign)].join(':'),
      channel: 'private-channel'
    }
  }.to_json)

end
send_subscribe(options) click to toggle source
# File spec/slanger_helper_methods.rb, line 77
def send_subscribe options
  info      = { user_id: options[:user_id], user_info: { name: options[:name] } }
  socket_id = JSON.parse(options[:message]['data'])['socket_id']
  to_sign   = [socket_id, 'presence-channel', info.to_json].join ':'

  digest = OpenSSL::Digest::SHA256.new

  options[:user].send({
    event: 'pusher:subscribe',
    data: {
      auth: [Pusher.key, OpenSSL::HMAC.hexdigest(digest, Pusher.secret, to_sign)].join(':'),
      channel_data: info.to_json,
      channel: 'presence-channel'
    }
  }.to_json)
end
start_slanger(options={})
start_slanger_with_options(options={}) click to toggle source
# File spec/slanger_helper_methods.rb, line 2
def start_slanger_with_options options={}
  # Fork service. Our integration tests MUST block the main thread because we want to wait for i/o to finish.
  @server_pid = EM.fork_reactor do
    Thin::Logging.silent = true

    opts = { host:             '0.0.0.0',
             api_port:         '4567',
             websocket_port:   '8080',
             app_key:          '765ec374ae0a69f4ce44',
             secret:           'your-pusher-secret',
             activity_timeout: 100
           }

    Slanger::Config.load opts.merge(options)

    Slanger::Service.run
  end
  wait_for_slanger
end
Also aliased as: start_slanger
stop_slanger() click to toggle source
# File spec/slanger_helper_methods.rb, line 24
def stop_slanger
  # Ensure Slanger is properly stopped. No orphaned processes allowed!
   Process.kill 'SIGKILL', @server_pid
   Process.wait @server_pid
end
stream(websocket, messages) { |message| ... } click to toggle source
# File spec/slanger_helper_methods.rb, line 69
def stream websocket, messages
  websocket.stream do |message|
    messages << JSON.parse(message)

    yield message
  end
end
wait_for_slanger(opts = {}) click to toggle source
# File spec/slanger_helper_methods.rb, line 30
def wait_for_slanger opts = {}
  opts = { port: 8080 }.update opts
  begin
    TCPSocket.new('0.0.0.0', opts[:port]).close
  rescue
    sleep 0.005
    retry
  end
end