-
Notifications
You must be signed in to change notification settings - Fork 0
/
slap.rb
57 lines (45 loc) · 1.11 KB
/
slap.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
class Slap
attr_accessor :sender, :message
ONE_HOUR = 3600 #seconds
def self.create( message )
sender = message.sender.screen_name
message = message.to_s
slap = Slap.new(sender,message)
slap.save
return slap
end
def initialize( sender, message )
@sender = sender
@message = message
@exists = storage.has_key?(sender)
end
def valid?
!exists? && valid_message?
end
def valid_message?
message.match(/@([0-9a-z_]+)/i)
end
def exists?
@exists
end
def outgoing_message
if exists?
d sender, "Steady on there old bean - only once an hour. Try again tomorrow."
elsif not valid?
d sender, "We're all about behavior modification and noticed that you didn't target your last note. Try again."
else
message
end
end
def save
return false unless valid?
storage.store(sender, message, :expires_in => ONE_HOUR)
end
private
def d( recipient, text )
"d #{recipient} #{text}"
end
def storage
@storage ||= Moneta::DataMapper.new(:setup => "sqlite3:///#{File.join(Dir.pwd,'data','slaps.db')}")
end
end