jist 0.6 → 0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/bin/jist +18 -9
  2. data/lib/jist.rb +28 -9
  3. metadata +2 -2
data/bin/jist CHANGED
@@ -6,14 +6,14 @@ require 'jist'
6
6
 
7
7
  # For the holdings of options.
8
8
  options = {}
9
-
9
+ filenames = []
10
10
 
11
11
  opts = OptionParser.new do |opts|
12
12
  executable_name = File.split($0)[1]
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] [-f FILENAME] [FILE]
16
+ Usage: #{executable_name} [-p] [-d DESC] [-t TOKEN] [-u URL] [-f NAME]* [FILE]*
17
17
  #{executable_name} --login
18
18
 
19
19
  When used with no arguments, jist creates an anonymous, private, gist, with
@@ -27,10 +27,11 @@ an Oauth2 access token. This is stored and used for all future uses of jist.
27
27
  If you're calling jist from another program that already has an access_token
28
28
  with the "gist" scope, then pass it using `jist -t`.
29
29
 
30
- If you specify a FILE on the command line, then jist will use that as the
31
- default value for FILENAME too. If not, jist will assume that the file you
32
- provide on STDIN is called "a.rb". The FILENAME is mostly important for
33
- determining which language to use for syntax highlighting.
30
+ If you specify FILEs on the command line, then jist will use the names of the
31
+ files you specify as the filenames for the gist. If you'd rather override the
32
+ name you can specify the -f parameter once per file. If you don't specify any
33
+ files, then the -f parameter will be used to name the contents of STDIN. If you
34
+ don't specify the -f parameter at all when using STDIN, "a.rb" will be used.
34
35
 
35
36
  Making a gist public causes it to have a prettier, guessable url. And adding
36
37
  a description can provide useful context to people who stumble across your
@@ -44,6 +45,7 @@ gist.
44
45
  end
45
46
 
46
47
  opts.on("-f", "--filename [NAME.EXTENSION]", "Sets the filename and syntax type.") do |filename|
48
+ filenames << filename
47
49
  options[:filename] = filename
48
50
  end
49
51
 
@@ -55,6 +57,10 @@ gist.
55
57
  options[:description] = description
56
58
  end
57
59
 
60
+ opts.on("-u", "--update [ URL | ID ]", "Update an existing gist.") do |update|
61
+ options[:update] = update
62
+ end
63
+
58
64
  opts.on("-t", "--token OAUTH_TOKEN", "The OAuth2 access_token to use.") do |token|
59
65
  options[:access_token] = token
60
66
  end
@@ -75,8 +81,11 @@ opts.parse!
75
81
  if ARGV.size == 0
76
82
  $stderr.puts "(type a gist. <ctrl-c> to cancel, <ctrl-d> when done)" if $stdin.tty?
77
83
  puts Jist.gist(ARGF.read, options)['html_url']
78
- elsif ARGV.size == 1
79
- puts Jist.gist(File.read(File.expand_path(ARGV[0])), {:filename => File.basename(ARGV[0])}.merge(options))['html_url']
80
84
  else
81
- puts opts
85
+ files = {}
86
+ ARGV.zip(filenames).each do |(file, name)|
87
+ files[name || file] = File.read(File.expand_path(file))
88
+ end
89
+
90
+ puts Jist.multi_gist(files, options)['html_url']
82
91
  end
data/lib/jist.rb CHANGED
@@ -5,7 +5,7 @@ require 'json'
5
5
  # It just gists.
6
6
  module Jist
7
7
 
8
- VERSION = '0.6'
8
+ VERSION = '0.7'
9
9
 
10
10
  module_function
11
11
  # Upload a gist to https://gist.github.com
@@ -17,28 +17,47 @@ module Jist
17
17
  # @option options [String] :filename ('a.rb') the filename
18
18
  # @option options [Boolean] :public (false) is this gist public
19
19
  # @option options [String] :access_token (`File.read("~/.jist")`) The OAuth2 access token.
20
+ # @option options [String] :update the URL or id of a gist to update
20
21
  #
21
22
  # @return [Hash] the decoded JSON response from the server
22
23
  # @raise [Exception] if something went wrong
23
24
  #
24
25
  # @see http://developer.github.com/v3/gists/
25
26
  def gist(content, options={})
27
+ filename = options[:filename] || "a.rb"
28
+ multi_gist({filename => content}, options)
29
+ end
30
+
31
+ # Upload a gist to https://gist.github.com
32
+ #
33
+ # @param [Hash] files the code you'd like to gist {filename => content}
34
+ # @param [Hash] options more detailed options
35
+ #
36
+ # @option options [String] :description the description
37
+ # @option options [Boolean] :public (false) is this gist public
38
+ # @option options [String] :access_token (`File.read("~/.jist")`) The OAuth2 access token.
39
+ # @option options [String] :update the URL or id of a gist to update
40
+ #
41
+ # @return [Hash] the decoded JSON response from the server
42
+ # @raise [Exception] if something went wrong
43
+ #
44
+ # @see http://developer.github.com/v3/gists/
45
+ def multi_gist(files, options={})
26
46
  json = {}
27
47
 
28
48
  json[:description] = options[:description] if options[:description]
29
49
  json[:public] = !!options[:public]
50
+ json[:files] = {}
30
51
 
31
- filename = options[:filename] || 'a.rb'
32
-
33
- json[:files] = {
34
- filename => {
35
- :content => content
36
- }
37
- }
52
+ files.each_pair do |(name, content)|
53
+ json[:files][File.basename(name)] = {:content => content}
54
+ end
38
55
 
56
+ existing_gist = options[:update].to_s.split("/").last
39
57
  access_token = (options[:access_token] || File.read(File.expand_path("~/.jist")) rescue nil)
40
58
 
41
59
  url = "/gists"
60
+ url << "/" << CGI.escape(existing_gist) if existing_gist.to_s != ''
42
61
  url << "?access_token=" << CGI.escape(access_token) if access_token.to_s != ''
43
62
 
44
63
  request = Net::HTTP::Post.new(url)
@@ -46,7 +65,7 @@ module Jist
46
65
 
47
66
  response = http(request)
48
67
 
49
- if Net::HTTPCreated === response
68
+ if Net::HTTPSuccess === response
50
69
  JSON.parse(response.body)
51
70
  else
52
71
  raise RuntimeError.new "Got #{response.class} from gist: #{response.body}"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jist
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.6'
4
+ version: '0.7'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-23 00:00:00.000000000 Z
12
+ date: 2012-06-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json