Skip to content

Instantly share code, notes, and snippets.

@ku1ik
Created October 29, 2009 13:31
Show Gist options
  • Save ku1ik/221445 to your computer and use it in GitHub Desktop.
Save ku1ik/221445 to your computer and use it in GitHub Desktop.
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