-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm.rb
215 lines (186 loc) · 5.26 KB
/
m.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# encoding: utf-8
Camping.goes :M
module M::Models
class Message < Base
CONV = %w(0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)
def mhash
out = ''
num = self.id
mult = 7 #precalced, allows for num > 99999999999999
mult -= 1 while CONV.length ** mult > num && mult > 0
while num >= 0 && mult >= 0
quotient, modulus = num.divmod(CONV.length ** mult)
if modulus > 0 or mult == 0
out << CONV[quotient]
num = modulus
end
mult -= 1
end
out
end
def self.mdehash(str)
sum = 0
str.split(//).each_with_index do |digit, index|
sum += CONV.index(digit) * (CONV.length ** (str.length - 1 - index))
end
sum
end
end
class CreateMessages < V 1.0
def self.up
create_table :m_messages do |t|
t.column :message, :string, :limit => 255
end
end
def self.down
drop_table :m_messages
end
end
end
module M::Controllers
class Index < R '/'
def get
render :index
end
end
class List
def get
@messages = Message.find(:all, :limit => 100, :order => 'id DESC')
render :list
end
end
class Create
def get
render :create
end
def post
@message = Message.create(:message => input.message[0..255])
render :created
end
end
class Show < R '/([A-z0-9]+)'
def get(id)
begin
@message = Message.find(Message.mdehash(id)).message
rescue ActiveRecord::RecordNotFound
@message = "404, message not found"
end
render :show
end
end
class Style < R '/style.css'
def get
@headers["Content-Type"] = "text/css; charset=utf-8"
@body = %{
* { margin: 0; padding: 0; }
html, body { height: 100%; }
body { font-family: "Trebuchet MS", Helvetica, Arial, sans-serif; background-color: #ffffff; color: #000000; }
table { width: 100%; height: 100%; }
td { vertical-align: middle; text-align: center; padding: 1em; font-size: 3.4em; line-height: 1.2em; position: relative; }
a { color: #0000ff; text-decoration: none; }
a:hover { text-decoration: underline; }
#desc { font-size: 0.7em; }
form { position: absolute; bottom: 5px; left: 5px; right: 5px; padding: 0 5px 0 5px; }
#create_message { font-size: 100%; text-align: center; width: 100%; }
#create_message.viewing { color: #808080; }
#create_message.editing { color: #000000; }
.list_message { font-size: 0.35em; padding: 0.3em 0; border-bottom: 1px solid #a0a0a0; text-align: left; }
#list_last { border-bottom: none; }
}
end
end
class Script < R '/js.js'
def get
@headers["Content-Type"] = "text/javascript; charset=utf-8"
@body = %{
var isIE = (window.all && !window.opera);
var instructions = "Create a message? Type here, hit <enter>"
function niceFocus(event)
{
var ele = (isIE ? event.srcElement : event.target);
if(ele.value == instructions)
{
ele.value = '';
ele.className = 'editing';
}
}
function niceBlur(event)
{
var ele = (isIE ? event.srcElement : event.target);
if(ele.value == '')
{
ele.className = 'viewing';
ele.value = instructions;
}
}
function load()
{
var createMessage = document.getElementById('create_message');
if(createMessage)
{
createMessage.className = "viewing";
createMessage.value = instructions;
createMessage.onfocus = niceFocus;
createMessage.onblur = niceBlur;
}
}
window.onload = load;
}
end
end
end
module M::Views
def layout
xhtml_transitional do
head do
title "Messages"
link :rel => 'stylesheet', :type => 'text/css', :href => '/style.css', :media => 'screen'
script :src => "js.js", :type => "text/javascript"
end
body do
table do
tr do
td do
self << yield
end
end
end
end
end
end
def index
div.desc! do
text "Ever wanted to <strong>really</strong> give someone the message? Now you can. Enter a message (be terse), give the recipient the generated link, and they'll get the message in stark black-on-white. "
a :href => "/list" do
text "See everybody's messages"
end
end
_create
end
def show
text @message
end
def list
@messages.each do |message|
id = message === @messages.last ? "list_last" : nil
div :id => id, :class => "list_message" do
text message.message
end
end
end
def created
text "link to message: "
a :href => "http://m.bloople.net/#{@message.mhash}" do
text "http://m.bloople.net/#{@message.mhash}"
end
end
def _create
form :action => "/create", :method => "post" do
input.create_message! :name => "message"
end
end
end
def M.create
M::Models.create_schema
ActiveRecord::Base.default_timezone = :utc
end