Skip to content

Instantly share code, notes, and snippets.

/Gemfile Secret

Created July 31, 2013 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1914d2014c3d6a180f73 to your computer and use it in GitHub Desktop.
Save anonymous/1914d2014c3d6a180f73 to your computer and use it in GitHub Desktop.
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.
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.
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
gem 'sinatra'
gem 'coffee-script'
<!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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment