Skip to content

Adding the gist -r <id> command to read a gist #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions bin/gist
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ original gist with the same GitHub account.

Usage: #{executable_name} [-o|-c|-e] [-p] [-s] [-R] [-d DESC] [-a] [-u URL] [-P] [-f NAME|-t EXT]* FILE*
#{executable_name} --login
#{executable_name} [-l|-r]

EOS

Expand Down Expand Up @@ -118,6 +119,10 @@ Usage: #{executable_name} [-o|-c|-e] [-p] [-s] [-R] [-d DESC] [-a] [-u URL] [-P]
options[:list] = user
end

opts.on("-r", "--read ID [FILENAME]", "Read a gist and print out the contents") do |id|
options[:read] = id
end

opts.on_tail("-h","--help", "Show this message.") do
puts opts
exit
Expand Down Expand Up @@ -156,6 +161,12 @@ begin
exit
end

if options.key? :read
file_name = ARGV.first
Gist.read_gist(options[:read], file_name)
exit
end

if options[:paste]
puts Gist.gist(Gist.paste, options)
else
Expand Down
22 changes: 22 additions & 0 deletions lib/gist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,28 @@ def list_all_gists(user = "")

end

def read_gist(id, file_name=nil)
url = "#{base_path}/gists/#{id}"
request = Net::HTTP::Get.new(url)
response = http(api_url, request)

if response.code == '200'
body = JSON.parse(response.body)
files = body["files"]

if file_name
file = files[file_name]
raise Error, "Gist with id of #{id} and file #{file_name} does not exist." unless file
else
file = files.values.first
end

puts file["content"]
else
raise Error, "Gist with id of #{id} does not exist."
end
end

def get_gist_pages(url)

request = Net::HTTP::Get.new(url)
Expand Down