-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem 'sinatra' | |
gem 'coffee-script' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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