/
user.rb
128 lines (100 loc) · 2.57 KB
/
user.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
module Hurl
class User < Model
attr_accessor :email, :password, :crypted_password
# change this in your app
SALT = '==asdaga3hg8hwg98w4h9hg8ohsrg8hsklghsdgl=='
# find_by_email
index :email
#
# each user has an associated list
# of hurls
#
def add_hurl(hurl)
redis.sadd(key, hurl)
redis.set(key(hurl), Time.now.to_i)
end
def remove_hurl(hurl)
redis.srem(key, hurl)
redis.del(key(hurl))
end
def unsorted_hurls
redis.smembers(key)
end
def any_hurls?
redis.scard(key).to_i > 0
end
def latest_hurl
hurls(0, 1).first
end
def second_to_last_hurl_id
any_hurls? and hurls(0, 2).size == 2 and hurls(0, 2)[1]['id']
end
def latest_hurl_id
any_hurls? and latest_hurl['id']
end
def hurls(start = 0, limit = 100)
@hurls ||= hurls!(start, limit)
end
def hurls!(start = 0, limit = 100)
return [] unless any_hurls?
hurls = redis.sort key,
:by => "#{key}:*",
:order => 'DESC',
:get => "*",
:limit => [start, limit]
# convert hurls to ruby objects
hurls.map! { |hurl| Hurl.decode(hurl) }
# find and set the corresponding timestamps for
# each hurl (scoped to this user)
keys = hurls.map { |h| key(h['id']) }
redis.mget(keys).each_with_index do |date, i|
hurls[i]['date'] = Time.at(date.to_i)
end
hurls
end
#
# authentication
#
def self.authenticate(email, password)
return unless user = find_by_email(email)
if user.crypted_password == crypted_password(password)
user
end
end
def self.crypted_password(password)
Digest::SHA1.hexdigest("--#{password}-#{SALT}--")
end
def password=(password)
@password = password
@crypted_password = self.class.crypted_password(password)
end
#
# instance methods
#
def to_s
email
end
def validate
if email.to_s.strip.empty?
errors[:email] = " is empty"
elsif password.to_s.strip.empty?
errors[:password] = " is empty"
elsif self.class.find_by_email(email)
errors[:email] = " already exists"
elsif email !~ /^[^@]+@[^@]+$/
errors[:email] = " isn't an email address"
end
errors.empty?
end
def to_hash
return {
'id' => id,
'email' => email,
'crypted_password' => crypted_password
}
end
def key(*parts)
super(*[id, :hurls, parts].flatten)
end
end
end