forked from johana-star/password-dynamo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamo.rb
60 lines (47 loc) · 1.07 KB
/
dynamo.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Run on Heroku using Ruby 1.9.1
require 'sinatra'
def load_dictionary(dictionary = "words.txt", max_word_length = 10)
words = []
File.open(dictionary) do |file|
file.each do |line|
words << line.strip.downcase unless line.length > max_word_length
end
end
words
end
SPACERS = Array(0..9).collect {|i| i.to_s} + ['!', '~', '-', '_', ' ', '+', '=', '%', '$', '#', '@']
WORDS = load_dictionary
def generate_password
password = ''
words_in_password = 3 + rand(2)
(words_in_password - 1).times do
password += WORDS.sample + SPACERS.sample
end
password + WORDS.sample
end
# FIXME Everything above this line can probably be stored in a class.
before do
headers "Content-Type" => "text/html; charset=utf-8"
end
get '/' do
@password = generate_password
erb :index
end
get '/*' do
redirect to('/')
end
__END__
@@ layout
<html>
<head>
<title><%= @title %></title>
<style>
body { margin: 40px; font: 40px/48px helvetica;}
</style>
</head>
<body>
<%= yield %>
</body>
</html>
@@ index
<p><%= @password %></p>