Skip to content
Browse files

Adserver Complete

  • Loading branch information...
1 parent 24a5ec9 commit eac14fac34487cc25c6cb46333c0557b4e7cf842 Melissa Lewis committed May 17, 2012
View
BIN Sinatra-1/adserver.db
Binary file not shown.
View
5 Sinatra-1/adserver/Capfile
@@ -0,0 +1,5 @@
+load 'deploy'
+# Uncomment if you are using Rails' asset pipeline
+ # load 'deploy/assets'
+Dir['vendor/gems/*/recipes/*.rb','vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }
+load 'config/deploy' # remove this line to skip loading any of the default tasks
View
14 Sinatra-1/adserver/Rakefile
@@ -0,0 +1,14 @@
+desc "Install gems that this app depends on. May need to be run with sudo."
+task :install_dependencies do
+ dependencies = {
+ "sinatra" => "0.9.4",
+ "dm-core" => "0.9.11",
+ "dm-timestamps" => "0.9.11",
+ "do_sqlite3" => "0.9.12",
+ }
+
+ dependencies.each do |gem_name, version|
+ puts "#{gem_name} #{version}"
+ system "gem install #{gem_name} --version #{version}"
+ end
+end
View
BIN Sinatra-1/adserver/adserver.db
Binary file not shown.
View
121 Sinatra-1/adserver/adserver.rb
@@ -0,0 +1,121 @@
+require 'rubygems'
+require 'sinatra'
+require 'data_mapper'
+require 'dm-core'
+require 'dm-timestamps'
+require_relative 'lib/authorization'
+
+DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/adserver.db")
+
+class Ad
+
+ include DataMapper::Resource
+
+ property :id, Serial
+ property :title, String
+ property :content, Text
+ property :width, Integer
+ property :height, Integer
+ property :filename, String
+ property :url, String
+ property :is_active, Boolean
+ property :created_at, DateTime
+ property :content_type,String
+ property :size, Integer
+
+ has n, :clicks
+end
+
+class Click
+
+ include DataMapper::Resource
+
+ property :id, Serial
+ property :ip_address, String
+ property :created_at, DateTime
+
+ belongs_to :ad
+end
+# Create or upgrade all tables at once, like magic
+configure :development do
+ # Create or upgrade all tables at once, like magic
+ DataMapper.auto_upgrade!
+end
+
+helpers do
+ include Sinatra::Authorization
+end
+# set utf-8 for outgoing
+before do
+ headers "Content-Type" => "text/html; charset=utf-8"
+end
+
+get '/' do
+ @title = "Welcome to the PeepCode Adserver"
+ erb :welcome
+end
+
+get '/ad' do
+ id = repository(:default).adapter.query(
+ 'SELECT id FROM ads ORDER BY random() LIMIT 1;'
+ )
+ @ad = Ad.get(id)
+ erb :ad, :layout => false;
+end
+
+get '/list' do
+ require_admin
+ @title = "List Ads"
+ @ads = Ad.all(:order => [:created_at.desc])
+ erb :list
+end
+
+get '/new' do
+ require_admin
+ @title = "Create A New Ad"
+ erb :new
+end
+
+post '/create' do
+ require_admin
+ @ad = Ad.new(params[:ad])
+ @ad.content_type = params[:image][:type]
+ @ad.size = File.size(params[:image][:tempfile])
+ if @ad.save
+ path = File.join(Dir.pwd, "/public/ads", @ad.filename)
+ File.open(path, "wb") do |f|
+ f.write(params[:image][:tempfile].read)
+ end
+ redirect "/show/#{@ad.id}"
+ else
+ redirect('/list')
+ end
+end
+
+get '/delete/:id' do
+ require_admin
+ ad = Ad.get(params[:id])
+ path = File.join(Dir.pwd, "/public/ads", ad.filename)
+ ad.destroy
+# File.delete(path)
+# ad.delete unless ad.nil?
+ redirect('/list')
+end
+
+get '/show/:id' do
+ require_admin
+ @ad = Ad.get(params[:id])
+ if @ad
+ erb :show
+ else
+ redirect('/list')
+ end
+end
+
+get '/click/:id' do
+ ad = Ad.get(params[:id])
+ ad.clicks.create(:ip_address => env["REMOTE_ADDR"])
+ redirect(ad.url)
+end
+
+
View
9 Sinatra-1/adserver/config.ru
@@ -0,0 +1,9 @@
+require 'rubygems'
+require 'sinatra'
+
+require 'adserver'
+
+set :environment, :production
+#Optionally set other variables here like :root or :views
+
+run Sinatra.application
View
25 Sinatra-1/adserver/config/deploy.rb
@@ -0,0 +1,25 @@
+set :application, "adserver.topfunky.com"
+set :deploy_to, "/var/www/sites/#{application}"
+set :repository, "ssh://hg@hg.topfunky.com/adserver"
+set :branch, "master"
+set :user, "deploy"
+
+set :scm, :mercurial
+set :ssh_options, { :forward_agent => true }
+set :deploy_via, :remote_cache
+set :copy_strategy, :checkout
+set :keep_releases, 3
+set :use_sudo, false
+set :copy_compression, :bz2
+
+role :app, "#{application}"
+role :web, "#{application}"
+role :db, "#{application}", :primary => true
+
+namespace :deploy do
+ task :restart, :roles => :app do
+ # with Passenger there's no need to restart anything
+ # just touch the tmp/restart.txt file
+ run "mkdir -p #{release_path}/tmp && touch #{release_path}/tmp/restart.txt"
+ end
+end
View
43 Sinatra-1/adserver/lib/authorization.rb
@@ -0,0 +1,43 @@
+module Sinatra
+ module Authorization
+
+ def auth
+ @auth ||= Rack::Auth::Basic::Request.new(request.env)
+ end
+
+ def unauthorized!(realm="Short URL Generator")
+ headers 'WWW-Authenticate' => %(Basic realm="#{realm}")
+ throw :halt, [401, 'Authorization Required']
+ end
+
+ def bad_request!
+ throw :halt, [400, 'Bad Request']
+ end
+
+ def authorized?
+ request.env['REMOTE_USER']
+ end
+
+ def authorize(username, password)
+ if (username== 'topfunky' && password== 'peepcode') then
+ true
+ else
+ false
+ end
+ end
+
+ def require_admin
+ return if authorized?
+ unauthorized! unless auth.provided?
+ bad_request! unless auth.basic?
+ unauthorized! unless authorize(*auth.credentials)
+ request.env['REMOTE_USER'] = auth.username
+ end
+
+ def admin?
+ authorized?
+ end
+
+ end
+end
+
View
BIN Sinatra-1/adserver/public/ads/120x90-advanced-command-line.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN Sinatra-1/adserver/public/ads/120x90-command-line.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
2 Sinatra-1/adserver/public/css/styles.css
@@ -0,0 +1,2 @@
+body { margin: 40px; font: 16px/18px helvetica; }
+#debug {font-size: 11px; color: #999; margin-top: 50px; }
View
16 Sinatra-1/adserver/test.html
@@ -0,0 +1,16 @@
+<!doctype html>
+
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+ <meta http-equi="Content-Type" content="text/html; charset=utf-8"/>
+
+ <title>Testing the PeepCode AdServer</title>
+
+</head>
+
+<body>
+
+<script type="text/javascript" src="http://localhost:9393/ad"></script>
+
+</body>
+</html>
View
14 Sinatra-1/adserver/views/ad.erb
@@ -0,0 +1,14 @@
+// display the advertisement
+
+var advert = ''
+
+advert+='<a href="http://<%= env["HTTP_HOST"] %>/click/<%= @ad.id %>">';
+advert+='<img class="ad" alt="<%= @ad.title %>" src="http://<%= env["HTTP_HOST"] %>/ads/<%= @ad.filename %>" border="0" height="<%= @ad.height %>" width="<%= @ad.width %>" />';
+advert+='</a>';
+advert+='<p class="ads">';
+advert+='<%= @ad.content.gsub(/"/, '\"').gsub(/\//, '\/').gsub(/\n/, '\n') %>';
+advert+='</p>';
+advert+='';
+
+document.write(advert);
+
View
19 Sinatra-1/adserver/views/layout.erb
@@ -0,0 +1,19 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns ="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+ <title><%= @title %></title>
+ <link href="/styles.css" media="screen" rel="stylesheet" type="text/css" media="screen, projection" />
+</head>
+<body>
+
+ <%= yield %>
+
+ <div id="debug">
+ <p>The time is <%=Time.now.strftime("%I:%M:%S %p") %>.</p>
+ <p>Sinatra version <%= Sinatra::VERSION %>.</p>
+ </div>
+
+
+</body>
+</html>
View
10 Sinatra-1/adserver/views/list.erb
@@ -0,0 +1,10 @@
+<h3> Ad List </h3>
+
+<table border="0" cellpadding="0" cellspacing="10" class="list">
+<% @ads.each_with_index do |ad,i| %>
+ <tr<%= ' class="alt"' if i%2==0 %>>
+ <td><a href="/show/<%= ad.id %>"><%= ad.title %></a></td>
+ <td><%= ad.clicks.size %></td>
+ <td><a href="/delete/<%= ad.id %>">delete</a></td>
+<% end %>
+</table>
View
35 Sinatra-1/adserver/views/new.erb
@@ -0,0 +1,35 @@
+<h1><%= @title %></h1>
+
+<form action="/create" method="post" id="ad" enctype="multipart/form-data">
+ <p>
+ <label>Title:</label><br />
+ <input type="text" name="ad[title]" id="title" />
+ </p>
+ <p>
+ <label>Width:</label><br />
+ <input type="text" name="ad[width]" id="width" />
+ </p>
+ <p>
+ <label>Height:</label><br />
+ <input type="text" name="ad[height]" id="height" />
+ </p>
+ <p>
+ <label>Filename:</label><br />
+ <input type="text" name="ad[filename]" id="filename" />
+ </p>
+ <p>
+ <label>URL:</label><br />
+ <input type="text" name="ad[url]" id="url" />
+ </p>
+ <p>
+ <label>Content:</label><br />
+ <input type="text" name="ad[content]" id="content" />
+ </p>
+ <p>
+ <label>Image File:</label><br />
+ <input type="file" name="image" id="image" />
+ </p>
+ <p>
+ <input type="submit">
+ </p>
+</form>
View
36 Sinatra-1/adserver/views/show.erb
@@ -0,0 +1,36 @@
+<p>
+ <img src="/ads/<%= @ad.filename %>" height="<%= @ad.height %>" width="<%=@ad.width %>" />
+</p>
+<p>
+ <b>Clicks:</b>
+ <%= @ad.clicks.size %>
+</p>
+<p>
+ <b>Title:</b>
+ <%= @ad.title %>
+</p>
+<p>
+ <b>Content:</b>
+ <%= @ad.content %>
+</p>
+<p>
+ <b>Width:</b>
+ <%= @ad.width %>
+</p>
+<p>
+ <b>Height:</b>
+ <%= @ad.height %>
+</p>
+<p>
+ <b>Filename:</b>
+ <%= @ad.filename %>
+</p>
+<p>
+ <b>Desitnation url:</b>
+ <%= @ad.url %>
+</p>
+<p>
+ <b>Is active:</b>
+ <%= @ad.is_active %>
+</p>
+
View
1 Sinatra-1/adserver/views/welcome.erb
@@ -0,0 +1 @@
+<h1>Welcome to the PeepCode AdServer</h1>
View
18 Sinatra-1/simple.rb
@@ -0,0 +1,18 @@
+require 'rubygems'
+require 'sinatra'
+
+#set utf-8 for outgoing
+before do
+ headers "Content-Type" => "text/html; charset=utf-8"
+end
+
+get '/' do
+ @title = "Hello"
+ erb :form
+end
+
+post '/' do
+ @name = "#{params[:post][:first_name]} #{params[:post][:last_name]}"
+ @title = "Hello #{@name}"
+ erb "Nice to meet you, #{@name}."
+end
View
5 Sinatra-1/views/form.erb
@@ -0,0 +1,5 @@
+<form action="/" method="post">
+ <p>First name: <input type="text" name="post[first_name]" size="20"/></p>
+ <p>Last name: <input type="text" name="post[last_name]" size="20"/></p>
+ <input type="submit" value="Say hi!">
+</form>
View
7 Sinatra-1/views/hello.erb
@@ -0,0 +1,7 @@
+<div style="text-align: center;">
+ <p>Nice to meet you, <%= @name %>.</p>
+ <p>The time is now <%=Time.now.strftime("%I:%M %p") %>.</p>
+ <p>We're using Sinatra version <%= Sinatra::VERSION %>.</p>
+</div>
+
+
View
12 Sinatra-1/views/layout.erb
@@ -0,0 +1,12 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang"en">
+<head>
+ <title><%= @title %></title>
+</head>
+<body>
+ <%= yield %>
+ <p> Copyright &#169; 2012 Topfunky </p>
+</body>
+</html>
+

0 comments on commit eac14fa

Please sign in to comment.
Something went wrong with that request. Please try again.