secret
anonymous / Gemfile
Created

A simple sinatra app which serves a /num_calls route returning the number of calls in queue, and some CoffeeScript which polls for this information and updates a span with the result every 5 seconds.

  • Download Gist
Gemfile
Ruby
1 2
gem 'sinatra'
gem 'coffee-script'
call_centre.coffee
CoffeeScript
1 2 3 4 5 6 7 8 9 10 11
jQuery ->
updateNumCalls() # You should call this function when the user navigates
# to the queue page.
 
updateNumCalls = ->
# Get the number of calls.
$.getJSON('/num_calls').then (result) ->
$num_calls = $('#num_calls') # Fetch the element that contains the number.
if $num_calls.length # Make sure it still exists on the page.
$num_calls.html(result.num_calls) # And if so, update the element,
setTimeout updateNumCalls, 5000 # and queue up another query in 5 secs.
call_centre.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
require 'sinatra/base'
require 'json'
 
class CallCentre < Sinatra::Base
set :views, settings.root
 
get '/' do
erb :index
end
 
get '/num_calls' do
content_type :json
{ "num_calls" => rand(1..100) }.to_json
end
 
get '/call_centre.js' do
coffee :call_centre
end
end
index.erb
HTML+ERB
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<p>
There are currently
<span id="num_calls">(Loading...)</span>
calls in the queue.
</p>
<script src="http://getbootstrap.com/assets/js/jquery.js"></script>
<script src="/call_centre.js"></script>
</body>
</html>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.