Created
October 28, 2021 04:51
-
-
Save airbr/ba7da8ce6620a57130dd523f42974641 to your computer and use it in GitHub Desktop.
Most Starred Repo of User on Github in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# a function which finds the most starred repo of a user on GitHub | |
# Usage: | |
# most_starred_repo.rb <github_username> | |
# Example: | |
# most_starred_repo.rb airbr | |
require 'net/http' | |
require 'json' | |
def most_starred_repo(username) | |
url = "https://api.github.com/users/#{username}/repos" | |
uri = URI(url) | |
response = Net::HTTP.get(uri) | |
repos = JSON.parse(response) | |
stars = repos.map { |repo| repo["stargazers_count"] } | |
max_star_count = stars.max | |
max_star_count_index = stars.index(max_star_count) | |
most_starred_repo = repos[max_star_count_index]["url"] | |
end | |
def main | |
username = ARGV[0] | |
puts most_starred_repo(username) | |
end | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment