blob: c7e8ff503ff5faef5e686a68b22cfb6dfa4d01c0 (
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
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
|
#!/usr/bin/env ruby
require 'net/http'
require 'uri'
require 'json'
require 'rspec/autorun'
class GithubScore
def initialize(handle)
@handle = handle
end
def score
events = get_events
get_score(events)
end
def build_url
"https://api.github.com/users/#{@handle}/events/public"
end
def get_events
begin
response = get_user_events
response_body = JSON.parse(response.body)
if response_body.is_a? Array
return response_body
elsif response_body.key?("message")
raise Exception.new response_body["message"]
else
raise Exception.new "Unknown error"
end
rescue Exception => e
puts "Request failed with #{e.message}"
rescue Timeout::Error => e
puts "Request timed out"
end
end
def get_score(events)
grouped = events.group_by{|h| h["type"]}.values
score = grouped.map { |g|
event_type = g.first["type"].strip
event_score = scoring_params[event_type] || 1
event_score * g.count
}.sum
score
end
def get_user_events
uri = URI.parse(build_url)
response = Net::HTTP.get_response(uri)
response
end
def scoring_params
scoring_params = {
"IssuesEvent" => 1,
"IssueCommentEvent" => 2,
"PushEvent" => 3,
"PullRequestReviewCommentEvent" => 4,
"WatchEvent" => 5,
"CreateEvent" => 6
}
end
end
RSpec.describe GithubScore do
describe "#score" do
context "when ?" do
subject(:github_score) { GithubScore.new("tenderlove") }
before do
response = Net::HTTPResponse.new(1, 2, 3)
response.instance_variable_set(:@read, true)
response.body = [
{type: 'PushEvent'},
{type: 'IssuesEvent'},
].to_json
end
it 'computes a score' do
allow(github_score).to receive(:get_user_events).and_return(response)
expect(github_score.score).to eq(13)
end
end
end
end
|