Skip to content

Instantly share code, notes, and snippets.

@splattael
Created September 7, 2009 13:58
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 splattael/182364 to your computer and use it in GitHub Desktop.
Save splattael/182364 to your computer and use it in GitHub Desktop.
require 'sinatra'
enable :sessions
helpers do
def refreshed?
# TODO better refresh logic
%w(max-age=0 no-cache).include?(env['HTTP_CACHE_CONTROL'])
end
def increment!
session[:counter] += 1
end
end
before do
session[:counter] ||= 0
end
get '/' do
increment! if refreshed?
@counter = session[:counter]
@http_headers = env.select { |(key, _)| key =~ /^HTTP/ }
erb :counter
end
get '/increment' do
increment!
redirect '/'
end
__END__
@@ layout
<html>
<%= yield %>
</html>
@@ counter
<p>counter: <%= @counter %></p>
<p>
<a href="/">reload page</a> (no increment) |
<a href="/increment">increment</a> or press F5 (FF) or CTRL-F5 (IE + FF)
</p>
<hr/>
<% @http_headers.each do |key, value| %>
<strong><%= key %>:</strong> <%= value.inspect %><br/>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment