Skip to content

Commit

Permalink
Merge commit 'refs/pull/176/head' of github:defunkt/gist
Browse files Browse the repository at this point in the history
  • Loading branch information
ConradIrwin committed Aug 6, 2014
2 parents 82fe83c + f4c3148 commit 974e0d1
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -64,6 +64,11 @@ upload content to https://gist.github.com/.

gist -o <a.rb

‌To list (public gists or all gists for authed user) gists for user

gist -l : all gists for authed user
gist -l USER : public gists for username USER

‌See `gist --help` for more detail.

## Login
Expand Down
14 changes: 14 additions & 0 deletions bin/gist
Expand Up @@ -110,6 +110,10 @@ Usage: #{executable_name} [-o|-c|-e] [-p] [-s] [-R] [-d DESC] [-a] [-u URL] [-P]
options[:raw] = true
end

opts.on("-l", "--list [USER]", "List all gists for user") do |user|
options[:list] = user
end

opts.on_tail("-h","--help", "Show this message.") do
puts opts
exit
Expand Down Expand Up @@ -139,6 +143,15 @@ begin

options[:public] = Gist.should_be_public?(options)

if options.key? :list
if options[:list]
Gist.list_gist(options[:list])
else
Gist.list_gist
end
exit
end

if options[:paste]
puts Gist.gist(Gist.paste, options)
else
Expand All @@ -160,6 +173,7 @@ begin

puts Gist.multi_gist(files, options)
end

rescue Gist::Error => e
puts "Error: #{e.message}"
exit 1
Expand Down
81 changes: 80 additions & 1 deletion lib/gist.rb
Expand Up @@ -40,6 +40,14 @@ def self.exception(*args)
end
class ClipboardError < RuntimeError; include Error end

# access token for authentication
# it helps 'multi_gist()' and 'list_gist()' functions
#
# @return [String] string value of access token or `nil`, if not found
def auth_token
@token ||= File.read(auth_token_file) rescue nil
end

# Upload a gist to https://gist.github.com
#
# @param [String] content the code you'd like to gist
Expand Down Expand Up @@ -90,7 +98,7 @@ def multi_gist(files, options={})
if options[:anonymous]
access_token = nil
else
access_token = (options[:access_token] || File.read(auth_token_file) rescue nil)
access_token = (options[:access_token] || auth_token())
end

url = "#{base_path}/gists"
Expand Down Expand Up @@ -120,6 +128,77 @@ def multi_gist(files, options={})
raise e.extend Error
end

# List all gists(private also) for authenticated user
# otherwise list public gists for given username (optional argument)
#
# @param [String] user
#
# see https://developer.github.com/v3/gists/#list-gists
def list_gist(user = "")
url = "#{base_path}"

if user == ""
access_token = auth_token()
if access_token.to_s != ''
url << "/gists?access_token=" << CGI.escape(access_token)

request = Net::HTTP::Get.new(url)
response = http(api_url, request)

pretty_gist(response)

else
raise Error, "you are't authenticated, use 'gist --login to login.'"
end

else
url << "/users/#{user}/gists"

request = Net::HTTP::Get.new(url)
response = http(api_url, request)

pretty_gist(response)
end
end

# return prettified string result of response body for all gists
# it Helps Gist.list_gist() function
#
# @params [Net::HTTPResponse] response
# @return [String] prettified result of listing all gists
#
# see https://developer.github.com/v3/gists/#response
def pretty_gist(response)
private_gists = ""
public_gists = ""

body = JSON.parse(response.body)
if response.code == '200'
body.each do |gist|

files = []
gist['files'].each do |file|
files.push(file[0])
end

content = "#{gist['html_url']} #{files}\n"
if gist['public']
public_gists << content
else
private_gists << content
end
end

result = "No Gist found for user"
result = public_gists if public_gists.to_s != ""
result << private_gists if private_gists.to_s != ""

puts result
else
raise Error, body['message']
end
end

# Convert long github urls into short git.io ones
#
# @param [String] url
Expand Down

0 comments on commit 974e0d1

Please sign in to comment.