Skip to content

Instantly share code, notes, and snippets.

@wycats
Last active August 29, 2015 14:03
Show Gist options
  • Save wycats/49e43522c59022a3877c to your computer and use it in GitHub Desktop.
Save wycats/49e43522c59022a3877c to your computer and use it in GitHub Desktop.
require "thor"
require "json"
require "pp"
require "asciiart"
class GithubCLI < Thor
desc "user [USER]", "show information for a particular user"
option "width", type: :numeric, aliases: "-w", default: 50,
banner: "The width of the ASCII art to show"
def user(name)
say "Fetching user from Github...", :green
fetch_user(name) do |user|
puts
say AsciiArt.new(user["avatar_url"]).to_ascii_art(width: options.width)
print_table user.reject {|k,v| k =~ /_(id|url|at)$/ }.map {|k,v| [k.sub("_", " "), v] }
end
end
desc "followers [USER]", "show the followers for a particular user"
def followers(name)
fetch_user(name) do |user|
get_json(user["followers_url"]) do |followers|
say "List of followers\n\n"
with_padding do
followers.each_with_index.map { |f, i|
say "#{i}. #{f["login"]}", :green
}
end
if yes?("Do you want to open any of the followers' Github page?")
which = ask("Which one (by number)?").to_i
`open #{followers[which]["html_url"]}`
end
end
end
end
private
def fetch_user(username, &block)
get_json("/users/#{username}", &block)
end
def get_json(path)
Net::HTTP.start("api.github.com", 443, use_ssl: true) do |http|
request = Net::HTTP::Get.new(path)
request.basic_auth "<username>", "<token>"
yield JSON.parse(http.request(request).body)
end
end
end
GithubCLI.start(ARGV)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment