pastr_it 0.1.7 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/pastr_it.rb +60 -22
- metadata +2 -2
data/lib/pastr_it.rb
CHANGED
@@ -1,9 +1,12 @@
|
|
1
1
|
require "optparse"
|
2
2
|
class PastrIt
|
3
|
-
VERSION = '0.
|
3
|
+
VERSION = '1.0.0'
|
4
4
|
REALM = 'Pastr Registered User'
|
5
|
-
|
6
|
-
|
5
|
+
PastrHome = "http://pastr.it"
|
6
|
+
PastrNew = "%s/%s" % [PastrHome, :new]
|
7
|
+
PastrEdit = "%s/%s" % [PastrHome, :edit]
|
8
|
+
PastrNote = "%s/%s" % [PastrHome, :annotate]
|
9
|
+
attr_accessor :password, :filename, :title, :network, :channel, :language, :username, :annotate_id, :pastr_id, :no_body
|
7
10
|
def initialize(args = nil)
|
8
11
|
@network, @username = "Freenode", ENV["USER"]
|
9
12
|
@args = args || ARGV
|
@@ -13,26 +16,38 @@ class PastrIt
|
|
13
16
|
def self.pastr_it(args)
|
14
17
|
me = PastrIt.new(args)
|
15
18
|
me.parse_args
|
16
|
-
me.
|
19
|
+
if me.annotate_id
|
20
|
+
me.annotate_it
|
21
|
+
elsif me.pastr_id
|
22
|
+
me.edit_it
|
23
|
+
else
|
24
|
+
me.pastr_it
|
25
|
+
end
|
17
26
|
end
|
18
27
|
|
19
28
|
def parse_args
|
20
29
|
return @opts if @opts
|
21
30
|
@opts = OptionParser.new
|
22
|
-
@opts.banner = "\nUsage: pastr-it [options] FILE
|
23
|
-
|
31
|
+
@opts.banner = "\nUsage: pastr-it [options] FILE"
|
32
|
+
|
33
|
+
@opts.separator "--- Common Pastr Options"
|
34
|
+
@opts.separator " To paste from STDIN (instead of a file), leave off FILE, and you'll need to auth with a ~/.netrc"
|
24
35
|
@opts.on("-u", "--username USERNAME", "Your username (Default: #{@username})") { |foo| @username = foo }
|
25
36
|
@opts.on("-c", "--channel CHANNEL", "IRC Channel for this Pastr (Default: same as username)") { |foo| @channel = foo }
|
26
37
|
@opts.separator "\tWhen using your username as the channel argument, the paste will be private"
|
27
38
|
@opts.on("-n", "--network NETWORK", "IRC Network for this Pastr (Default: #{network})") { |foo| @network = foo }
|
28
39
|
@opts.on("-l", "--language LANGUAGE", "Language to use for syntax highlighting") { |foo| @language = foo }
|
29
40
|
@opts.on("-t", "--title TITLE", "Title of this paste (Default: Filename or 'Pastr by #{username}')") { |foo| @title = foo }
|
30
|
-
# coming soon
|
31
|
-
#@opts.on("-i", "--id PID", "ID Of paste to edit (instead of making a new paste)") { |foo| @pastr_id = foo }
|
32
|
-
#@opts.on("-a", "--annotate PID", "ID Of paste to annotate (incompatible with -i)") { |foo| @annotate_id = foo }
|
33
|
-
@opts.separator "\n---------------\n"
|
34
|
-
@opts.on("-L", "--list", "List supported languages") { |foo| @list_langs = true }
|
35
41
|
|
42
|
+
@opts.separator "--- Editing"
|
43
|
+
@opts.on("-e", "--edit PASTR_ID", "ID Of paste to edit (instead of making a new paste)") { |foo| @pastr_id = foo }
|
44
|
+
@opts.on("-N", "--no-body", "Just edit metadata (channel, language, etc), not the paste body") { |foo| @no_body = true }
|
45
|
+
|
46
|
+
@opts.separator "--- Annotate"
|
47
|
+
@opts.on("-a", "--annotate PASTR_ID", "ID Of paste to annotate (incompatible with -e/--edit. -n/--network, and -c/--channel ignored.)") { |foo| @annotate_id = foo }
|
48
|
+
|
49
|
+
@opts.separator "--- Informational"
|
50
|
+
@opts.on("-L", "--list", "List supported languages") { |foo| @list_langs = true }
|
36
51
|
@opts.on("-v", "--version", "Print pastr version") { |foo| @version_only = true }
|
37
52
|
@opts.on_tail("-h", "--help", "Show this message") do
|
38
53
|
puts @opts
|
@@ -53,13 +68,34 @@ class PastrIt
|
|
53
68
|
@channel ||= @username
|
54
69
|
if @filename.nil? and STDIN.isatty
|
55
70
|
$stderr.puts "No Input on STDIN and no filename given, what am I supposed to paste?"
|
71
|
+
puts @opts
|
56
72
|
exit 1
|
57
|
-
end
|
73
|
+
end unless @pastr_id
|
74
|
+
end
|
75
|
+
|
76
|
+
def annotate_it
|
77
|
+
aid = "annotation_#{@annotate_id}"
|
78
|
+
form = {"#{aid}[paste_body]" => paste_body}
|
79
|
+
form["#{aid}[title]"] = title if title
|
80
|
+
form["language"] = language if language
|
81
|
+
puts http_request(:form => form, :url => "%s/%s" % [PastrNote, @annotate_id]).content
|
82
|
+
end
|
83
|
+
|
84
|
+
def edit_it
|
85
|
+
pid = "pastr_#{@paste_id}"
|
86
|
+
form = {"#{pid}[network]" => network, "#{pid}[channel]" => channel, "#{pid}[paste_body]" => paste_body}
|
87
|
+
form = {}
|
88
|
+
form["#{pid}[network]"] = network if network
|
89
|
+
form["#{pid}[channel]"] = channel if channel
|
90
|
+
form["#{pid}[title]"] = title if title
|
91
|
+
form["language"] = language if language
|
92
|
+
form["#{pid}[paste_body]"] = paste_body unless no_body
|
93
|
+
puts http_request(:form => form, :url => "%s/%s" % [PastrEdit, @pastr_id]).content
|
58
94
|
end
|
59
95
|
|
60
96
|
def pastr_it
|
61
97
|
form = {'network' => network, 'channel' => channel, 'paste_body' => paste_body}
|
62
|
-
form["title"]
|
98
|
+
form["title"] = title if title
|
63
99
|
form["language"] = language if language
|
64
100
|
puts http_request(:form => form).content
|
65
101
|
end
|
@@ -67,28 +103,31 @@ class PastrIt
|
|
67
103
|
private
|
68
104
|
def http_request(args)
|
69
105
|
form = args[:form] || nil
|
70
|
-
url = args[:url] ||
|
106
|
+
url = args[:url] || PastrNew
|
107
|
+
auth = args[:auth] || true
|
71
108
|
require "httpclient"
|
72
|
-
|
73
|
-
|
109
|
+
if auth
|
110
|
+
check_netrc
|
111
|
+
check_password
|
112
|
+
end
|
74
113
|
client = HTTPClient.new(ENV["HTTP_PROXY"] ? ENV["HTTP_PROXY"] : ENV["http_proxy"])
|
75
114
|
# Have to do this to get a valid cookie with the server before we auth (lame)
|
76
|
-
res = client.get(
|
115
|
+
res = client.get(PastrHome)
|
77
116
|
if res.status != 200
|
78
|
-
puts "Cannot access
|
117
|
+
puts "Cannot access #{PastrHome}. Webserver said Status: #{res.status} -> #{res.reason}"
|
79
118
|
exit 1
|
80
119
|
end
|
81
120
|
if form
|
82
121
|
# Now set auth and post
|
83
|
-
client.set_auth(url, username.strip, password.strip)
|
122
|
+
client.set_auth(url, username.strip, password.strip) if auth
|
84
123
|
res = client.post(url, form)
|
85
124
|
else
|
86
125
|
# Or set auth and get
|
87
|
-
client.set_auth(url, username.strip, password.strip)
|
126
|
+
client.set_auth(url, username.strip, password.strip) if auth
|
88
127
|
res = client.get(url)
|
89
128
|
end
|
90
129
|
if res.status != 200
|
91
|
-
puts "An error occurred posting to#{
|
130
|
+
puts "An error occurred posting to #{url}. Webserver said Status: #{res.status} -> #{res.reason}"
|
92
131
|
exit 1
|
93
132
|
end
|
94
133
|
res
|
@@ -135,7 +174,6 @@ class PastrIt
|
|
135
174
|
end
|
136
175
|
end
|
137
176
|
|
138
|
-
|
139
177
|
def paste_body
|
140
178
|
return @paste_body if @paste_body
|
141
179
|
if filename
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pastr_it
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jayson Vaughn
|
@@ -12,7 +12,7 @@ autorequire:
|
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
14
|
|
15
|
-
date: 2009-04-
|
15
|
+
date: 2009-04-12 00:00:00 -05:00
|
16
16
|
default_executable:
|
17
17
|
dependencies:
|
18
18
|
- !ruby/object:Gem::Dependency
|