Created
October 29, 2009 13:31
-
-
Save ku1ik/221445 to your computer and use it in GitHub Desktop.
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' | |
require 'dm-core' | |
require 'dm-types' | |
APP_ROOT = File.dirname(__FILE__) | |
DB_PATH = File.expand_path(File.join(APP_ROOT, 'db.sqlite3')) | |
DataMapper.setup(:default, "sqlite3://#{DB_PATH}") | |
class Media | |
include DataMapper::Resource | |
property :id, Serial | |
property :path, String, :nullable => false | |
property :clicks, Integer, :nullable => false, :default => 0 | |
end | |
DataMapper.auto_migrate! unless ::File.exist?(DB_PATH) | |
get '/banners/:banner' do | |
banner = params[:banner] | |
media = Media.first(:path => banner) || Media.create(:path => banner) | |
media.clicks += 1 | |
media.save | |
response["X-Accel-Redirect"] = "...../files/images/#{banner}" | |
end | |
get '/stats' do | |
stats = Media.all.inject({}) { |h,m| h[m.path] = m.clicks; h } | |
"<pre>#{stats.inspect}</pre>" | |
end | |
run Sinatra::Application |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment