Permalink
Browse files

init add

  • Loading branch information...
0 parents commit 6e324ee76aaebba0eca69758c61586c89f6ec05a @bmizerany committed Apr 30, 2009
@@ -0,0 +1,2 @@
+*.sqlite3
+config/database.yml
@@ -0,0 +1,11 @@
+require 'sinatra-dj'
+require File.dirname(__FILE__) + "/vendor/delayed_job/tasks/tasks"
+
+namespace :db do
+ task :migrate do
+ ActiveRecord::Migrator.migrate(
+ 'db/migrate',
+ ENV["VERSION"] ? ENV["VERSION"].to_i : nil
+ )
+ end
+end
@@ -0,0 +1,3 @@
+require 'sinatra-dj'
+
+run Sinatra::Application
@@ -0,0 +1,15 @@
+class CreateDelayedJobs < ActiveRecord::Migration
+ def self.up
+ create_table :delayed_jobs do |t|
+ t.integer :priority, :default => 0
+ t.integer :attempts, :default => 0
+ t.text :handler
+ t.string :last_error
+ t.datetime :run_at
+ t.datetime :locked_at
+ t.datetime :failed_at
+ t.string :locked_by
+ t.timestamps
+ end
+ end
+end
@@ -0,0 +1,9 @@
+class CreateTranslations < ActiveRecord::Migration
+ def self.up
+ create_table :translations do |t|
+ t.text :input
+ t.text :output
+ t.timestamps
+ end
+ end
+end
@@ -0,0 +1,22 @@
+class Translation < ActiveRecord::Base
+ after_create :queue
+
+ def queue
+ Delayed::Job.enqueue self
+ end
+
+ def perform
+ self.output = self.class.english_to_pig_latin(input)
+ save!
+ end
+
+ def self.english_to_pig_latin(text)
+ text.split.map do |word|
+ if word.length <= 2
+ word
+ else
+ (word[1,9999] + word[0,1] + "ay").downcase
+ end
+ end.join(" ")
+ end
+end
@@ -0,0 +1,26 @@
+$:.unshift *Dir[File.dirname(__FILE__) + "/vendor/*/lib"]
+
+require 'sinatra'
+require 'activerecord'
+require 'delayed_job'
+
+require File.dirname(__FILE__) + '/lib/translation'
+
+configure do
+ config = YAML::load(File.open('config/database.yml'))
+ environment = Sinatra::Application.environment.to_s
+ ActiveRecord::Base.logger = Logger.new($stdout)
+ ActiveRecord::Base.establish_connection(
+ config[environment]
+ )
+end
+
+get '/' do
+ @translations = Translation.all(:order => 'created_at desc')
+ erb :translations
+end
+
+post '/translations' do
+ Translation.create! :input => params[:input]
+ redirect '/'
+end
Submodule delayed_job added at 266fc1
@@ -0,0 +1,19 @@
+<h1>Pig Latin Translator</h1>
+
+<% @translations.each do |translation| %>
+ <ul>
+ <li>
+ <span><%= translation.input %></span>
+ <span>&rarr;</span>
+ <span><%= translation.output || '<i>...pending...</i>' %></span>
+ </li>
+ </ul>
+<% end %>
+
+<hr/>
+
+<h2>New Translation</h2>
+<form method="post" action="/translations">
+ <div><textarea rows="3" cols="80" name="input">Enter text to translate</textarea></div>
+ <div><input type="submit" value="Submit" /></div>
+</form>

0 comments on commit 6e324ee

Please sign in to comment.