forked from hotsh/rstat.us
-
Notifications
You must be signed in to change notification settings - Fork 0
/
author.rb
61 lines (49 loc) · 1.48 KB
/
author.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
class Author
DEFAULT_AVATAR = "http://rstat.us/images/avatar.png"
ENCODED_DEFAULT_AVATAR = URI.encode_www_form_component(DEFAULT_AVATAR)
GRAVATAR_HOST = "gravatar.com"
include MongoMapper::Document
key :username, String
key :name, String
key :email, String
key :website, String
key :bio, String
key :image_url, String
one :feed
one :user
# The url of their profile page
key :remote_url, String
# This takes results from an omniauth reponse and generates an author
def self.create_from_hash!(hsh)
create!(
:name => hsh['user_info']['name'],
:username => hsh['user_info']['nickname'],
:website => hsh['user_info']['urls']['Website'],
:bio => hsh['user_info']['description'],
:image_url => hsh['user_info']['image'],
:remote_url => hsh['user_info']['url']
)
end
def url
return remote_url if remote_url
"/users/#{username}"
end
def avatar_url
return image_url if image_url
return DEFAULT_AVATAR if email.nil?
# if the gravatar doesn't exist, gravatar will use a default that we provide
gravatar_url
end
def display_name
return username if name.nil? || name.empty?
name
end
def gravatar_url
"http://#{GRAVATAR_HOST}#{gravatar_path}"
end
# these query parameters are described at:
# <http://en.gravatar.com/site/implement/images/#default-image>
def gravatar_path
"/avatar/#{Digest::MD5.hexdigest(email)}?s=48&r=r&d=#{ENCODED_DEFAULT_AVATAR}"
end
end