Skip to content

Instantly share code, notes, and snippets.

@igrabes
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igrabes/dd9c8e3c80b3c9ce16a0 to your computer and use it in GitHub Desktop.
Save igrabes/dd9c8e3c80b3c9ce16a0 to your computer and use it in GitHub Desktop.
call Github
require 'httparty'
require 'pry'
class User
def initialize(username, score, reposcore)
@username = username
@score = score
@reposcore = reposcore
@reponame = ""
end
def response_is_valid?(github_response)
if github_response["message"]
return false
else
return true
end
end
def get_calculation_attributes
response = HTTParty.get("https://api.github.com/users/#{@username}")
if response_is_valid?(response)
return get_score(response["id"], response["public_repos"])
else
reset_username
end
end
def get_repo_attributes
repo_response = HTTParty.get("https://api.github.com/users/#{@username}/repos")
repo = repo_response.sample
@reponame = repo["name"]
language_response = HTTParty.get("https://api.github.com/repos/#{@username}/#{@reponame}/languages")
get_repo_score(language_response.values)
end
def get_score(user_id, user_num_repos)
@score = user_id.to_i/user_num_repos.to_i
end
def score
@score
end
def reset_username
puts "#{@username} is not valid. Please input a new name"
@username = gets.chomp
get_calculation_attributes
end
def get_repo_score(language_values)
language_score = 0
language_values.each { |value| language_score += value }
language_score = language_score/language_values.length
@reposcore = language_score
end
def reposcore
@reposcore
end
def reponame
@reponame
end
end
# class Repo < User
# def initialize(username, reponame, score)
# super(username, score)
# @reponame = reponame
# end
# def get_calculation_attributes
# response = HTTParty.get("https://api.github.com/repos/#{@username}/#{@reponame}/languages")
# if response_is_valid?(response)
# return get_score(response.values)
# else
# reset_username
# end
# end
# def get_score(language_values)
# language_score = 0
# language_values.each { |value| language_score += value }
# language_score = language_score/language_values.length
# @score = language_score
# end
# end
def get_user_input
puts "Input Github User Name"
gets.chomp
end
def calculate_score_difference(user1_score, user2_score)
if user1_score > user2_score
return user1_score - user2_score
else
return user2_score - user1_score
end
end
user1 = User.new(get_user_input, 0, 0)
user2 = User.new(get_user_input, 0, 0)
user1.get_calculation_attributes
user2.get_calculation_attributes
user1.get_repo_attributes
puts "USER 1 reposcore for #{user1.reponame} = #{user1.reposcore}"
user2.get_repo_attributes
puts "USER 2 reposcore for #{user2.reponame} = #{user2.reposcore}"
final_score = calculate_score_difference(user1.score, user2.score)
puts "FINAL SCORE: #{final_score}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment