code2gist 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/README.md +9 -0
- data/bin/code2gist +32 -0
- data/code2gist.gemspec +1 -1
- data/lib/code2gist.rb +13 -1
- data/test/code2gist_test.rb +11 -3
- metadata +5 -1
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -44,6 +44,15 @@ gists:
|
|
44
44
|
Code2Gist::Config.github_login = "your_login"
|
45
45
|
Code2Gist::Config.github_token = "your_token"
|
46
46
|
|
47
|
+
### Command-line interface
|
48
|
+
|
49
|
+
Use `code2gist`from the command-line. Just run:
|
50
|
+
|
51
|
+
code2gist file "optional description"
|
52
|
+
|
53
|
+
It supports all options. Check `code2gist --help` to see how to use them
|
54
|
+
properly.
|
55
|
+
|
47
56
|
## Real example
|
48
57
|
|
49
58
|
code = <<-eoc
|
data/bin/code2gist
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "optparse"
|
4
|
+
require "code2gist"
|
5
|
+
|
6
|
+
options = {}
|
7
|
+
OptionParser.new do |opt|
|
8
|
+
opt.banner = "Usage: code2gist file [description] [OPTS]"
|
9
|
+
opt.on('-e', '--embed', 'Embed links to the code', "Default: false") { |e| options[:embed] = e }
|
10
|
+
opt.on('-h', '--html', 'Assume it\'s HTML. Use this in combination with --embed.', "Default: false") { |h| options[:html] = h }
|
11
|
+
opt.on('-l', '--login LOGIN', String, 'Your GitHub login (to be the owner of the gist)') { |l| options[:login] = l }
|
12
|
+
opt.on('-t', '--token TOKEN', String, 'Your GitHub api token (to be the owner of the gist)') { |t| options[:token] = t }
|
13
|
+
opt.parse!(ARGV)
|
14
|
+
end
|
15
|
+
|
16
|
+
path = ARGV[0]
|
17
|
+
description = ARGV[1] || ""
|
18
|
+
|
19
|
+
if path.nil? || !File.exists?(path)
|
20
|
+
$stderr.puts "You must specify a valid file to read from"
|
21
|
+
exit
|
22
|
+
end
|
23
|
+
|
24
|
+
login = options.delete(:login)
|
25
|
+
token = options.delete(:token)
|
26
|
+
|
27
|
+
if login && options
|
28
|
+
Code2Gist::Config.github_login = login
|
29
|
+
Code2Gist::Config.github_token = token
|
30
|
+
end
|
31
|
+
|
32
|
+
puts Code2Gist.upload(File.read(path), description, options)
|
data/code2gist.gemspec
CHANGED
data/lib/code2gist.rb
CHANGED
@@ -26,7 +26,13 @@ module Code2Gist
|
|
26
26
|
code_regex = /```(\w+\.\w+)?[^\n]*\n(.*?)```/m
|
27
27
|
new_text = name_nameless_code_blocks(text)
|
28
28
|
|
29
|
-
|
29
|
+
code_blocks = Hash[*new_text.scan(code_regex).flatten]
|
30
|
+
|
31
|
+
if code_blocks.empty?
|
32
|
+
return "No code blocks found"
|
33
|
+
end
|
34
|
+
|
35
|
+
gist_url = get_gist(code_blocks, description)
|
30
36
|
|
31
37
|
if options[:embed]
|
32
38
|
if options[:html]
|
@@ -69,6 +75,12 @@ module Code2Gist
|
|
69
75
|
post_data.merge!("description" => description)
|
70
76
|
|
71
77
|
result = Net::HTTP.post_form(URI.parse("http://gist.github.com/api/v1/json/new"), post_data)
|
78
|
+
if result.code == "401"
|
79
|
+
$stderr.puts "Your GitHub login/token credentials are incorrect"
|
80
|
+
elsif result.code != "200"
|
81
|
+
$stderr.puts "There was a problem communicating with http://gist.github.com (#{result.code} error)"
|
82
|
+
end
|
83
|
+
|
72
84
|
parsed_json = JSON(result.body)
|
73
85
|
repo = nil
|
74
86
|
parsed_json['gists'].each do |key, val|
|
data/test/code2gist_test.rb
CHANGED
@@ -24,10 +24,18 @@ class Code2GistTest < Test::Unit::TestCase
|
|
24
24
|
The end
|
25
25
|
eoc
|
26
26
|
|
27
|
-
|
27
|
+
nocode = <<-eonc
|
28
|
+
There is no code here
|
28
29
|
|
29
|
-
|
30
|
+
But it should work normally
|
31
|
+
eonc
|
30
32
|
|
31
|
-
|
33
|
+
assert Code2Gist.upload(code, "test description") =~ /https:\/\/gist\.github\.com\/\d+/
|
34
|
+
|
35
|
+
assert Code2Gist.upload(code, "another description", :embed => true) =~ /https:\/\/gist.github.com\/\d+\?file=\w+\.\w+/
|
36
|
+
|
37
|
+
assert Code2Gist.upload(code, "yet another description", :embed => true, :html => true) =~ /<script src="https:\/\/gist.github.com\/\d+\.js\?file=\w+\.\w+"><\/script>/
|
38
|
+
|
39
|
+
assert Code2Gist.upload(nocode, "no code!") == "No code blocks found"
|
32
40
|
end
|
33
41
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: code2gist
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -24,6 +24,7 @@ files:
|
|
24
24
|
- LICENSE
|
25
25
|
- README.md
|
26
26
|
- Rakefile
|
27
|
+
- bin/code2gist
|
27
28
|
- code2gist.gemspec
|
28
29
|
- lib/code2gist.rb
|
29
30
|
- test/code2gist_test.rb
|
@@ -39,6 +40,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
39
40
|
- - ! '>='
|
40
41
|
- !ruby/object:Gem::Version
|
41
42
|
version: '0'
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
hash: 4231852110147090976
|
42
46
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
47
|
none: false
|
44
48
|
requirements:
|