-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels.rb
38 lines (32 loc) · 866 Bytes
/
models.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
# SPDX-License-Identifier: AGPL-3.0-only
# SPDX-FileCopyrightText: 2022 Hugo Peixoto <[email protected]>
require 'active_record'
require 'date'
require './codewars'
class User < ActiveRecord::Base
def self.register(username)
self.create(
username: username,
profile: Codewars.profile(username),
)
end
def refresh!
update!(
completed_challenges: Codewars.completed_challenges(username),
)
end
def points(challenges)
challenges.map do |challenge|
self
.completed_challenges
.find { |c| c["id"] == challenge.kata }
&.then do |c|
Date.parse(c["completedAt"]).strftime("%Yw%-V") == "#{challenge.year}w#{challenge.week}" ? 3 : 1
end
end
end
def score(challenges)
points(challenges).reject(&:nil?).sum
end
end
class Challenge < ActiveRecord::Base; end