rpaste 0.0.9

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.
data/History.txt ADDED
@@ -0,0 +1,6 @@
1
+ == 0.0.9 / 2008-01-09
2
+
3
+ * Initial release.
4
+ * Supports NoPaste paste board.
5
+ * Semi-support for PasteBin.
6
+
data/LICENSE.txt ADDED
@@ -0,0 +1,23 @@
1
+
2
+
3
+ The MIT License
4
+
5
+ Copyright (c) 2007-2008 Hal Brodigan
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in
15
+ all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ THE SOFTWARE.
data/Manifest.txt ADDED
@@ -0,0 +1,24 @@
1
+ History.txt
2
+ LICENSE.txt
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ lib/rpaste.rb
7
+ lib/rpaste/version.rb
8
+ lib/rpaste/rpaste.rb
9
+ lib/rpaste/metadata.rb
10
+ lib/rpaste/paste.rb
11
+ lib/rpaste/result_set.rb
12
+ lib/rpaste/nopaste/metadata.rb
13
+ lib/rpaste/nopaste/paste.rb
14
+ lib/rpaste/nopaste/result_set.rb
15
+ lib/rpaste/nopaste/recent.rb
16
+ lib/rpaste/nopaste/nopaste.rb
17
+ lib/rpaste/nopaste.rb
18
+ lib/rpaste/pastebin/metadata.rb
19
+ lib/rpaste/pastebin/paste.rb
20
+ lib/rpaste/pastebin/result_set.rb
21
+ lib/rpaste/pastebin/recent.rb
22
+ lib/rpaste/pastebin/pastebin.rb
23
+ lib/rpaste/pastebin.rb
24
+ test/test_rpaste.rb
data/README.txt ADDED
@@ -0,0 +1,48 @@
1
+ RPaste
2
+ by Postmodern Modulus III
3
+ http://rubyforge.net/projects/rpaste/
4
+
5
+ == DESCRIPTION:
6
+
7
+ RPaste provides access to many of the online paste services. Currently
8
+ RPaste supports NoPaste and PasteBin.
9
+
10
+ == FEATURES/PROBLEMS:
11
+
12
+ * Supports posting and retrieving from NoPaste.
13
+ * Supports retrieving from PasteBin.
14
+ * Provides HTTP access with custom User-Agent strings.
15
+
16
+ == REQUIREMENTS:
17
+
18
+ * Hpricot
19
+ * Mechanize
20
+
21
+ == INSTALL:
22
+
23
+ $ sudo gem install rpaste
24
+
25
+ == LICENSE:
26
+
27
+ The MIT License
28
+
29
+ Copyright (c) 2007-2008 Hal Brodigan
30
+
31
+ Permission is hereby granted, free of charge, to any person obtaining
32
+ a copy of this software and associated documentation files (the
33
+ 'Software'), to deal in the Software without restriction, including
34
+ without limitation the rights to use, copy, modify, merge, publish,
35
+ distribute, sublicense, and/or sell copies of the Software, and to
36
+ permit persons to whom the Software is furnished to do so, subject to
37
+ the following conditions:
38
+
39
+ The above copyright notice and this permission notice shall be
40
+ included in all copies or substantial portions of the Software.
41
+
42
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
43
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
45
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
46
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
47
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
48
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require './lib/rpaste/version.rb'
6
+
7
+ Hoe.new('rpaste', RPaste::VERSION) do |p|
8
+ p.rubyforge_name = 'rpaste'
9
+ p.author = 'Postmodern Modulus III'
10
+ p.email = 'postmodern.mod3@gmail.com'
11
+ p.summary = 'RPaste provides access to many of the online paste services'
12
+ p.description = p.paragraphs_of('README.txt', 2..5).join("\n\n")
13
+ p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[1..-1]
14
+ p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
15
+ p.extra_deps = ['hpricot', 'mechanize']
16
+ end
17
+
18
+ # vim: syntax=Ruby
@@ -0,0 +1,23 @@
1
+ module RPaste
2
+ class Metadata
3
+
4
+ # Name of the paste
5
+ attr_accessor :name
6
+
7
+ # Author of the paste
8
+ attr_accessor :author
9
+
10
+ #
11
+ # Creates a new Metadata object with the given _name_ and _author_.
12
+ # If a block is given, it will be passed the newly created Metadata
13
+ # object.
14
+ #
15
+ def initialize(name,author,&block)
16
+ @name = name
17
+ @author = author
18
+
19
+ block.call(self) if block
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,50 @@
1
+ require 'rpaste/nopaste/paste'
2
+ require 'rpaste/metadata'
3
+
4
+ module RPaste
5
+ module NoPaste
6
+ class Metadata < RPaste::Metadata
7
+
8
+ include Comparable
9
+
10
+ # Date of the paste
11
+ attr_accessor :date
12
+
13
+ # Syntax of the paste text
14
+ attr_accessor :syntax
15
+
16
+ # Description of the paste
17
+ attr_accessor :description
18
+
19
+ #
20
+ # Creates a new NoPaste Metadata object with the specified _name_,
21
+ # _author_, _date_, _syntax_ and _description_.
22
+ #
23
+ def initialize(name,author,date,syntax,description,&block)
24
+ @date = Time.parse(date)
25
+ @syntax = syntax
26
+ @description = description
27
+
28
+ super(name,author,&block)
29
+ end
30
+
31
+ #
32
+ # Returns the NoPaste Paste object associated with the Metadata
33
+ # object. If _opts_ are given they will be used in accessing
34
+ # the NoPaste website.
35
+ #
36
+ def paste(opts={})
37
+ Paste.paste(name,opts)
38
+ end
39
+
40
+ #
41
+ # Compare self to another NoPaste Metadata object based on their
42
+ # respective dates.
43
+ #
44
+ def <=>(other)
45
+ @date <=> other.date
46
+ end
47
+
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,60 @@
1
+ require 'rpaste/nopaste/recent'
2
+ require 'rpaste/nopaste/paste'
3
+ require 'rpaste/rpaste'
4
+
5
+ require 'hpricot'
6
+
7
+ module RPaste
8
+ module NoPaste
9
+ #
10
+ # Returns the list of all recent pastes on NoPaste. See Recent.get.
11
+ #
12
+ def NoPaste.recent(opts={},&block)
13
+ Recent.get(opts,&block)
14
+ end
15
+
16
+ #
17
+ # Retrieves the NoPaste Paste object associated with the specified
18
+ # _name_. See Paste.paste.
19
+ #
20
+ def NoPaste.paste(name,opts={})
21
+ Paste.paste(name,opts)
22
+ end
23
+
24
+ #
25
+ # Submits a new paste to NoPaste. If a _block_ is given, then it will be
26
+ # passed the Paste object before it is submitted to NoPaste.
27
+ #
28
+ # NoPaste.post(:author => 'briefcase man') do |paste|
29
+ # paste.description = 'todays key'
30
+ # paste.text = 'is brought to you by the letter S, for symmetric'
31
+ # end
32
+ #
33
+ def NoPaste.post(opts={},&block)
34
+ author = opts[:author]
35
+ syntax = opts[:syntax]
36
+ description = opts[:description]
37
+ text = opts[:text]
38
+
39
+ paste = Paste.new(nil,author,nil,syntax,description,text,&block)
40
+
41
+ agent = RPaste.http_agent(opts)
42
+ page = agent.get('http://rafb.net/paste/index.html')
43
+ form = page.forms.first
44
+
45
+ form.lang = paste.syntax if paste.syntax
46
+ form.nick = paste.author if paste.author
47
+ form.desc = paste.description if paste.description
48
+
49
+ if opts[:convert_tabs]
50
+ form.cvt_tabs = opts[:convert_tabs]
51
+ end
52
+
53
+ form.text = paste.text
54
+ page = agent.submit(form)
55
+
56
+ paste.name = page.search('//tr[3]/td/small').inner_text.gsub(/^URL: .*\//,'').gsub(/\.html$/,'')
57
+ return paste
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,50 @@
1
+ require 'rpaste/paste'
2
+ require 'rpaste/rpaste'
3
+
4
+ require 'hpricot'
5
+
6
+ module RPaste
7
+ module NoPaste
8
+ class Paste < RPaste::Paste
9
+
10
+ PREFIX_URL = 'http://rafb.net/p/'
11
+
12
+ # Description of the entry
13
+ attr_accessor :description
14
+
15
+ #
16
+ # Creates a new NoPaste Paste object with the given _name_, _author_,
17
+ # _date_, _syntax_, _description_ and _text_.
18
+ #
19
+ def initialize(name=nil,author=nil,date=nil,syntax=nil,description=nil,text=nil,&block)
20
+ @description = description
21
+
22
+ super(name,author,date,syntax,text,&block)
23
+ end
24
+
25
+ #
26
+ # Returns the URL of the NoPaste Paste object.
27
+ #
28
+ def url
29
+ "#{PREFIX_URL}#{@name}.html"
30
+ end
31
+
32
+ #
33
+ # Retrieves the NoPaste Paste object associated with the specified
34
+ # _name_. If _opts_ are given they will be used in accessing the
35
+ # NoPaste website.
36
+ #
37
+ def self.paste(name,opts={})
38
+ page = Hpricot(RPaste.open("#{PREFIX_URL}#{name}.html",opts))
39
+
40
+ return Paste.new(name) do |paste|
41
+ paste.syntax = page.search('//table[1]/tr[1]/td/small/b[1]').inner_text
42
+ paste.author = page.search('//table[1]/tr[1]/td/small/b[2]').inner_text
43
+ paste.description = page.search('//table[1]/tr[2]/td/small').inner_text.gsub(/^Description: /,'')
44
+ paste.text = page.search('#codemain').inner_text
45
+ end
46
+ end
47
+
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,47 @@
1
+ require 'rpaste/nopaste/result_set'
2
+ require 'rpaste/rpaste'
3
+
4
+ require 'hpricot'
5
+
6
+ module RPaste
7
+ module NoPaste
8
+ class Recent < ResultSet
9
+
10
+ URL = 'http://www.rafb.net/paste/results.html'
11
+
12
+ #
13
+ # Returns the list of recent pastes on NoPaste. If _opts_ are
14
+ # given they will be used in accessing the NoPaste website. If a
15
+ # _block_ is given, it will be passed the list of recent pastes.
16
+ #
17
+ def self.get(opts={},&block)
18
+ len = opts[:length]
19
+
20
+ if len.nil? || len==0
21
+ len = -1
22
+ else
23
+ len = len.to_i+1
24
+ end
25
+
26
+ page = Hpricot(RPaste.open(URL,opts))
27
+ rows = page.search('//div.filelist/table/tr')[1..len]
28
+
29
+ metadata = rows.map do |row|
30
+ name = row.search('td[4]/a').first.get_attribute('href').gsub(/^.*\//,'').gsub(/\..*$/,'')
31
+ date = row.search('td[1]').inner_text
32
+ syntax = row.search('td[2]').inner_text
33
+ author = row.search('td[3]').inner_text
34
+ description = row.search('td[4]/a').inner_text
35
+
36
+ Metadata.new(name,author,date,syntax,description)
37
+ end
38
+
39
+ new_recent = self.new(metadata)
40
+
41
+ block.call(new_recent) if block
42
+ return new_recent
43
+ end
44
+
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,180 @@
1
+ require 'rpaste/nopaste/metadata'
2
+ require 'rpaste/resultset'
3
+
4
+ module RPaste
5
+ module NoPaste
6
+ class ResultSet < RPaste::ResultSet
7
+
8
+ #
9
+ # Selects the metadata with dates in between _start_ and _stop_.
10
+ #
11
+ def between_dates(start,stop)
12
+ self.select { |data| data.date.between?(start,stop) }
13
+ end
14
+
15
+ #
16
+ # Selects the metadata with the specified _syntax_.
17
+ #
18
+ # results.with_syntax('PHP')
19
+ #
20
+ def with_syntax(syntax)
21
+ self.select { |data| data.syntax == syntax }
22
+ end
23
+
24
+ #
25
+ # Selects the metadata containing _description_.
26
+ #
27
+ # results.with_description('hello world')
28
+ # results.with_description(/(form|reg)/)
29
+ #
30
+ def with_description(description)
31
+ if description.kind_of?(Regexp)
32
+ return self.select { |data| data.description =~ description }
33
+ else
34
+ return self.select { |data| data.description.include?(description) }
35
+ end
36
+ end
37
+
38
+ #
39
+ # A symonym for with_description.
40
+ #
41
+ def search(desc)
42
+ with_description(desc)
43
+ end
44
+
45
+ #
46
+ # Iterates over the metadata with dates falling in between
47
+ # _start_ and _stop_, passing each to the given _block_.
48
+ #
49
+ def each_between_dates(start,stop,&block)
50
+ between_dates(start,stop).each(&block)
51
+ end
52
+
53
+ #
54
+ # Iterates over the metadata with the matching _syntax_, passing each
55
+ # to the given _block_.
56
+ #
57
+ # results.each_with_syntax('ASM') do |metadata|
58
+ # puts metadata.description
59
+ # end
60
+ #
61
+ def each_with_syntax(syntax,&block)
62
+ with_syntax(syntax).each(&block)
63
+ end
64
+
65
+ #
66
+ # Iterates over the metadata with the matching _description_, passing
67
+ # each to the given _block_.
68
+ #
69
+ # results.each_with_description('fizz') do |metadata|
70
+ # puts metadata.author
71
+ # end
72
+ #
73
+ # results.each_with_description(/(fi|bu)zz/) do |metadata|
74
+ # puts metadata.syntax
75
+ # end
76
+ #
77
+ def each_with_description(description,&block)
78
+ with_description(description).each(&block)
79
+ end
80
+
81
+ #
82
+ # Returns the pastes with dates falling in between _start_ and
83
+ # _stop_.
84
+ #
85
+ def pastes_between_dates(start,stop)
86
+ pastes_with { |data| data.date.between?(start,stop) }
87
+ end
88
+
89
+ #
90
+ # Returns the pastes with the matching _syntax_.
91
+ #
92
+ # results.pastes_with_syntax('Javascript')
93
+ #
94
+ def pastes_with_syntax(syntax)
95
+ pastes_with { |data| data.syntax == syntax }
96
+ end
97
+
98
+ #
99
+ # Returns the pastes with the matching _description_.
100
+ #
101
+ # results.pastes_with_description('error')
102
+ # results.pastes_with_description(/\d+(\.\d+)+/)
103
+ #
104
+ def pastes_with_description(description)
105
+ if description.kind_of?(Regexp)
106
+ return pastes_with { |data| data.description =~ description }
107
+ else
108
+ return pastes_with { |data| data.description.include?(description) }
109
+ end
110
+ end
111
+
112
+ #
113
+ # Returns the pastes with the matching _text_.
114
+ #
115
+ # results.pastes_with_text('strcpy')
116
+ # results.pastes_with_text(/printf\([^\",]+,/)
117
+ #
118
+ def pastes_with_text(text)
119
+ if text.kind_of?(Regexp)
120
+ return pastes_with { |data| data.text =~ text }
121
+ else
122
+ return pastes_with { |data| data.text.include?(text) }
123
+ end
124
+ end
125
+
126
+ #
127
+ # Iterates over the pastes with dates falling in between _start_ and
128
+ # _stop_, passing each to the given _block_.
129
+ #
130
+ def each_paste_between_dates(start,stop,&block)
131
+ pastes_between_dates(start,stop).each(&block)
132
+ end
133
+
134
+ #
135
+ # Iterates over the pastes with the matching _syntax_, passing each
136
+ # to the given _block_.
137
+ #
138
+ # results.each_paste_with_syntax('Java') do |paste|
139
+ # puts paste.author
140
+ # end
141
+ #
142
+ def each_paste_with_syntax(syntax,&block)
143
+ pastes_with_syntax(syntax).each(&block)
144
+ end
145
+
146
+ #
147
+ # Itereates over the pastes with the matching _description_, passing
148
+ # each to the given _block_.
149
+ #
150
+ # results.each_paste_with_description('stack') do |paste|
151
+ # puts paste.text
152
+ # end
153
+ #
154
+ # results.each_paste_with_description(/(weird|odd)/) do |paste|
155
+ # puts paste.text
156
+ # end
157
+ #
158
+ def each_paste_with_description(description,&block)
159
+ pastes_with_syntax(description).each(&block)
160
+ end
161
+
162
+ #
163
+ # Iterates over the pastes with the matching _text_, passing each to
164
+ # the given _block_.
165
+ #
166
+ # results.each_paste_with_text('$_GET') do |paste|
167
+ # puts paste.text
168
+ # end
169
+ #
170
+ # results.each_paste_with_text(/goto/) do |paste|
171
+ # puts paste.author
172
+ # end
173
+ #
174
+ def each_paste_with_text(text,&block)
175
+ pastes_with_text(text).each(&block)
176
+ end
177
+
178
+ end
179
+ end
180
+ end
@@ -0,0 +1,4 @@
1
+ require 'rpaste/nopaste/metadata'
2
+ require 'rpaste/nopaste/paste'
3
+ require 'rpaste/nopaste/recent'
4
+ require 'rpaste/nopaste/nopaste'
@@ -0,0 +1,49 @@
1
+ module RPaste
2
+ class Paste
3
+
4
+ # Name of the entry
5
+ attr_accessor :name
6
+
7
+ # Author of the entry
8
+ attr_accessor :author
9
+
10
+ # Date of the entry
11
+ attr_accessor :date
12
+
13
+ # Syntax of the entry text
14
+ attr_accessor :syntax
15
+
16
+ # Text of the entry
17
+ attr_accessor :text
18
+
19
+ #
20
+ # Creates a new Paste object with the optional _name_, _author_, _date_,
21
+ # _syntax_ and _text_. If a block is given, then it will be passed the
22
+ # newly created Paste object.
23
+ #
24
+ def initialize(name=nil,author=nil,date=nil,syntax=nil,text=nil,&block)
25
+ @name = name
26
+ @author = author
27
+ @date = Time.parse(date) if date
28
+ @syntax = syntax
29
+ @text = text
30
+
31
+ block.call(self) if block
32
+ end
33
+
34
+ #
35
+ # Returns a string containing the text of the paste.
36
+ #
37
+ def to_s
38
+ @text.to_s
39
+ end
40
+
41
+ #
42
+ # Returns an array containing the text lines of the paste.
43
+ #
44
+ def to_a
45
+ self.to_s.split
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,23 @@
1
+ require 'rpaste/metadata'
2
+
3
+ module RPaste
4
+ module PasteBin
5
+ class Metadata < RPaste::Metadata
6
+
7
+ # Time since being posted
8
+ attr_reader :age
9
+
10
+ #
11
+ # Creates a new PasteBin Metadata object with the specified _name_,
12
+ # _author_ and _age_. If a block is given, then it will be passed
13
+ # the newly created PasteBin Metadata object.
14
+ #
15
+ def initialize(name,author,age,&block)
16
+ @age = age
17
+
18
+ super(name,author,&block)
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,47 @@
1
+ require 'rpaste/paste'
2
+ require 'rpaste/rpaste'
3
+
4
+ module RPaste
5
+ module PasteBin
6
+ class Paste < RPaste::Paste
7
+
8
+ PREFIX_URL = 'http://pastebin.com/'
9
+
10
+ # Time to retain
11
+ attr_reader :retained
12
+
13
+ #
14
+ # Creates a new PasteBin Paste object with the given _name_, _author_,
15
+ # _date_, _syntax_, _retained_ and _text_. If a block is given, then
16
+ # it will be passed the newly created PasteBin Paste object.
17
+ #
18
+ def initialize(name=nil,author=nil,date=nil,syntax=nil,retained=nil,text=nil,&block)
19
+ @retained = retained
20
+
21
+ super(name,author,date,syntax,text,&block)
22
+ end
23
+
24
+ #
25
+ # Returns the URL of the PasteBin Paste object.
26
+ #
27
+ def url
28
+ "#{PREFIX_URL}#{@name}"
29
+ end
30
+
31
+ #
32
+ # Retrieves the PasteBin Paste object associated with the specified
33
+ # _name_. If _opts_ is given they will be used in accessing the
34
+ # PasteBin website.
35
+ #
36
+ def self.paste(name,opts={})
37
+ page = Hpricot(RPaste.open("#{PREFIX_URL}#{name}",opts))
38
+
39
+ return Paste.new(name) do |paste|
40
+ paste.text = page.search('//div.text/ol/li').inner_text
41
+ paste.author = page.search('#content/h1[1]'.inner_text.gsub(/Posted by /,'').split(' on ')
42
+ end
43
+ end
44
+
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,55 @@
1
+ require 'rpaste/pastebin/recent'
2
+ require 'rpaste/pastebin/paste'
3
+ require 'rpaste/rpaste'
4
+
5
+ require 'hpricot'
6
+
7
+ module RPaste
8
+ module PasteBin
9
+ #
10
+ # Returns the list of all recent pastes on PasteBin. See Recent.get.
11
+ #
12
+ def PasteBin.recent(opts={},&block)
13
+ Recent.get(opts,&block)
14
+ end
15
+
16
+ #
17
+ # Submits a new paste to PasteBin. If a _block_ is given, then it will be
18
+ # passed the Paste object before it is submitted to PasteBin.
19
+ #
20
+ # PasteBin.post(:author => 'xyz') do |paste|
21
+ # paste.description = 'test'
22
+ # paste.text = %{
23
+ # <?xml version="1.0" ?>
24
+ # <test>
25
+ # <x>1</x>
26
+ # <y>0</y>
27
+ # </test>
28
+ # }
29
+ # end
30
+ #
31
+ def PasteBin.post(opts={},&block)
32
+ paste = Paste.new(opts,&block)
33
+
34
+ agent = RPaste.http_agent(opts)
35
+ page = agent.get('http://pastebin.com/pastebin.php')
36
+ form = page.forms.first
37
+
38
+ form.format = paste.syntax if paste.syntax
39
+ form.code2 = paste.text
40
+ form.poster = paste.author if paste.author
41
+
42
+ case paste.retained
43
+ when :day then
44
+ form.expire = 'd'
45
+ when :month
46
+ form.expire = 'm'
47
+ when :forever
48
+ form.expire = 'f'
49
+ end
50
+
51
+ agent.submit(form)
52
+ return true
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,47 @@
1
+ require 'rpaste/pastebin/result_set'
2
+ require 'rpaste/rpaste'
3
+
4
+ require 'hpricot'
5
+
6
+ module RPaste
7
+ module PasteBin
8
+ class Recent < ResultSet
9
+
10
+ URL = 'http://pastebin.com/'
11
+
12
+ MAX_LENGTH = 10
13
+
14
+ #
15
+ # Returns the list of recent pastes on PasteBin. If _opts_ are
16
+ # given they will be used in accessing the PasteBin website. If a
17
+ # _block_ is given, it will be passed the list of recent pastes.
18
+ #
19
+ def self.get(opts={},&block)
20
+ len = opts[:length]
21
+
22
+ if (len.nil? || len>MAX_LENGTH)
23
+ len = MAX_LENGTH
24
+ else
25
+ len = len.to_i
26
+ end
27
+
28
+ page = Hpricot(RPaste.open(URL,opts))
29
+ rows = page.search('#menu/ul/li')[0...len]
30
+
31
+ metadata = rows.map do |row|
32
+ name = row.search('a').first.get_attribute('href').gsub(/^.*\//,'')
33
+ author = row.search('a').inner_text
34
+ ago = row.inner_text
35
+
36
+ Metadata.new(name,author,ago)
37
+ end
38
+
39
+ new_recent = self.new(metadata)
40
+
41
+ block.call(new_recent) if block
42
+ return new_recent
43
+ end
44
+
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,40 @@
1
+ require 'rpaste/pastebin/metadata'
2
+ require 'rpaste/result_set'
3
+
4
+ module RPaste
5
+ module NoPaste
6
+ class ResultSet < RPaste::ResultSet
7
+
8
+ #
9
+ # Selects the metadata with ages between _start_ and _stop_.
10
+ #
11
+ def between_ages(start,stop)
12
+ self.select { |data| data.ago.between?(start,stop) }
13
+ end
14
+
15
+ #
16
+ # Iterates over the metadata with ages between _start_ and _stop_,
17
+ # passing each to the given _block_.
18
+ #
19
+ def each_between_ages(start,stop,&block)
20
+ between_ages(start,stop).each(&block)
21
+ end
22
+
23
+ #
24
+ # Returns the pastes with ages between _start_ and _stop_.
25
+ #
26
+ def pastes_between_ages(start,stop)
27
+ pastes_with { |data| data.age.between?(start,stop) }
28
+ end
29
+
30
+ #
31
+ # Iterates over the pastes with ages between _start_ and _stop_,
32
+ # passing each to the given _block_.
33
+ #
34
+ def each_paste_between_ages(start,stop,&block)
35
+ pastes_between_ages(start,stop).each(&block)
36
+ end
37
+
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ require 'rpaste/pastebin/metadata'
2
+ require 'rpaste/pastebin/paste'
3
+ require 'rpaste/pastebin/pastebin'
@@ -0,0 +1,253 @@
1
+ module RPaste
2
+ class ResultSet < Array
3
+
4
+ #
5
+ # Creates a new ResultSet object with the given _results_.
6
+ #
7
+ def initialize(results=[],&block)
8
+ @pastes = {}
9
+
10
+ super(results,&block)
11
+ end
12
+
13
+ #
14
+ # Selects metadata with the matching _name_.
15
+ #
16
+ # results.with_name('secret')
17
+ # results.with_name(/login/)
18
+ #
19
+ def with_name(name)
20
+ if name.kind_of?(Regexp)
21
+ return self.select { |data| data.name =~ name }
22
+ else
23
+ return self.select { |data| data.name == name }
24
+ end
25
+ end
26
+
27
+ #
28
+ # Selects metadata with the matching _author_.
29
+ #
30
+ # results.by_author('dj food')
31
+ # results.by_author(/^ro/)
32
+ #
33
+ def by_author(author)
34
+ if author.kind_of?(Regexp)
35
+ return self.select { |data| data.author =~ author }
36
+ else
37
+ return self.select { |data| data.author == author }
38
+ end
39
+ end
40
+
41
+ #
42
+ # Iterates over the metadata with the matching name, passing each
43
+ # to the given _block_.
44
+ #
45
+ # results.each_with_name('secret')
46
+ # results.each_with_name(/reg/)
47
+ #
48
+ def each_with_name(name,&block)
49
+ with_name(name).each(&block)
50
+ end
51
+
52
+ #
53
+ # Iterates over the metadata with the matching author name, passing
54
+ # each to the given _block_.
55
+ #
56
+ # results.each_by_author('aphex twin')
57
+ # results.each_by_author(/square/)
58
+ #
59
+ def each_by_author(author,&block)
60
+ by_author(author).each(&block)
61
+ end
62
+
63
+ #
64
+ # Returns the paste at the given _index_. If _index_ is a Range, then
65
+ # all Pastes within the range are returned.
66
+ #
67
+ # results.paste(10)
68
+ # results.paste(1..4)
69
+ #
70
+ def paste(index)
71
+ if index.kind_of?(Range)
72
+ return self[index].map { |data| cache_paste(data) }
73
+ else
74
+ return cache_paste(self[index])
75
+ end
76
+ end
77
+
78
+ #
79
+ # Returns all pastes.
80
+ #
81
+ def pastes
82
+ self.map { |data| cache_paste(data) }
83
+ end
84
+
85
+ #
86
+ # Iterates over all pastes, passing each to the given _block_.
87
+ #
88
+ # results.each_paste do |paste|
89
+ # if paste.syntax == 'ASM'
90
+ # puts paste
91
+ # end
92
+ # end
93
+ #
94
+ def each_paste(&block)
95
+ pastes.each(&block)
96
+ end
97
+
98
+ #
99
+ # Returns the pastes which match the given _block_.
100
+ #
101
+ def pastes_with(&block)
102
+ self.select(&block).map { |data| cache_paste(data) }
103
+ end
104
+
105
+ #
106
+ # Returns the pastes which have the matching _name_.
107
+ #
108
+ # results.pastes_with_name('config')
109
+ # results.pastes_with_name(/rock/)
110
+ #
111
+ def pastes_with_name(name)
112
+ if name.kind_of?(Regexp)
113
+ return pastes_with { |data| data.name =~ name }
114
+ else
115
+ return pastes_with { |data| data.name == name }
116
+ end
117
+ end
118
+
119
+ #
120
+ # Returns the pastes which have the matching _author_.
121
+ #
122
+ # results.pastes_by_author('adam freeland')
123
+ # results.pastes_by_author(/hendrix/)
124
+ #
125
+ def pastes_by_author(author)
126
+ if author.kind_of?(Regexp)
127
+ return pastes_with { |data| data.author =~ author }
128
+ else
129
+ return pastes_with { |data| data.author == author }
130
+ end
131
+ end
132
+
133
+ #
134
+ # Returns the pastes which have the matching _date_.
135
+ #
136
+ # results.pastes_on_date(Time.now)
137
+ #
138
+ def pastes_on_date(date)
139
+ pastes_with { |data| data.date == date }
140
+ end
141
+
142
+ #
143
+ # Returns the pastes which have the matching _syntax_.
144
+ #
145
+ # results.pastes_with_syntax('C')
146
+ #
147
+ def pastes_with_syntax(syntax)
148
+ pastes_with { |data| data.syntax == syntax }
149
+ end
150
+
151
+ #
152
+ # Returns the pastes which contain the specified _text_.
153
+ #
154
+ # results.pastes_with_text('ls')
155
+ #
156
+ def pastes_with_text(text)
157
+ if text.kind_of?(Regexp)
158
+ return pastes_with { |data| data.text =~ text }
159
+ else
160
+ return pastes_with { |data| data.text == text }
161
+ end
162
+ end
163
+
164
+ #
165
+ # Iterates over the pastes with the matching _name_, passing each
166
+ # to the given _block_.
167
+ #
168
+ # results.each_paste_with_name('code') do |paste|
169
+ # puts paste
170
+ # end
171
+ #
172
+ def each_paste_with_name(name,&block)
173
+ pastes_with_name(name).each(&block)
174
+ end
175
+
176
+ #
177
+ # Iterates over the pastes with the matching _author_, passing each
178
+ # to the given _block_.
179
+ #
180
+ # results.each_paste_with_name('King Tubby') do |paste|
181
+ # puts paste
182
+ # end
183
+ #
184
+ def each_paste_by_author(author,&block)
185
+ pastes_by_author(author).each(&block)
186
+ end
187
+
188
+ #
189
+ # Iterates over the pastes with the matching _date_, passing each
190
+ # to the given _block_.
191
+ #
192
+ # results.each_paste_on_date(Time.now) do |paste|
193
+ # puts paste
194
+ # end
195
+ #
196
+ def each_paste_on_date(date,&block)
197
+ pastes_on_date(date).each(&block)
198
+ end
199
+
200
+ #
201
+ # Iterates over the pastes with the matching _syntax_, passing each
202
+ # to the given _block_.
203
+ #
204
+ # results.each_paste_with_syntax('Ruby') do |paste|
205
+ # puts paste
206
+ # end
207
+ #
208
+ def each_paste_with_syntax(syntax,&block)
209
+ pastes_with_syntax(syntax).each(&block)
210
+ end
211
+
212
+ #
213
+ # Iterates over the pastes containing the specified _text_, passing
214
+ # each to the given _block_.
215
+ #
216
+ # results.each_paste_with_text('strcpy') do |paste|
217
+ # puts paste
218
+ # end
219
+ #
220
+ # results.each_paste_with_text(/strcpy\(\w+,\w+\)/) do |paste|
221
+ # puts paste
222
+ # end
223
+ #
224
+ def each_paste_with_text(text,&block)
225
+ pastes_with_text(text).each(&block)
226
+ end
227
+
228
+ #
229
+ # Clears the results.
230
+ #
231
+ def clear
232
+ clear_pastes
233
+ return super
234
+ end
235
+
236
+ protected
237
+
238
+ #
239
+ # Retrieves the Paste from the _metadata_ and caches it.
240
+ #
241
+ def cache_paste(metadata)
242
+ @pastes[metadata.name] ||= metadata.paste
243
+ end
244
+
245
+ #
246
+ # Clears all cached pastes.
247
+ #
248
+ def clear_pastes
249
+ @pastes.clear
250
+ end
251
+
252
+ end
253
+ end
@@ -0,0 +1,62 @@
1
+ require 'mechanize'
2
+ require 'open-uri'
3
+
4
+ module RPaste
5
+ #
6
+ # Returns the RPaste user-agent
7
+ #
8
+ def RPaste.user_agent
9
+ @user_agent
10
+ end
11
+
12
+ #
13
+ # Sets the RPaste user-agent to the specified _agent_.
14
+ #
15
+ def RPaste.user_agent=(agent)
16
+ @user_agent = agent
17
+ end
18
+
19
+ #
20
+ # Opens the _uri_ with the given _opts_. The contents of the _uri_ will
21
+ # be returned.
22
+ #
23
+ # RPaste.open('http://www.hackety.org/')
24
+ # RPaste.open('http://tenderlovemaking.com/',
25
+ # :user_agent_alias => 'Linux Mozilla')
26
+ # RPaste.open('http://www.wired.com/', :user_agent => 'the future')
27
+ #
28
+ def RPaste.open(uri,opts={})
29
+ headers = {}
30
+
31
+ if opts[:user_agent_alias]
32
+ headers['User-Agent'] = WWW::Mechanize::AGENT_ALIASES[opts[:user_agent_alias]]
33
+ elsif opts[:user_agent]
34
+ headers['User-Agent'] = opts[:user_agent]
35
+ elsif RPaste.user_agent
36
+ headers['User-Agent'] = RPaste.user_agent
37
+ end
38
+
39
+ return Kernel.open(uri,headers)
40
+ end
41
+
42
+ #
43
+ # Creates a new Mechanize agent with the given _opts_.
44
+ #
45
+ # RPaste.http_agent
46
+ # RPaste.http_agent(:user_agent_alias => 'Linux Mozilla')
47
+ # RPaste.http_agent(:user_agent => 'wooden pants')
48
+ #
49
+ def RPaste.http_agent(opts={})
50
+ agent = WWW::Mechanize.new
51
+
52
+ if opts[:user_agent_alias]
53
+ agent.user_agent_alias = opts[:user_agent_alias]
54
+ elsif opts[:user_agent]
55
+ agent.user_agent = opts[:user_agent]
56
+ elsif RPaste.user_agent
57
+ agent.user_agent = RPaste.user_agent
58
+ end
59
+
60
+ return agent
61
+ end
62
+ end
@@ -0,0 +1,3 @@
1
+ module RPaste
2
+ VERSION = '0.0.9'
3
+ end
data/lib/rpaste.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'rpaste/nopaste'
2
+ require 'rpaste/pastebin'
File without changes
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
4
+ name: rpaste
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.0.9
7
+ date: 2008-01-09 00:00:00 -08:00
8
+ summary: RPaste provides access to many of the online paste services
9
+ require_paths:
10
+ - lib
11
+ email: postmodern.mod3@gmail.com
12
+ homepage: " by Postmodern Modulus III"
13
+ rubyforge_project: rpaste
14
+ description: "== FEATURES/PROBLEMS: * Supports posting and retrieving from NoPaste. * Supports retrieving from PasteBin. * Provides HTTP access with custom User-Agent strings. == REQUIREMENTS: * Hpricot * Mechanize == INSTALL:"
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Postmodern Modulus III
31
+ files:
32
+ - History.txt
33
+ - LICENSE.txt
34
+ - Manifest.txt
35
+ - README.txt
36
+ - Rakefile
37
+ - lib/rpaste.rb
38
+ - lib/rpaste/version.rb
39
+ - lib/rpaste/rpaste.rb
40
+ - lib/rpaste/metadata.rb
41
+ - lib/rpaste/paste.rb
42
+ - lib/rpaste/result_set.rb
43
+ - lib/rpaste/nopaste/metadata.rb
44
+ - lib/rpaste/nopaste/paste.rb
45
+ - lib/rpaste/nopaste/result_set.rb
46
+ - lib/rpaste/nopaste/recent.rb
47
+ - lib/rpaste/nopaste/nopaste.rb
48
+ - lib/rpaste/nopaste.rb
49
+ - lib/rpaste/pastebin/metadata.rb
50
+ - lib/rpaste/pastebin/paste.rb
51
+ - lib/rpaste/pastebin/result_set.rb
52
+ - lib/rpaste/pastebin/recent.rb
53
+ - lib/rpaste/pastebin/pastebin.rb
54
+ - lib/rpaste/pastebin.rb
55
+ - test/test_rpaste.rb
56
+ test_files:
57
+ - test/test_rpaste.rb
58
+ rdoc_options:
59
+ - --main
60
+ - README.txt
61
+ extra_rdoc_files:
62
+ - History.txt
63
+ - LICENSE.txt
64
+ - Manifest.txt
65
+ - README.txt
66
+ executables: []
67
+
68
+ extensions: []
69
+
70
+ requirements: []
71
+
72
+ dependencies:
73
+ - !ruby/object:Gem::Dependency
74
+ name: hpricot
75
+ version_requirement:
76
+ version_requirements: !ruby/object:Gem::Version::Requirement
77
+ requirements:
78
+ - - ">"
79
+ - !ruby/object:Gem::Version
80
+ version: 0.0.0
81
+ version:
82
+ - !ruby/object:Gem::Dependency
83
+ name: mechanize
84
+ version_requirement:
85
+ version_requirements: !ruby/object:Gem::Version::Requirement
86
+ requirements:
87
+ - - ">"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.0.0
90
+ version:
91
+ - !ruby/object:Gem::Dependency
92
+ name: hoe
93
+ version_requirement:
94
+ version_requirements: !ruby/object:Gem::Version::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: 1.4.0
99
+ version: