Skip to content

Instantly share code, notes, and snippets.

@sharmaabhinav
Last active August 29, 2015 14:05
Show Gist options
  • Save sharmaabhinav/3f5556c130713697ba00 to your computer and use it in GitHub Desktop.
Save sharmaabhinav/3f5556c130713697ba00 to your computer and use it in GitHub Desktop.
require 'open-uri'
require 'json'
module Github
COMMIT_TYPE_SCORE_MAPPING = {
PushEvent: 5,
PullRequestReviewCommentEvent: 4,
WatchEvent: 3,
CreateEvent: 2
}
def self.uri(username)
if username.nil?
nil
else
"https://github.com/" + username.to_s + ".json"
end
end
def self.get_commits(username)
begin
commits_obj = Hash.new
commits_obj[:errors] = []
commits_obj[:commits] = []
if uri(username).nil?
commits_obj[:errors] << "username not provided"
else
commits = to_json(URI.parse(uri(username)).read)
commits_obj[:commits] = commits
commits_obj[:errors] = []
end
rescue OpenURI::HTTPError => e
commits_obj[:errors] << e.io.status[1]
puts commits_obj[:errors]
end
return commits_obj
end
def self.to_json(obj_to_transform)
json = JSON.parse(obj_to_transform.to_s)
return json
end
end
class User
def initialize(username)
@username = username
end
def get_commits
response = Github.get_commits(@username)
@commits = []
response[:commits].each do |commit|
score = nil
if Github::COMMIT_TYPE_SCORE_MAPPING.has_key? commit["type"].to_sym
score = Github::COMMIT_TYPE_SCORE_MAPPING[commit["type"].to_sym]
else
score = 1
end
@commits << Commit.new(commit["type"], score)
end
@commits
end
def user_score
get_commits
score = 0
@commits.each do |commit|
score += commit.score
end
score
end
end
class Commit
attr_reader :type, :score
def initialize(type, score)
@type = type
@score = score
end
end
user = User.new('dhh')
puts user.user_score
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment