pastr_it 0.1.4 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/pastr_it.rb +55 -25
  2. metadata +2 -2
data/lib/pastr_it.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require "optparse"
2
2
  class PastrIt
3
- VERSION = '0.1.4'
3
+ VERSION = '0.1.6'
4
4
  REALM = 'Pastr Registered User'
5
5
  PasteLink = "http://pastr.it/new"
6
6
  attr_accessor :password, :filename, :title, :network, :channel, :language, :username
@@ -10,6 +10,12 @@ class PastrIt
10
10
  parse_args
11
11
  end
12
12
 
13
+ def self.pastr_it(args)
14
+ me = PastrIt.new(args)
15
+ me.parse_args
16
+ me.pastr_it
17
+ end
18
+
13
19
  def parse_args
14
20
  return @opts if @opts
15
21
  @opts = OptionParser.new
@@ -22,12 +28,19 @@ class PastrIt
22
28
  @opts.on("-t", "--title TITLE", "Title of this paste (Default: Filename or 'Pastr by #{username}')") { |foo| @title = foo }
23
29
  #@opts.on("-f", "--file FILENAME", "Read paste_body from FILENAME (otherwise reads from stdin)") { |foo| @filename = foo }
24
30
  @opts.separator "\tTo paste from STDIN (instead of a file), leave off FILE, and you'll need to auth with a ~/.netrc"
31
+ @opts.separator "\n---------------\n"
32
+ @opts.on("-L", "--list", "List supported languages") { |foo| @list_langs = true }
25
33
 
26
34
  @opts.on_tail("-h", "--help", "Show this message") do
27
35
  puts @opts
28
36
  exit
29
37
  end
30
38
  @opts.parse!(@args)
39
+ if @list_langs
40
+ puts http_request(:url => "http://pastr.it/languages").content
41
+ exit
42
+ end
43
+
31
44
  @filename = ARGV.shift if ARGV.size == 1 and File.file?(ARGV.first)
32
45
  @title ||= File.basename(@filename) if @filename
33
46
  @channel ||= @username
@@ -38,6 +51,43 @@ class PastrIt
38
51
  end
39
52
 
40
53
  def pastr_it
54
+ form = {'network' => network, 'channel' => channel, 'paste_body' => paste_body}
55
+ form["title"] = title if title
56
+ form["language"] = language if language
57
+ puts http_request(:form => form).content
58
+ end
59
+
60
+ private
61
+ def http_request(args)
62
+ form = args[:form] || nil
63
+ url = args[:url] || PasteLink
64
+ require "httpclient"
65
+ check_netrc
66
+ check_password
67
+ client = HTTPClient.new(ENV["HTTP_PROXY"] ? ENV["HTTP_PROXY"] : ENV["http_proxy"])
68
+ # Have to do this to get a valid cookie with the server before we auth (lame)
69
+ res = client.get("http://pastr.it")
70
+ if res.status != 200
71
+ puts "Cannot access http://pastr.it. Webserver said Status: #{res.status} -> #{res.reason}"
72
+ exit 1
73
+ end
74
+ if form
75
+ # Now set auth and post
76
+ client.set_auth(url, username.strip, password.strip)
77
+ res = client.post(url, form)
78
+ else
79
+ # Or set auth and get
80
+ client.set_auth(url, username.strip, password.strip)
81
+ res = client.get(url)
82
+ end
83
+ if res.status != 200
84
+ puts "An error occurred posting to#{PasteLink}. Webserver said Status: #{res.status} -> #{res.reason}"
85
+ exit 1
86
+ end
87
+ res
88
+ end
89
+
90
+ def check_netrc
41
91
  if File.file?(netrc = ENV["HOME"] + "/.netrc")
42
92
  if p_auth = File.readlines(netrc).detect { |line| line.match(/^machine\s+pastr\.it/) }
43
93
  if uname = p_auth.match(/login\s+(\S+)/)
@@ -54,6 +104,9 @@ class PastrIt
54
104
  end
55
105
  end
56
106
  end
107
+ end
108
+
109
+ def check_password
57
110
  unless @password
58
111
  if STDIN.isatty
59
112
  begin
@@ -71,27 +124,9 @@ class PastrIt
71
124
  exit 1
72
125
  end
73
126
  end
74
- form = {'network' => network, 'channel' => channel, 'paste_body' => paste_body}
75
- form["title"] = title if title
76
- form["language"] = language if language
77
- require "httpclient"
78
- client = HTTPClient.new(ENV["HTTP_PROXY"])
79
- # Have to do this to get a valid cookie with the server before we auth (lame)
80
- res = client.get("http://pastr.it")
81
- if res.status != 200
82
- puts "Cannot access http://pastr.it. Webserver said Status: #{res.status} -> #{res.reason}"
83
- exit 1
84
- end
85
- # Now set auth and post
86
- client.set_auth(PasteLink, username.strip, password.strip)
87
- res = client.post(PasteLink, form)
88
- if res.status != 200
89
- puts "An error occurred posting to#{PasteLink}. Webserver said Status: #{res.status} -> #{res.reason}"
90
- exit 1
91
- end
92
- puts res.content
93
127
  end
94
128
 
129
+
95
130
  def paste_body
96
131
  return @paste_body if @paste_body
97
132
  if filename
@@ -102,10 +137,5 @@ class PastrIt
102
137
  end
103
138
  end
104
139
 
105
- def self.pastr_it(args)
106
- me = PastrIt.new(args)
107
- me.parse_args
108
- me.pastr_it
109
- end
110
140
  end
111
141
  #:et;ts=2;sw=2;foldmethod=indent
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.1.4
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jayson Vaughn
@@ -58,7 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
58
58
  requirements: []
59
59
 
60
60
  rubyforge_project:
61
- rubygems_version: 1.2.0
61
+ rubygems_version: 1.3.1
62
62
  signing_key:
63
63
  specification_version: 2
64
64
  summary: A command line program to paste text to http://pastr.it