RubyでGistの一覧を出力したり個別の内容を出力したり
#!/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