Skip to content

Commit 59c0b8f

Browse files
committed
Merge pull request #219 from ibash/master
Adding the gist -r <id> command to read a gist
2 parents 30d3228 + bf26322 commit 59c0b8f

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

bin/gist

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ original gist with the same GitHub account.
4949
5050
Usage: #{executable_name} [-o|-c|-e] [-p] [-s] [-R] [-d DESC] [-a] [-u URL] [-P] [-f NAME|-t EXT]* FILE*
5151
#{executable_name} --login
52+
#{executable_name} [-l|-r]
5253
5354
EOS
5455

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

122+
opts.on("-r", "--read ID [FILENAME]", "Read a gist and print out the contents") do |id|
123+
options[:read] = id
124+
end
125+
121126
opts.on_tail("-h","--help", "Show this message.") do
122127
puts opts
123128
exit
@@ -156,6 +161,12 @@ begin
156161
exit
157162
end
158163

164+
if options.key? :read
165+
file_name = ARGV.first
166+
Gist.read_gist(options[:read], file_name)
167+
exit
168+
end
169+
159170
if options[:paste]
160171
puts Gist.gist(Gist.paste, options)
161172
else

lib/gist.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,28 @@ def list_all_gists(user = "")
201201

202202
end
203203

204+
def read_gist(id, file_name=nil)
205+
url = "#{base_path}/gists/#{id}"
206+
request = Net::HTTP::Get.new(url)
207+
response = http(api_url, request)
208+
209+
if response.code == '200'
210+
body = JSON.parse(response.body)
211+
files = body["files"]
212+
213+
if file_name
214+
file = files[file_name]
215+
raise Error, "Gist with id of #{id} and file #{file_name} does not exist." unless file
216+
else
217+
file = files.values.first
218+
end
219+
220+
puts file["content"]
221+
else
222+
raise Error, "Gist with id of #{id} does not exist."
223+
end
224+
end
225+
204226
def get_gist_pages(url)
205227

206228
request = Net::HTTP::Get.new(url)

0 commit comments

Comments
 (0)