Skip to content

Commit

Permalink
created Sinatra version of Rails examples_controller and tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Tryus authored and Igor Tryus committed Oct 29, 2012
1 parent 985a6b4 commit e12ad8a
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 68 deletions.
85 changes: 17 additions & 68 deletions sinatra/3.3/hello.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,107 +2,56 @@
require 'sinatra/hashfix'
require 'pubnub'

# presence callback
def presence_out(message)
body_out = message[0].to_s
puts(message)
halt body_out if message[0] != []
end

# subscribe callback
def sub_out(message)
body_out = message[0].to_s
puts(body_out)
halt body_out if message[0] != []
end

# publish callback
def pub_out(message)
body_out = message[0].to_s
puts(body_out)
halt body_out if message[0] != []
end

# uuid callback
def uuid_out(message)
body_out = "#{message}"
puts(body_out)
halt body_out
end

# time callback
def time_out(message)
body_out = "#{message[0].to_s}"
puts(body_out)
halt body_out
end

# message history callback
def history_out(message)
body_out = "#{message[0].to_s}, starting at: #{message[1]}, ending at: #{message[2]}"
puts(body_out)
halt body_out
end
set :server, :webrick

# here_now callback
def here_now_out(message)
body_out = "UUIDS: #{message["uuids"].to_s}<br/>Occupancy: #{message["occupancy"]}"
puts(message)
halt body_out
end
# callback
# if output returns false, return immediately, otherwise, keep going...
def output(out, cycle = false); p out; cycle; end

# presence route
get '/presence' do
my_presence_callback = method(:presence_out)

pubnub = Pubnub.new(:subscribe_key => :demo)
pubnub.presence(:channel => :hello_world, :callback => my_presence_callback)
pubnub.presence(:channel => :hello_world, :callback => method(:output))
end

# subscribe route
get '/sub' do
my_sub_callback = method(:sub_out)

pubnub = Pubnub.new(:subscribe_key => :demo)
pubnub.subscribe(:channel => :hello_world, :callback => my_sub_callback)
pubnub.subscribe(:channel => :hello_world, :callback => method(:output))
end

# publish route
get '/pub/:message' do
my_pub_callback = method(:pub_out)

pubnub = Pubnub.new(:subscribe_key => :demo, :publish_key => :demo)
pubnub.publish(:channel => :hello_world, :callback => my_pub_callback, :message => "#{Time.now} - Sinatra says #{params[:message]}!")
pubnub.publish(:channel => :hello_world, :callback => method(:output), :message => "#{Time.now} - Sinatra says #{params[:message]}!")
end

# message detailed_history route
get '/detailed_history' do
pubnub = Pubnub.new(:subscribe_key => :demo)
pubnub.detailed_history(:channel => :hello_world, :count => 5, :callback => method(:output))
end

# message history route
get '/history' do
my_history_callback = method(:history_out)

pubnub = Pubnub.new(:subscribe_key => :demo)
pubnub.detailed_history(:channel => :hello_world, :count => 5, :callback => my_history_callback)
pubnub.history(:channel => :hello_world, :limit => 5, :callback => method(:output))
end

# here_now route
get '/here_now' do
my_here_now_callback = method(:here_now_out)

pubnub = Pubnub.new(:subscribe_key => :demo)
pubnub.here_now(:channel => :hello_world, :count => 5, :callback => my_here_now_callback)
pubnub.here_now(:channel => :hello_world, :count => 5, :callback => method(:output))
end

# time route
get '/time' do
my_time_callback = method(:time_out)

pubnub = Pubnub.new(:subscribe_key => :demo)
pubnub.time(:callback => my_time_callback)
pubnub.time(:callback => method(:output))
end

# uuid route
get '/uuid' do
my_uuid_callback = method(:uuid_out)

pubnub = Pubnub.new(:subscribe_key => :demo)
uuid_out(pubnub.uuid)
output(pubnub.uuid)
end
84 changes: 84 additions & 0 deletions sinatra/3.3/hello_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
require File.dirname(__FILE__) + '/hello.rb'
require 'rack/test'

set :enviroment, :test

def app
Sinatra::Application
end

# GET /pub/:message
describe "responding to GET pub" do
include Rack::Test::Methods

it "should send message" do
get '/pub/hi'

last_response.should be_ok
end

end

# GET /time
describe "responding to GET time" do
include Rack::Test::Methods

it "should return server time" do
get '/time'

last_response.should be_ok
end

end

# GET /uuid
describe "responding to GET uuid" do
include Rack::Test::Methods

it "should return session UUID" do

get '/uuid'

last_response.should be_ok
end

end

# GET /here_now
describe "responding to GET here_now" do
include Rack::Test::Methods

it "should return current occupancy status of the channel" do

get '/here_now'

last_response.should be_ok
end

end

# GET /history
describe "responding to GET history" do
include Rack::Test::Methods

it "should return the last 5 messages published to the channel" do
get '/history'

last_response.should be_ok
end

end

# GET /detailed_history
describe "responding to GET detailed_history" do
include Rack::Test::Methods

it "should return archive messages of on a given channel" do
get '/detailed_history'

last_response.should be_ok
end

end


0 comments on commit e12ad8a

Please sign in to comment.