This repository has been archived by the owner on Jan 23, 2019. It is now read-only.
/
user.rb
97 lines (78 loc) · 2.46 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
class User
def initialize(db)
@db = db
@coll = db.collection('users')
# Github specific fields are prefixed with gh_ and automatically updated
@user = {
'login' => nil, # can be set after user logged in
'created_at' => nil,
'updated_at' => nil,
'name' => nil,
'email' => nil, # not displayed publicly
'avatar_url' => nil,
'show_avatar' => true,
'opted_out' => false,
'homepage' => nil,
'location' => nil,
'hireable' => nil,
'company' => nil,
'gh_login' => nil, # only required field at the moment
'gh_followers' => nil,
'gh_type' => nil,
'gh_public_gists' => nil,
'gh_following' => nil,
'gh_public_repos' => nil,
'gh_html_url' => nil,
'gh_created_at' => nil
}
end
def get(gh_login)
return @coll.find_one({ 'gh_login' => gh_login })
end
def get_by_id(id)
return @coll.find_one({ '_id' => id })
end
def get_coll()
return @coll
end
# currently only works with github data
def create(data)
now = Time.now.utc
@user['created_at'] = now
@user['updated_at'] = now
# map data to fields
@user['gh_login'] = data['login']
@user['name'] = data['name']
@user['email'] = data['email']
@user['avatar_url'] = data['avatar_url']
@user['homepage'] = data['blog']
@user['location'] = data['location']
@user['hireable'] = data['hireable']
@user['company'] = data['company']
@user['gh_followers'] = data['followers']
@user['gh_type'] = data['type']
@user['gh_public_gists'] = data['public_gists']
@user['gh_following'] = data['following']
@user['gh_public_repos'] = data['public_repos']
@user['gh_html_url'] = data['html_url']
@user['gh_created_at'] = data['created_at']
return @coll.find_one({ '_id' => @coll.insert(@user) })
end
def update(user, data)
user['updated_at'] = Time.now.utc
# update github specific fields except created_at
user['gh_followers'] = data['followers']
user['gh_type'] = data['type']
user['gh_public_gists'] = data['public_gists']
user['gh_following'] = data['following']
user['gh_public_repos'] = data['public_repos']
user['gh_html_url'] = data['html_url']
@coll.update({ '_id' => user['_id'] }, user)
return user
end
def notfound(user)
user['updated_at'] = Time.now.utc
user['notfound'] = true
@coll.update({ '_id' => user['_id'] }, user)
end
end