Skip to content

Commit

Permalink
Make upload backend work
Browse files Browse the repository at this point in the history
  • Loading branch information
ConradIrwin committed Jan 2, 2013
1 parent c12d2a9 commit 253d461
Show file tree
Hide file tree
Showing 13 changed files with 129 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ gem 'rails', '3.2.9'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'sqlite3'
gem 'pg'

group :development do
gem 'pry-rails'
Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ GEM
method_source (0.8.1)
mime-types (1.19)
multi_json (1.5.0)
pg (0.14.1)
polyglot (0.3.3)
pry (0.9.10)
coderay (~> 1.0.5)
Expand Down Expand Up @@ -99,7 +100,6 @@ GEM
multi_json (~> 1.0)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
sqlite3 (1.3.6)
thor (0.16.0)
tilt (1.3.3)
treetop (1.4.12)
Expand All @@ -117,8 +117,8 @@ DEPENDENCIES
awesome_print
coffee-rails (~> 3.2.1)
jquery-rails
pg
pry-rails
rails (= 3.2.9)
sass-rails (~> 3.2.3)
sqlite3
uglifier (>= 1.0.3)
26 changes: 11 additions & 15 deletions app/assets/javascripts/index.js.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@

# cdf(window.faithful);
(->
$ ->
cdf = (data) ->
Expand Down Expand Up @@ -72,7 +70,7 @@

viz.selectAll("text.xtitle").data([0]).enter()
.append("svg:text").attr("class", "xtitle")
.text("Email size (kb)")
.text(d3.select('#cdf').attr('data-title'))
.attr
x: 40 + width / 2
y: height + 30
Expand Down Expand Up @@ -173,18 +171,16 @@

height = 400
width = 600
window.emails = window.emails.filter((x) ->
x isnt 0
data = []
$('textarea#data').val().split("\n").forEach((x) ->
return if x.match(/^(#|\s*$)/)
n = Number(x)
# TODO warn about unparseable lines?
data.push(n) unless isNaN(n)
)
window.normal = window.normal.map(Number).filter((x) ->
x isnt 0
).sort(d3.ascending)
window.faithful = window.faithful.sort(d3.ascending)
window.foo = [1, 2]
cdf window.emails
cdf window.normal
cdf window.files
cdf window.faithful
cdf window.foo

data.sort(d3.ascending)

cdf data

).call this
8 changes: 8 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
class ApplicationController < ActionController::Base
protect_from_forgery

private
def render(args={})
if Hash === args && args[:text]
response.headers['Content-Type'] = 'text/plain; charset=utf-8'
end
super
end
end
32 changes: 32 additions & 0 deletions app/controllers/plots_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
class PlotsController < ApplicationController
def index
end

def upload
title, upload = params.detect do |k, v|
ActionDispatch::Http::UploadedFile === v
end

unless upload
return render :status => 401, :error => "Usage: cat data | curl plotocrat.com -F 'X axis title'=@-"
end

plot = Plot.new(:data => upload.read, :title => title).tap(&:save!)
render :text => url_for(:action => :view, :slug => plot.slug)
rescue ActiveRecord::RecordInvalid => e
render :status => 401, :text => url_for(:action => :error, :error => e.message)
end

def view
@plot = Plot.where(:slug => params[:slug]).first
end

def error
render :text => params[:error]
end

private

def render(args={})
if error = args.delete(:error)
super :text => url_for(:action => :error, :error => e.message)
else
super
end
end
end
11 changes: 11 additions & 0 deletions app/controllers/uploads_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class UploadsController < ActionController::Metal

def upload
plot = Plot.new(:data => request.raw_post, :title => params[:title]).tap(&:save!)
puts request.raw_post.inspect
render :text => url_for(:action => :view, :slug => plot.slug)
rescue ActiveRecord::RecordInvalid => e
render :status => 401, :text => url_for(:action => :error, :error => e.message)
end

end
14 changes: 14 additions & 0 deletions app/models/plot.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Plot < ActiveRecord::Base
attr_accessible :slug, :title, :data
validates :data, :presence => true
before_save :ensure_crlf
before_create :set_defaults

def ensure_crlf
self.data = self.data.gsub(/\r\n|\r|\n/, "\r\n")
end

def set_defaults
self.slug = Digest::SHA1.hexdigest(data)[0...20] if data
end
end
2 changes: 2 additions & 0 deletions app/views/plots/view.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<div data-title="<%= @plot.title %>" id="cdf"></div>
<textarea id="data"><%= @plot.data %></textarea>
3 changes: 2 additions & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ class Application < Rails::Application
# -- all .rb files in that directory are automatically loaded.

# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)
config.autoload_paths += %W(#{config.root}/lib)

# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named.
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
#

# Activate observers that should always be running.
# config.active_record.observers = :cacher, :garbage_collector, :forum_observer
Expand Down
12 changes: 4 additions & 8 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
adapter: postgresql
database: plotocrat

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
adapter: postgresql
database: plotocrat_test

production:
adapter: sqlite3
Expand Down
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@

# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
root :to => 'plots#upload', :via => :post
root :to => 'plots#index'
match ':slug' => 'plots#view', :slug => /[0-9a-f]{20}/
match '/error' => 'plots#error'

# See how all your routes lay out with "rake routes"

Expand Down
13 changes: 13 additions & 0 deletions db/migrate/20130101213921_create_plots.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreatePlots < ActiveRecord::Migration
def change
create_table :plots do |t|
t.text :slug, :null => false
t.text :data, :null => false
t.text :title, :null => true

t.timestamps
end

add_index :plots, :slug
end
end
26 changes: 26 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20130101213921) do

create_table "plots", :force => true do |t|
t.text "slug", :null => false
t.text "data", :null => false
t.text "title"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

add_index "plots", ["slug"], :name => "index_plots_on_slug"

end

0 comments on commit 253d461

Please sign in to comment.