-
Notifications
You must be signed in to change notification settings - Fork 3
/
chat_demo.rb
44 lines (35 loc) · 1000 Bytes
/
chat_demo.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
require 'sinatra/base'
class ChatDemo < Sinatra::Base
helpers TorqueBox::Injectors
helpers do
def content_for(key, &block)
@content ||= {}
@content[key] = capture_haml(&block)
end
def content(key)
@content && @content[key]
end
def stomp_url
inject( 'stomp-endpoint' )
end
end
get '/' do
session[:start] = 'hi'
haml :login
end
post '/' do
username = params[:username]
redirect to( '/' ) and return if username.nil?
username.strip!
redirect to( '/' ) and return if ( username == '' )
redirect to( '/' ) and return if ( ! ( username =~ /^[a-zA-Z0-9_]+$/ ) )
session[:username] = username
haml :chat
end
get '/profile/:username' do
username = params[:username]
message = "#{username}, someone from #{env['REMOTE_ADDR']} checked out your profile"
inject( '/topics/chat' ).publish( message, :properties=>{ :recipient=>username, :sender=>'system' } )
haml :profile
end
end