Skip to content

Commit

Permalink
Try to clean up output stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
ConradIrwin committed Feb 24, 2013
1 parent 1ab99bc commit 4a98996
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 33 deletions.
19 changes: 15 additions & 4 deletions bin/jist
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ Usage: #{executable_name} [-o|-c|-j] [-p] [-s] [-d DESC] [-t TOKEN|-a] [-u URL]
opts.on("-c", "--copy", "Copy the resulting URL to the clipboard") do
options[:copy] = true
end
opts.on("-j", "--copy-js", "Copy the resulting embeddable URL to the clipboard") do
opts.on("-j", "--copy-js", "Copy the embed code for the gist to the clipboard") do
options[:copy_js] = true
options[:copy] = true
end

opts.on("-o", "--open", "Open the resulting URL in a browser") do
Expand All @@ -108,11 +109,21 @@ end
opts.parse!

begin
options[:output] = if options[:copy_js] && options[:shorten]
raise Jist::Error, "--copy-js does not make sense with --shorten"
elsif options[:copy_js]
:javascript
elsif options[:shorten]
:short_url
else
:html_url
end

if options[:paste]
puts Jist.gist(Jist.paste, options)['html_url']
puts Jist.gist(Jist.paste, options)
elsif ARGV.size == 0
$stderr.puts "(type a gist. <ctrl-c> to cancel, <ctrl-d> when done)" if $stdin.tty?
puts Jist.gist(ARGF.read, options)['html_url']
puts Jist.gist(ARGF.read, options)
else
files = {}
ARGV.zip(filenames).each do |(file, name)|
Expand All @@ -123,7 +134,7 @@ begin
end
end

puts Jist.multi_gist(files, options)['html_url']
puts Jist.multi_gist(files, options)
end
rescue Jist::Error => e
puts "Error: #{e.message}"
Expand Down
49 changes: 24 additions & 25 deletions lib/jist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,8 @@ class ClipboardError < RuntimeError; include Error end
# Upload a gist to https://gist.github.com
#
# @param [String] content the code you'd like to gist
# @param [Hash] options more detailed options
#
# @option options [String] :description the description
# @option options [String] :filename ('a.rb') the filename
# @option options [Boolean] :public (false) is this gist public
# @option options [Boolean] :anonymous (false) is this gist anonymous
# @option options [Boolean] :shorten (false) Shorten the resulting URL using git.io.
# @option options [String] :access_token (`File.read("~/.jist")`) The OAuth2 access token.
# @option options [String] :update the URL or id of a gist to update
# @option options [Boolean] :copy (false) Copy resulting URL to clipboard, if successful.
# @option options [Boolean] :open (false) Open the resulting URL in a browser.
#
# @return [Hash] the decoded JSON response from the server
# @raise [Jist::Error] if something went wrong
# @param [Hash] options more detailed options, see
# the documentation for {multi_gist}
#
# @see http://developer.github.com/v3/gists/
def gist(content, options = {})
Expand All @@ -60,13 +48,17 @@ def gist(content, options = {})
# @option options [String] :description the description
# @option options [Boolean] :public (false) is this gist public
# @option options [Boolean] :anonymous (false) is this gist anonymous
# @option options [Boolean] :shorten (false) Shorten the resulting URL using git.io.
# @option options [String] :access_token (`File.read("~/.jist")`) The OAuth2 access token.
# @option options [String] :update the URL or id of a gist to update
# @option options [Boolean] :copy (false) Copy resulting URL to clipboard, if successful.
# @option options [Boolean] :open (false) Open the resulting URL in a browser.
# @option options [Symbol] :output (:all) The type of return value you'd like:
# :html_url gives a String containing the url to the gist in a browser
# :short_url gives a String contianing a git.io url that redirects to html_url
# :javascript gives a String containing a script tag suitable for embedding the gist
# :all gives a Hash containing the parsed json response from the server
#
# @return [Hash] the decoded JSON response from the server
# @return [String, Hash] the return value as configured by options[:output]
# @raise [Jist::Error] if something went wrong
#
# @see http://developer.github.com/v3/gists/
Expand Down Expand Up @@ -211,20 +203,27 @@ def http(url, request)

# Called after an HTTP response to gist to perform post-processing.
#
# @param [String] body the HTTP-200 response
# @param [Hash] options any options
# @option options [Boolean] :copy copy the URL to the clipboard
# @return [Hash] the parsed JSON response from the server
# @param [String] body the text body from the github api
# @param [Hash] options more detailed options, see
# the documentation for {multi_gist}
def on_success(body, options={})
json = JSON.parse(body)

json['html_url'] = shorten(json['html_url']) if options[:shorten]
js_link = %Q{<script src="#{json['html_url']}.js"></script>}
Jist.copy(json['html_url']) if options[:copy]
Jist.copy(js_link) if options[:copy_js]
output = case options[:output]
when :javascript
%Q{<script src="#{json['html_url']}.js"></script>}
when :html_url
json['html_url']
when :short_url
shorten(json['html_url'])
else
json
end

Jist.copy(output.to_s) if options[:copy]
Jist.open(json['html_url']) if options[:open]

json
output
end

# Copy a string to the clipboard.
Expand Down
6 changes: 3 additions & 3 deletions spec/clipboard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
end

def ask_for_copy
Jist.on_success({'html_url' => @bobo_url}.to_json, :copy => true )
Jist.on_success({'html_url' => @bobo_url}.to_json, :copy => true, :output => :html_url)
end
def jist_but_dont_ask_for_copy
Jist.on_success({'html_url' => 'http://example.com/'}.to_json)
Jist.on_success({'html_url' => 'http://example.com/'}.to_json, :output => :html_url)
end

it 'should try to copy the url when the clipboard option is passed' do
Expand All @@ -23,7 +23,7 @@ def jist_but_dont_ask_for_copy
it 'should try to copy the embed url when the clipboard-js option is passed' do
js_link = %Q{<script src="#{@bobo_url}.js"></script>}
Jist.should_receive(:copy).with(js_link)
Jist.on_success({'html_url' => @bobo_url}.to_json, :copy_js => true )
Jist.on_success({'html_url' => @bobo_url}.to_json, :copy => true, :output => :javascript)
end

it "should not copy when not asked to" do
Expand Down
2 changes: 1 addition & 1 deletion spec/shorten_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
end

it "should return a shortened version of the URL" do
Jist.gist("Test gist", :shorten => true).should == {"html_url" => "http://git.io/XXXXXX"}
Jist.gist("Test gist", :output => :short_url).should == "http://git.io/XXXXXX"
end
end

0 comments on commit 4a98996

Please sign in to comment.