Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6e324ee
Showing
9 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,2 @@ | |||
*.sqlite3 | |||
config/database.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,3 @@ | |||
require 'sinatra-dj' | |||
|
|||
run Sinatra::Application |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,19 @@ | |||
<h1>Pig Latin Translator</h1> | |||
|
|||
<% @translations.each do |translation| %> | |||
<ul> | |||
<li> | |||
<span><%= translation.input %></span> | |||
<span>→</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> |