Skip to content

Commit

Permalink
init add
Browse files Browse the repository at this point in the history
  • Loading branch information
bmizerany committed Apr 30, 2009
0 parents commit 6e324ee
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
*.sqlite3
config/database.yml
11 changes: 11 additions & 0 deletions Rakefile
@@ -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
3 changes: 3 additions & 0 deletions config.ru
@@ -0,0 +1,3 @@
require 'sinatra-dj'

run Sinatra::Application
15 changes: 15 additions & 0 deletions db/migrate/20090411011247_create_delayed_jobs.rb
@@ -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
9 changes: 9 additions & 0 deletions db/migrate/20090411011516_create_translations.rb
@@ -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
22 changes: 22 additions & 0 deletions lib/translation.rb
@@ -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
26 changes: 26 additions & 0 deletions sinatra-dj.rb
@@ -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
1 change: 1 addition & 0 deletions vendor/delayed_job
Submodule delayed_job added at 266fc1
19 changes: 19 additions & 0 deletions views/translations.erb
@@ -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.