Skip to content

Instantly share code, notes, and snippets.

@neerajsingh0101
Created May 1, 2014 12:56
Show Gist options
  • Save neerajsingh0101/b2cee86f0d7da2768fc4 to your computer and use it in GitHub Desktop.
Save neerajsingh0101/b2cee86f0d7da2768fc4 to your computer and use it in GitHub Desktop.
['json','rest-client','pry'].each {|lib| require lib}
module Helper
def self.get_data_from_api(url)
response = RestClient.get url
response.force_encoding('utf-8')
JSON.parse response
end
end
module Rule
COMMIT_TYPE_SCORE_MAP = {
:PushEvent => 5,
:PullRequestReviewCommentEvent => 4,
:WatchEvent => 3,
:CreateEvent => 2
}
def self.score(commit_type)
if COMMIT_TYPE_SCORE_MAP.has_key? commit_type.to_sym
COMMIT_TYPE_SCORE_MAP[commit_type.to_sym]
else
1
end
end
end
class Commit
attr_reader :type, :score
def initialize(type, score)
@type = type
@score = score
end
end
class User
attr_reader :commits
def initialize(data)
@commits = []
data.each do |entry|
commit_type = entry["type"]
@commits << Commit.new(commit_type,Rule.score(commit_type))
end
end
def user_score
@commits.inject(0) {|final_score, commit| final_score + commit.score }
end
end
data = Helper.get_data_from_api("https://github.com/dhh.json")
user = User.new(data)
puts user.user_score
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment