Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Try to clean up output stuff

  • Loading branch information...
commit 4a98996448610fd26889e0ff179afe73154b15d7 1 parent 1ab99bc
@ConradIrwin authored
View
19 bin/jist
@@ -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
@rking Collaborator
rking added a note

Definite +1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
options[:copy_js] = true
+ options[:copy] = true
end
opts.on("-o", "--open", "Open the resulting URL in a browser") do
@@ -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
+
@rking Collaborator
rking added a note

Good user-facing feature (worth the conditionals, IMO)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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)|
@@ -123,7 +134,7 @@ begin
end
end
- puts Jist.multi_gist(files, options)['html_url']
+ puts Jist.multi_gist(files, options)
end
@rking Collaborator
rking added a note

Yay x3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
rescue Jist::Error => e
puts "Error: #{e.message}"
View
49 lib/jist.rb
@@ -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}
@rking Collaborator
rking added a note

Cool.

YARD has an include thing, though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
#
# @see http://developer.github.com/v3/gists/
def gist(content, options = {})
@@ -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/
@@ -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]
@rking Collaborator
rking added a note

Yep.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Jist.open(json['html_url']) if options[:open]
- json
+ output
end
# Copy a string to the clipboard.
View
6 spec/clipboard_spec.rb
@@ -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
@@ -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
View
2  spec/shorten_spec.rb
@@ -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

3 comments on commit 4a98996

@rking
Collaborator

If you want to RefactorConditionalToPolymorphism, this would be a fun experiment (because these conditionals are almost simple enough not to warrant it, but it'd be an interesting experiment).

@ConradIrwin

@rking thanks for the comments. I didn't consider polymorphism here. It might be interesting to try, though I'm guessing quite boilerplatey.

@rking
Collaborator
Please sign in to comment.
Something went wrong with that request. Please try again.