summaryrefslogtreecommitdiff
path: root/lib/shogun/github_score.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/shogun/github_score.rb')
-rw-r--r--lib/shogun/github_score.rb51
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