pastis 0.0.1 → 0.1.0
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/README +9 -0
- data/lib/pastis.rb +28 -124
- data/lib/pastis/collection.rb +69 -0
- data/lib/pastis/paste.rb +39 -0
- metadata +6 -2
data/README
ADDED
data/lib/pastis.rb
CHANGED
|
@@ -1,25 +1,18 @@
|
|
|
1
1
|
require 'net/http'
|
|
2
|
-
require 'open-uri'
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
require 'rubygems'
|
|
7
|
-
require 'hpricot'
|
|
8
|
-
Kernel.send(:Hpricot, object)
|
|
9
|
-
end
|
|
3
|
+
require File.expand_path(File.dirname(__FILE__)) + "/pastis/paste"
|
|
4
|
+
require File.expand_path(File.dirname(__FILE__)) + "/pastis/collection"
|
|
10
5
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
require '
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const_missing_without_pastis(const)
|
|
22
|
-
end
|
|
6
|
+
unless defined? Hpricot
|
|
7
|
+
def Hpricot(*args) #:nodoc:
|
|
8
|
+
# load hpricot on demand
|
|
9
|
+
begin
|
|
10
|
+
require 'hpricot'
|
|
11
|
+
rescue LoadError
|
|
12
|
+
require 'rubygems'
|
|
13
|
+
gem 'hpricot'
|
|
14
|
+
end
|
|
15
|
+
Kernel.send(:Hpricot, *args)
|
|
23
16
|
end
|
|
24
17
|
end
|
|
25
18
|
|
|
@@ -44,112 +37,13 @@ end
|
|
|
44
37
|
# pages.next_page.last.url
|
|
45
38
|
# # => "http://pastie.caboo.se/pastes/43577"
|
|
46
39
|
module Pastis
|
|
47
|
-
Version = [ 0,
|
|
40
|
+
Version = [ 0, 1, 0 ].freeze unless defined?(Version)
|
|
48
41
|
|
|
49
42
|
class Error < RuntimeError; end
|
|
50
43
|
class PasteNotCreatedError < Error; end
|
|
51
44
|
class EmptyPasteError < Error; end
|
|
52
45
|
class PaginationError < Error; end
|
|
53
46
|
|
|
54
|
-
# Represents a paste, shoudn't be created by hand
|
|
55
|
-
class Paste
|
|
56
|
-
def initialize(attributes)
|
|
57
|
-
%w(url body time preview).each do |attribute|
|
|
58
|
-
instance_variable_set("@#{attribute}", attributes[attribute.intern])
|
|
59
|
-
end
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
attr_reader :url
|
|
63
|
-
|
|
64
|
-
# Url of the raw paste
|
|
65
|
-
def raw_url
|
|
66
|
-
"#{url}.txt"
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
# 5 first lines of the paste
|
|
70
|
-
def preview
|
|
71
|
-
@preview ||= body.to_a[0, 6].to_s
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
# Creation time
|
|
75
|
-
def time
|
|
76
|
-
@time ||= Time.parse(Hpricot(get(url)).at("#paste_date").innerText)
|
|
77
|
-
end
|
|
78
|
-
|
|
79
|
-
# Body of the paste
|
|
80
|
-
def body
|
|
81
|
-
@body ||= get(raw_url)
|
|
82
|
-
end
|
|
83
|
-
alias_method :to_s, :body
|
|
84
|
-
|
|
85
|
-
private
|
|
86
|
-
|
|
87
|
-
def get(url)
|
|
88
|
-
Pastis.send(:get, url)
|
|
89
|
-
end
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
# Represents a collection of pastes paginated
|
|
93
|
-
class Collection < Array
|
|
94
|
-
def initialize(page, &block)
|
|
95
|
-
@page, @block = page, block
|
|
96
|
-
|
|
97
|
-
parse_url(yield(page))
|
|
98
|
-
super(@pastes)
|
|
99
|
-
end
|
|
100
|
-
|
|
101
|
-
attr_reader :page, :pages_count
|
|
102
|
-
|
|
103
|
-
def next_page
|
|
104
|
-
raise PaginationError, "last page" if last_page?
|
|
105
|
-
move(page + 1)
|
|
106
|
-
end
|
|
107
|
-
alias_method :next, :next_page
|
|
108
|
-
|
|
109
|
-
def previous_page
|
|
110
|
-
raise PaginationError, "first page" if first_page?
|
|
111
|
-
move(page - 1)
|
|
112
|
-
end
|
|
113
|
-
alias_method :previous, :previous_page
|
|
114
|
-
|
|
115
|
-
def goto_page(page)
|
|
116
|
-
raise PaginationError, "page out of range" unless (1..pages_count).member?(page)
|
|
117
|
-
move(page)
|
|
118
|
-
end
|
|
119
|
-
alias_method :goto, :goto_page
|
|
120
|
-
|
|
121
|
-
def last_page?
|
|
122
|
-
page == pages_count
|
|
123
|
-
end
|
|
124
|
-
|
|
125
|
-
def first_page?
|
|
126
|
-
page == 1
|
|
127
|
-
end
|
|
128
|
-
|
|
129
|
-
private
|
|
130
|
-
|
|
131
|
-
def get(url)
|
|
132
|
-
Pastis.send(:get, url)
|
|
133
|
-
end
|
|
134
|
-
|
|
135
|
-
def move(page)
|
|
136
|
-
Collection.new(page, &@block)
|
|
137
|
-
end
|
|
138
|
-
|
|
139
|
-
def parse_url(url)
|
|
140
|
-
doc = Hpricot(get(url))
|
|
141
|
-
|
|
142
|
-
@pastes = (doc/'.pastePreview').collect do |node|
|
|
143
|
-
Paste.new :time => Time.parse(node.at('.when span').innerText),
|
|
144
|
-
:url => node.at('.link a').attributes['href'],
|
|
145
|
-
:preview => node.at('.code').innerText
|
|
146
|
-
end
|
|
147
|
-
|
|
148
|
-
@pages_count =
|
|
149
|
-
Hpricot((doc/'.pages strong').first.innerText.split(' ').last).innerText.to_i
|
|
150
|
-
end
|
|
151
|
-
end
|
|
152
|
-
|
|
153
47
|
class << self
|
|
154
48
|
def url #:nodoc:
|
|
155
49
|
"http://pastie.caboo.se"
|
|
@@ -200,7 +94,7 @@ module Pastis
|
|
|
200
94
|
end
|
|
201
95
|
|
|
202
96
|
def get(url)
|
|
203
|
-
|
|
97
|
+
Net::HTTP.get(URI.parse(url))
|
|
204
98
|
end
|
|
205
99
|
|
|
206
100
|
def post(url, parameters = {})
|
|
@@ -212,11 +106,21 @@ module Pastis
|
|
|
212
106
|
end
|
|
213
107
|
|
|
214
108
|
def search_url(query, page)
|
|
215
|
-
"#{url}/search?q=#{
|
|
109
|
+
"#{url}/search?q=#{cgi.escape(query.to_s)}&page=#{page}"
|
|
216
110
|
end
|
|
217
111
|
|
|
218
112
|
def all_url(page)
|
|
219
|
-
"#{url}/
|
|
113
|
+
"#{url}/pastes/page/#{page}"
|
|
220
114
|
end
|
|
221
|
-
|
|
222
|
-
|
|
115
|
+
|
|
116
|
+
def cgi
|
|
117
|
+
# load CGI on demand
|
|
118
|
+
if const_defined?(:CGI)
|
|
119
|
+
CGI
|
|
120
|
+
else
|
|
121
|
+
require 'cgi'
|
|
122
|
+
CGI
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
module Pastis
|
|
2
|
+
# Represents a collection of pastes paginated
|
|
3
|
+
class Collection < Array
|
|
4
|
+
def initialize(page, &block)
|
|
5
|
+
@page, @block = page, block
|
|
6
|
+
|
|
7
|
+
parse_url(yield(page))
|
|
8
|
+
super(@pastes)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
attr_reader :page, :pages_count
|
|
12
|
+
|
|
13
|
+
def next_page
|
|
14
|
+
raise PaginationError, "last page" if last_page?
|
|
15
|
+
move(page + 1)
|
|
16
|
+
end
|
|
17
|
+
alias_method :next, :next_page
|
|
18
|
+
|
|
19
|
+
def previous_page
|
|
20
|
+
raise PaginationError, "first page" if first_page?
|
|
21
|
+
move(page - 1)
|
|
22
|
+
end
|
|
23
|
+
alias_method :previous, :previous_page
|
|
24
|
+
|
|
25
|
+
def first_page
|
|
26
|
+
move(1)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def last_page
|
|
30
|
+
move(pages_count)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def goto_page(page)
|
|
34
|
+
raise PaginationError, "page out of range" unless page.between?(1, pages_count)
|
|
35
|
+
move(page)
|
|
36
|
+
end
|
|
37
|
+
alias_method :goto, :goto_page
|
|
38
|
+
|
|
39
|
+
def last_page?
|
|
40
|
+
page == pages_count
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def first_page?
|
|
44
|
+
page == 1
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
private
|
|
48
|
+
|
|
49
|
+
def get(url)
|
|
50
|
+
Pastis.send(:get, url)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def move(page)
|
|
54
|
+
Collection.new(page, &@block)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def parse_url(url)
|
|
58
|
+
doc = Hpricot(get(url))
|
|
59
|
+
|
|
60
|
+
@pastes = (doc/'.pastePreview').collect do |node|
|
|
61
|
+
Paste.new :time => Time.parse(node.at('.when span').innerText),
|
|
62
|
+
:url => node.at('.link a').attributes['href'],
|
|
63
|
+
:preview => node.at('.code').innerText
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
@pages_count = (doc/'.pages strong').first.innerText.split(' ').last.to_i rescue 0
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
data/lib/pastis/paste.rb
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
module Pastis
|
|
2
|
+
# Represents a paste, shoudn't be created by hand
|
|
3
|
+
class Paste
|
|
4
|
+
def initialize(attributes)
|
|
5
|
+
%w(url body time preview).each do |attribute|
|
|
6
|
+
instance_variable_set("@#{attribute}", attributes[attribute.intern])
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
attr_reader :url
|
|
11
|
+
|
|
12
|
+
# Url of the raw paste
|
|
13
|
+
def raw_url
|
|
14
|
+
"#{url}.txt"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# 5 first lines of the paste
|
|
18
|
+
def preview
|
|
19
|
+
@preview ||= body.split("\n")[0, 6].join("\n")
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Creation time
|
|
23
|
+
def time
|
|
24
|
+
@time ||= Time.parse(Hpricot(get(url)).at("#paste_date").innerText)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Body of the paste
|
|
28
|
+
def body
|
|
29
|
+
@body ||= get(raw_url)
|
|
30
|
+
end
|
|
31
|
+
alias_method :to_s, :body
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def get(url)
|
|
36
|
+
Pastis.send(:get, url)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
metadata
CHANGED
|
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
|
|
|
3
3
|
specification_version: 1
|
|
4
4
|
name: pastis
|
|
5
5
|
version: !ruby/object:Gem::Version
|
|
6
|
-
version: 0.0
|
|
7
|
-
date:
|
|
6
|
+
version: 0.1.0
|
|
7
|
+
date: 2008-03-13 00:00:00 +01:00
|
|
8
8
|
summary: A simple interface to Pastie
|
|
9
9
|
require_paths:
|
|
10
10
|
- lib
|
|
@@ -30,7 +30,11 @@ authors:
|
|
|
30
30
|
- Samuel Lebeau
|
|
31
31
|
files:
|
|
32
32
|
- LICENSE
|
|
33
|
+
- README
|
|
33
34
|
- bin/pastis
|
|
35
|
+
- lib/pastis
|
|
36
|
+
- lib/pastis/collection.rb
|
|
37
|
+
- lib/pastis/paste.rb
|
|
34
38
|
- lib/pastis.rb
|
|
35
39
|
test_files: []
|
|
36
40
|
|