Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 82 lines (66 sloc) 1.932 kb
71f1b9a Blake Mizerany sinatra-captcha
authored
1 $:.unshift *Dir[File.dirname(__FILE__) + "/vendor/*/lib"]
6e324ee Blake Mizerany init add
authored
2
3 require 'sinatra'
71f1b9a Blake Mizerany sinatra-captcha
authored
4 require 'sinatra/captcha'
6e324ee Blake Mizerany init add
authored
5 require 'activerecord'
6 require 'delayed_job'
8e4edb8 Blake Mizerany captcha
authored
7 require 'open-uri'
6e324ee Blake Mizerany init add
authored
8
9 configure do
10 config = YAML::load(File.open('config/database.yml'))
11 environment = Sinatra::Application.environment.to_s
12 ActiveRecord::Base.logger = Logger.new($stdout)
13 ActiveRecord::Base.establish_connection(
14 config[environment]
15 )
16 end
17
84f9b81 Blake Mizerany seperation unneeded
authored
18 class Translation < ActiveRecord::Base
19 after_create :queue
20
21 def queue
22 Delayed::Job.enqueue self
23 end
24
25 def perform
26 self.output = self.class.english_to_pig_latin(input)
27 save!
28 end
29
30 def self.english_to_pig_latin(text)
31 text.split.map do |word|
32 if word.length <= 2
33 word
34 else
35 (word[1,9999] + word[0,1] + "ay").downcase
36 end
37 end.join(" ")
38 end
39 end
40
6e324ee Blake Mizerany init add
authored
41 get '/' do
8e4edb8 Blake Mizerany captcha
authored
42 @session = rand(1000) + 1000
6e324ee Blake Mizerany init add
authored
43 @translations = Translation.all(:order => 'created_at desc')
44 erb :translations
45 end
46
47 post '/translations' do
bb70d87 Blake Mizerany refactor out captcha, get ready for gem
authored
48 halt 401, "Invalid Captcha Answer" unless captcha_pass?
6e324ee Blake Mizerany init add
authored
49 Translation.create! :input => params[:input]
50 redirect '/'
51 end
74c02e1 Blake Mizerany the sinatra way
authored
52
53 use_in_file_templates!
54
55 __END__
56
57 @@ translations
58
59 <h1>Pig Latin Translator</h1>
9d1b115 Blake Mizerany docn' the site
authored
60 <h2>An example of <a href="http://www.sinatrarb.com">Sinatra</a> + <a href="http://github.com/tobi/delayed_job/">DJ (Delayed Job)</a> on Heroku</h2>
61 <h3>See the <a href="http://github.com/bmizerany/sinatra-dj">code</a></h3>
74c02e1 Blake Mizerany the sinatra way
authored
62
63 <% @translations.each do |translation| %>
64 <ul>
65 <li>
66 <span><%= translation.input %></span>
67 <span>&rarr;</span>
68 <span><%= translation.output || '<i>...pending...</i>' %></span>
69 </li>
70 </ul>
71 <% end %>
72
73 <hr/>
74
75 <h2>New Translation</h2>
76 <form method="post" action="/translations">
77 <div><textarea rows="3" cols="80" name="input">Enter text to translate</textarea></div>
bb70d87 Blake Mizerany refactor out captcha, get ready for gem
authored
78 <p><%= captcha_image_tag %></p>
79 <p><%= captcha_answer_tag %></div>
74c02e1 Blake Mizerany the sinatra way
authored
80 <div><input type="submit" value="Submit" /></div>
81 </form>
Something went wrong with that request. Please try again.