diff options
| author | mo khan <mo@mokhan.ca> | 2021-12-21 10:04:34 -0700 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2021-12-21 10:04:34 -0700 |
| commit | 1da7737f8970000fc7bd592c19aa6c647d49c347 (patch) | |
| tree | 8374631214504ecdae929013638d5d73dc80e889 /lib/shogun | |
| parent | 752ec386ab387a76f60b2571d43c42faf71e2e29 (diff) | |
refactor: move files to lib/spec/bin
Diffstat (limited to 'lib/shogun')
| -rw-r--r-- | lib/shogun/github_score.rb | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/lib/shogun/github_score.rb b/lib/shogun/github_score.rb new file mode 100644 index 0000000..48947a0 --- /dev/null +++ b/lib/shogun/github_score.rb @@ -0,0 +1,51 @@ +module Shogun + class UserNotFound < ArgumentError + end + + class GithubScore + DEFAULT_POINTS = { + "IssuesEvent" => 1, + "IssueCommentEvent" => 2, + "PushEvent" => 3, + "PullRequestReviewCommentEvent" => 4, + "WatchEvent" => 5, + "CreateEvent" => 6 + }.freeze + + def initialize(handle, points: DEFAULT_POINTS) + @handle = handle + @points = points + end + + def score + calculate_score_for(events) + end + + private + + attr_reader :points, :handle + + def build_url + "https://api.github.com/users/#{handle}/events/public" + end + + def events + response = Net::HTTP.get_response(URI.parse(build_url)) + raise UserNotFound.new(handle) if response.code == "404" + + JSON.parse(response.body) + end + + def calculate_score_for(events) + grouped = events.group_by { |h| h["type"] }.values + score = grouped.map { |g| + event_type = g.first["type"].strip + event_score = points[event_type] || 1 + event_score * g.count + }.sum + score + end + end + + +end |
