Skip to content

Instantly share code, notes, and snippets.

@rsbohn
Last active August 29, 2015 14:05
Show Gist options
  • Save rsbohn/bb1f4fffaccbd30125b9 to your computer and use it in GitHub Desktop.
Save rsbohn/bb1f4fffaccbd30125b9 to your computer and use it in GitHub Desktop.
xiki gist menu
# save to ~/menu
# then run using "gist"
require "rest-client"
require "json"
class Gist
Github = RestClient::Resource.new "https://api.github.com"
DefaultMenu =
" - bb1f4fffaccbd30125b9
- user/rsbohn/
- user/trogdoro/
"
# id/filename? or user/:username/id/filename?
def self.menu section=nil, username=nil, id=nil, filename=nil
return DefaultMenu if section == nil
return self.files section, username if section != "user"
return self.by_user username if id == nil
return self.files id, filename
end
def self.by_user username
gists = Github["users/#{username}/gists"].get
reply = []
JSON.parse(gists).each do |g|
reply.push "> #{g['description']}"
reply.push "#{g['id']}"
end
return "no public gists" if reply.length < 1
return reply
end
def self.files id, filename=nil
gist = Github["/gists/#{id}"].get
files = JSON.parse(gist)["files"]
reply = []
JSON.parse(gist)["files"].each do |file|
return file[1]["content"] if filename != nil && file[0] == filename
reply.push file[0]
end
return reply
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment