Permalink
Browse files

Add impressions & basic serving

  • Loading branch information...
1 parent fd7fd93 commit 3a2be190863e49a2211f844ac39505447672148e @kneath committed Apr 27, 2008
@@ -4,4 +4,4 @@ def index
render
end
-end
+end
@@ -0,0 +1,20 @@
+# Responsible for serving the ads (public facing)
+class Server < Merb::Controller
+
+ def spot
+ @spot = Spot.find(params[:id])
+ @campaign = @spot.campaign_for_serving
+ render_then_call(render partial("shared/ad"), :layout => false) do
+ geo_ip = GeoIP.new(Merb.root / "data" / "GeoLiteCity.dat")
+ geo_ip_data = geo_ip.city(request.remote_ip)
+ impression = @campaign.impressions.create(:ip => request.remote_ip, :country => geo_ip_data[5])
+ end
+ end
+
+ def click
+ @impression = Impression.find(params[:id])
+ @impression.click
+ return nil
+ end
+
+end
@@ -15,5 +15,10 @@ def to_percentage(fraction)
(fraction*100).to_s + "%"
end
+ def host_with_port
+ request.protocol + request.host
+ #"http://" + Merb.config[:host] + ":" + Merb.config[:port]
+ end
+
end
end
@@ -0,0 +1,5 @@
+module Merb
+ module ServerHelper
+
+ end
+end
@@ -1,6 +1,7 @@
class Campaign < ActiveRecord::Base
belongs_to :spot
+ has_many :impressions
validates_presence_of :name, :starts_on, :ends_on, :spot_id
@@ -0,0 +1,12 @@
+class Impression < ActiveRecord::Base
+
+ belongs_to :campaign, :counter_cache => true
+
+ def click
+ self.clicked = true
+ self.save
+ self.campaign.clicks_count += 1
+ self.campaign.save
+ end
+
+end
View
@@ -13,4 +13,9 @@ def campaign_for(index)
return self.campaigns[index - 1]
end
+ # finds an advertisment with weighting, etc taken into account
+ def campaign_for_serving
+ self.campaigns.first
+ end
+
end
@@ -0,0 +1 @@
+You're in index of the Server
@@ -0,0 +1,13 @@
+<div class="greed-advertisment">
+ <% unless @campaign.filename.blank? %>
+ <p><a href="<%= @campaign.link %>" title="<%= @campaign.name %>"><img src="<%= host_with_port + @campaign.filename %>" /></a></p>
+ <% unless @campaign.description.blank? %>
+ <p><%= @campaign.description %></p>
+ <% end %>
+ <% else %>
+ <h4><a href="<%= @campaign.link %>"><%=h @campaign.name %></a></h4>
+ <% unless @campaign.description.blank? %>
+ <p><%= @campaign.description %></p>
+ <% end %>
+ <% end %>
+</div>
@@ -0,0 +1,9 @@
+<script type="text/javascript">
+ (function(){
+ document.write('<div id="greed_<%= spot.id %>=document"></div>');
+ s<%= spot.id %>=document.createElement('script');
+ s<%= spot.id %>=document.type="text/javascript";
+ s<%= spot.id %>=document.src="<%= "#{host_with_port}/server/spot/#{spot.id}" %>";
+ setTimeout("document.getElementById('greed_<%= spot.id %>').appendChild(s<%= spot.id %>);",1);
+ })();
+</script>
@@ -20,11 +20,13 @@
<% if @site.spots.empty? %>
<p>No spots added for this site.</p>
<% else %>
- <ul class="spots">
+ <ul class="spots full-width-spots">
<% for spot in @site.spots %>
<li>
- <h4><%=h spot.name %></h4>
+ <p class="status"><%=h spot.name %></p>
<p class="meta"><%= spot.campaigns.count %> / <%= spot.campaign_limit %> campaigns filled</p>
+ <p class="action"><a href="#" class="codetoggler" toggle="code-<%= spot.id %>">Show Code</a></p>
+ <textarea id="code-<%= spot.id %>" class="code-insertion"><%= partial :insertion_code, :spot => spot %></textarea>
</li>
<% end %>
</ul>
View
@@ -25,7 +25,7 @@
### Add your other dependencies here
-dependencies "merb-assets", "merb_helpers"
+dependencies "merb-assets", "merb_helpers", "geoip"
Merb::BootLoader.after_app_loads do
my_formats = {
View
Binary file not shown.
@@ -37,6 +37,22 @@ var rules = {
// update hidden fields if visual fields have info in them
element.fireEvent('keyup');
+ },
+
+ '.codetoggler': function(element){
+ element = $(element);
+ toggle_element = $(element.getAttribute('toggle'));
+ toggle_element.hide();
+ element.addEvent('click', function(e){
+ if (toggle_element.visible()){
+ toggle_element.hide();
+ this.innerHTML = 'Show Code';
+ }else{
+ toggle_element.show();
+ this.innerHTML = 'Hide Code';
+ }
+ (new Event(e)).stop();
+ });
}
}
@@ -190,6 +190,13 @@ input[type=text], input[type=password], textarea{
input[type=text].datepicker{
width:100px;
}
+textarea.code-insertion{
+ width:97%;
+ height:120px;
+ font-family: "Courier New", Courier, monospace;
+ font-size:11px;
+ color:#000;
+}
input[type=submit], button[type=submit]{
padding:5px 10px;
@@ -285,6 +292,9 @@ ul.meta li a{
ul.spots{
margin:15px 0 15px -10px;
}
+ul.full-width-spots{
+ margin-left:0;
+}
ul.spots li{
list-style-type:none;
float:left;
@@ -298,7 +308,11 @@ ul.spots li{
-o-border-radius:4px;
border-radius:4px;
}
-
+ul.full-width-spots li{
+ float:none;
+ margin-left:0;
+ width:auto;
+}
.spots p.status{
margin:5px 0;
font-size:11px;
@@ -0,0 +1,17 @@
+class ImpressionMigration < ActiveRecord::Migration
+ def self.up
+ create_table :impressions do |t|
+ t.string :ip
+ t.string :country
+ t.boolean :clicked, :default => false
+ t.integer :campaign_id
+ t.datetime :created_at
+ end
+ add_column :campaigns, :impressions_count, :integer, :default => 0
+ add_column :campaigns, :clicks_count, :integer, :default => 0
+ end
+
+ def self.down
+ drop_table :impressions
+ end
+end
View
@@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 3) do
+ActiveRecord::Schema.define(:version => 4) do
create_table "campaigns", :force => true do |t|
t.string "name"
@@ -22,6 +22,17 @@
t.integer "spot_id"
t.datetime "created_at"
t.datetime "updated_at"
+ t.string "link"
+ t.integer "impressions_count", :default => 0
+ t.integer "clicks_count", :default => 0
+ end
+
+ create_table "impressions", :force => true do |t|
+ t.string "ip"
+ t.string "country"
+ t.boolean "clicked", :default => false
+ t.integer "campaign_id"
+ t.datetime "created_at"
end
create_table "sites", :force => true do |t|
@@ -0,0 +1,7 @@
+require File.join(File.dirname(__FILE__), "..", 'spec_helper.rb')
+
+describe Server do
+
+ it "should serve an ad for an active campaign"
+
+end
@@ -0,0 +1,5 @@
+require File.join(File.dirname(__FILE__), "..", 'spec_helper.rb')
+
+describe Merb::ServerHelper do
+
+end
@@ -0,0 +1,17 @@
+require File.join( File.dirname(__FILE__), "..", "spec_helper" )
+
+describe Impression do
+
+ it "should serve an image ad with description"
+
+ it "should serve an image ad without description"
+
+ it "should serve a text ad with description"
+
+ it "should serve a text ad without description"
+
+ it "should find the country based on IP"
+
+ it "should respond to a click"
+
+end

0 comments on commit 3a2be19

Please sign in to comment.