summaryrefslogtreecommitdiff
path: root/lib/shogun/github_score.rb
blob: 22019ec75ff1b1405893f9d46069880ba8decf90 (plain)
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
module Shogun
  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
      events.sum do |event|
        points.fetch(event["type"], 1)
      end
    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
  end
end