Created
August 19, 2012 12:51
-
-
Save mugyu/3394665 to your computer and use it in GitHub Desktop.
RubyでGistの一覧を出力したり個別の内容を出力したり
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
#!/usr/bin/env ruby | |
# vim: fileencoding=utf-8 | |
require 'net/https' | |
require 'uri' | |
require 'json' | |
class GistIDNotFound < RuntimeError;end | |
def get(url) | |
uri = URI.parse(url) | |
https = Net::HTTP.new(uri.host, uri.port) | |
https.use_ssl = true | |
https.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
res = https.start{https.get(uri.path)} | |
raise GistIDNotFound unless res.code == "200" | |
JSON.parse(res.body) | |
end | |
def list(gists) | |
if gists.size.zero? | |
warn "Not found!" | |
else | |
gists.each do |gist| | |
puts "#{gist["id"]} : #{gist["description"]}" | |
end | |
end | |
end | |
def files(gist) | |
puts "user: #{gist["user"]["login"]}" | |
puts "#{gist["description"]}" | |
gist["files"].each do |key, value| | |
puts "-*- #{key} -*- vim: filetype=#{value["language"]}" | |
puts value["content"] | |
puts | |
end | |
end | |
if ARGV.size.zero? | |
arg = "mugyu" | |
else | |
arg = ARGV.shift | |
end | |
if arg.to_i.zero? | |
list(get("https://api.github.com/users/#{arg}/gists")) | |
else | |
begin | |
files(get("https://api.github.com/gists/#{arg.to_i}")) | |
rescue GistIDNotFound | |
warn "GistID is not found! [GistID: #{arg.to_i}]" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment