jist 0.9.2 → 0.10.0.pre.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/bin/jist +13 -1
  2. data/lib/jist.rb +86 -3
  3. metadata +37 -5
data/bin/jist CHANGED
@@ -13,7 +13,7 @@ opts = OptionParser.new do |opts|
13
13
  opts.banner = <<-EOS
14
14
  Jist (v#{Jist::VERSION}) lets you upload to https://gist.github.com/
15
15
 
16
- Usage: #{executable_name} [-p] [-d DESC] [-t TOKEN|-a] [-u URL] [-f NAME]* [FILE]*
16
+ Usage: #{executable_name} [-o|-c] [-p] [-d DESC] [-t TOKEN|-a] [-u URL] [-f NAME]* [FILE]*
17
17
  #{executable_name} --login
18
18
 
19
19
  The content to be uploaded can be passed as a list of files, if none are
@@ -32,6 +32,10 @@ Anonymous gists are not associated with your GitHub account, they can be created
32
32
  with "-a" even after you have used "jist --login". If you already have an access
33
33
  token with the "gist" scope, you can pass that with "-t".
34
34
 
35
+ To copy the resulting URL to your clipboard you can use the -c option, or to just
36
+ open it directly in your browser, use -o. You can add `alias jist='jist -c'` to
37
+ your shell's rc file to configure this behaviour by default.
38
+
35
39
  Instead of creating a new jist, you can update an existing one by passing its ID
36
40
  or URL with "-u". For this to work, you must be logged in, and have created the
37
41
  original gist with the same GitHub account.
@@ -68,6 +72,14 @@ original gist with the same GitHub account.
68
72
  options[:access_token] = token
69
73
  end
70
74
 
75
+ opts.on("-c", "--copy", "Copy the resulting URL to the clipboard") do
76
+ options[:copy] = true
77
+ end
78
+
79
+ opts.on("-o", "--open", "Open the resulting URL in a browser") do
80
+ options[:open] = true
81
+ end
82
+
71
83
  opts.on_tail("-h","--help", "Show this message.") do
72
84
  puts opts
73
85
  exit
@@ -4,13 +4,21 @@ require 'json'
4
4
 
5
5
  # It just gists.
6
6
  module Jist
7
+ extend self
7
8
 
8
- VERSION = '0.9.2'
9
+ VERSION = '0.10.0.pre.1'
10
+
11
+ # A list of clipboard commands with copy and paste support.
12
+ CLIPBOARD_COMMANDS = {
13
+ 'xclip' => 'xclip -o',
14
+ 'xsel' => 'xsel -o',
15
+ 'pbcopy' => 'pbpaste',
16
+ 'putclip' => 'getclip'
17
+ }
9
18
 
10
19
  # Exception tag for errors raised while gisting.
11
20
  module Error; end
12
21
 
13
- module_function
14
22
  # Upload a gist to https://gist.github.com
15
23
  #
16
24
  # @param [String] content the code you'd like to gist
@@ -22,6 +30,8 @@ module Jist
22
30
  # @option options [Boolean] :anonymous (false) is this gist anonymous
23
31
  # @option options [String] :access_token (`File.read("~/.jist")`) The OAuth2 access token.
24
32
  # @option options [String] :update the URL or id of a gist to update
33
+ # @option options [Boolean] :copy (false) Copy resulting URL to clipboard, if successful.
34
+ # @option options [Boolean] :open (false) Open the resulting URL in a browser.
25
35
  #
26
36
  # @return [Hash] the decoded JSON response from the server
27
37
  # @raise [Jist::Error] if something went wrong
@@ -42,6 +52,8 @@ module Jist
42
52
  # @option options [Boolean] :anonymous (false) is this gist anonymous
43
53
  # @option options [String] :access_token (`File.read("~/.jist")`) The OAuth2 access token.
44
54
  # @option options [String] :update the URL or id of a gist to update
55
+ # @option options [Boolean] :copy (false) Copy resulting URL to clipboard, if successful.
56
+ # @option options [Boolean] :open (false) Open the resulting URL in a browser.
45
57
  #
46
58
  # @return [Hash] the decoded JSON response from the server
47
59
  # @raise [Jist::Error] if something went wrong
@@ -79,7 +91,7 @@ module Jist
79
91
  begin
80
92
  response = http(request)
81
93
  if Net::HTTPSuccess === response
82
- JSON.parse(response.body)
94
+ on_success(response.body, options)
83
95
  else
84
96
  raise "Got #{response.class} from gist: #{response.body}"
85
97
  end
@@ -152,4 +164,75 @@ module Jist
152
164
  rescue Timeout::Error
153
165
  raise "Could not connect to https://api.github.com/"
154
166
  end
167
+
168
+ # Called after an HTTP response to gist to perform post-processing.
169
+ #
170
+ # @param [String] body the HTTP-200 response
171
+ # @param [Hash] options any options
172
+ # @option options [Boolean] :copy copy the URL to the clipboard
173
+ # @return [Hash] the parsed JSON response from the server
174
+ def on_success(body, options={})
175
+ json = JSON.parse(body)
176
+
177
+ Jist.copy(json['html_url']) if options[:copy]
178
+ Jist.open(json['html_url']) if options[:open]
179
+
180
+ json
181
+ end
182
+
183
+ # Copy a string to the clipboard.
184
+ #
185
+ # @param [String] content
186
+ # @raise [RuntimeError] if no clipboard integration could be found
187
+ #
188
+ # This method was heavily inspired by defunkt's Gist#copy,
189
+ # @see https://github.com/defunkt/gist/blob/bca9b29/lib/gist.rb#L178
190
+ def copy(content)
191
+ IO.popen(clipboard_command(:copy), 'r+') { |clip| clip.print content }
192
+ raise "Copying to clipboard failed" unless paste == content
193
+ end
194
+
195
+ # Get a string from the clipboard.
196
+ #
197
+ # @param [String] content
198
+ # @raise [RuntimeError] if no clipboard integration could be found
199
+ def paste
200
+ `#{clipboard_command(:paste)}`
201
+ end
202
+
203
+ # Get the command to use for the clipboard action.
204
+ #
205
+ # @param [Symbol] action either :copy or :paste
206
+ # @return [String] the command to run
207
+ # @raise [RuntimeError] if no clipboard integration could be found
208
+ def clipboard_command(action)
209
+ command = CLIPBOARD_COMMANDS.keys.detect do |cmd|
210
+ system("type #{cmd} >/dev/null 2>&1")
211
+ end
212
+ raise "Could not find copy command, tried: #{CLIPBOARD_COMMANDS}" unless command
213
+ action == :copy ? command : CLIPBOARD_COMMANDS[command]
214
+ end
215
+
216
+ # Open a URL in a browser.
217
+ #
218
+ # @param [String] url
219
+ # @raise [RuntimeError] if no browser integration could be found
220
+ #
221
+ # This method was heavily inspired by defunkt's Gist#open,
222
+ # @see https://github.com/defunkt/gist/blob/bca9b29/lib/gist.rb#L157
223
+ def open(url)
224
+ command = if ENV['BROWSER']
225
+ ENV['BROWSER']
226
+ elsif RUBY_PLATFORM =~ /darwin/
227
+ 'open'
228
+ elsif RUBY_PLATFORM =~ /linux/
229
+ 'sensible-browser'
230
+ elsif ENV['OS'] == 'Windows_NT' || RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw|wince/i
231
+ 'start ""'
232
+ else
233
+ raise "Could not work out how to use a browser."
234
+ end
235
+
236
+ `#{command} #{url}`
237
+ end
155
238
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jist
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
5
- prerelease:
4
+ version: 0.10.0.pre.1
5
+ prerelease: 7
6
6
  platform: ruby
7
7
  authors:
8
8
  - Conrad Irwin
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-24 00:00:00.000000000 Z
12
+ date: 2012-08-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -27,6 +27,38 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
30
62
  description: Provides a single function (Jist.gist) that uploads a gist.
31
63
  email: conrad.irwin@gmail.com
32
64
  executables:
@@ -51,9 +83,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
51
83
  required_rubygems_version: !ruby/object:Gem::Requirement
52
84
  none: false
53
85
  requirements:
54
- - - ! '>='
86
+ - - ! '>'
55
87
  - !ruby/object:Gem::Version
56
- version: '0'
88
+ version: 1.3.1
57
89
  requirements: []
58
90
  rubyforge_project:
59
91
  rubygems_version: 1.8.24