shorturl 0.8.0 → 0.8.1

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/ChangeLog CHANGED
@@ -1,3 +1,6 @@
1
+ 0.8.1:
2
+ - Wrapped the library in a WWW namespace
3
+
1
4
  0.8.0:
2
5
  - Made some improvements to bin/shorturl
3
6
 
data/README CHANGED
@@ -1,4 +1,4 @@
1
- = ShortURL 0.8.0
1
+ = ShortURL 0.8.1
2
2
 
3
3
  == Summary
4
4
  ShortURL is a very simple library to use URL shortening services such as
@@ -31,13 +31,14 @@ Here is the list of the services supported by ShortURL:
31
31
  * http://clipurl.com
32
32
  * http://shortify.com
33
33
  * http://0rz.net
34
+ * http://skinnylink.com
34
35
 
35
36
  == Usage:
36
37
 
37
38
  call-seq:
38
39
  require "shorturl"
39
- puts ShortURL.shorten("http://mypage.com")
40
- puts ShortURL.shorten("http://mypage.com", :tinyurl)
40
+ puts WWW::ShortURL.shorten("http://mypage.com")
41
+ puts WWW::ShortURL.shorten("http://mypage.com", :tinyurl)
41
42
 
42
43
  The second parameter represents the service you want to use. These are:
43
44
  * <tt>:rubyurl</tt>
@@ -58,6 +59,7 @@ The second parameter represents the service you want to use. These are:
58
59
  * <tt>:clipurl</tt>
59
60
  * <tt>:shortify</tt>
60
61
  * <tt>:orz</tt>
62
+ * <tt>:skinnylink</tt>
61
63
 
62
64
 
63
65
  You can use <tt>ShortURL.valid_services</tt> to obtain a
data/bin/shorturl CHANGED
@@ -4,6 +4,8 @@ require "rubygems"
4
4
  require "shorturl"
5
5
  require "optparse"
6
6
 
7
+ include WWW
8
+
7
9
  def service_list
8
10
  ShortURL.valid_services.map { |s| s.to_s }.sort.join("\n\t")
9
11
  end
data/lib/shorturl.rb CHANGED
@@ -7,202 +7,204 @@ require "net/http"
7
7
  require "cgi"
8
8
  require "uri"
9
9
 
10
- class InvalidService < Exception
11
- end
10
+ module WWW
11
+ class InvalidService < Exception
12
+ end
12
13
 
13
- class Service
14
- attr_accessor :port, :code, :method, :action, :field, :block
15
-
16
- # Intialize the service with a hostname (required parameter) and you
17
- # can override the default values for the HTTP port, expected HTTP
18
- # return code, the form method to use, the form action, the form
19
- # field which contains the long URL, and the block of what to do
20
- # with the HTML code you get.
21
- def initialize(hostname) # :yield: service
22
- @hostname = hostname
23
- @port = 80
24
- @code = 200
25
- @method = :post
26
- @action = "/"
27
- @field = "url"
28
- @block = lambda { |body| }
29
-
30
- if block_given?
31
- yield self
14
+ class Service
15
+ attr_accessor :port, :code, :method, :action, :field, :block
16
+
17
+ # Intialize the service with a hostname (required parameter) and you
18
+ # can override the default values for the HTTP port, expected HTTP
19
+ # return code, the form method to use, the form action, the form
20
+ # field which contains the long URL, and the block of what to do
21
+ # with the HTML code you get.
22
+ def initialize(hostname) # :yield: service
23
+ @hostname = hostname
24
+ @port = 80
25
+ @code = 200
26
+ @method = :post
27
+ @action = "/"
28
+ @field = "url"
29
+ @block = lambda { |body| }
30
+
31
+ if block_given?
32
+ yield self
33
+ end
32
34
  end
33
- end
34
35
 
35
- # Now that our service is set up, call it with all the parameters to
36
- # (hopefully) return only the shortened URL.
37
- def call(url)
38
- Net::HTTP.start(@hostname, @port) { |http|
39
- response = case @method
40
- when :post: http.post(@action, "#{@field}=#{url}")
41
- when :get: http.get("#{@action}?#{@field}=#{CGI.escape(url)}")
42
- end
43
- if response.code == @code.to_s
44
- begin
45
- @block.call(response.read_body)
46
- rescue NoMethodError
47
- nil
36
+ # Now that our service is set up, call it with all the parameters to
37
+ # (hopefully) return only the shortened URL.
38
+ def call(url)
39
+ Net::HTTP.start(@hostname, @port) { |http|
40
+ response = case @method
41
+ when :post: http.post(@action, "#{@field}=#{url}")
42
+ when :get: http.get("#{@action}?#{@field}=#{CGI.escape(url)}")
43
+ end
44
+ if response.code == @code.to_s
45
+ begin
46
+ @block.call(response.read_body)
47
+ rescue NoMethodError
48
+ nil
49
+ end
48
50
  end
49
- end
50
- }
51
+ }
52
+ end
51
53
  end
52
- end
53
54
 
54
- class ShortURL
55
- # Hash table of all the supported services. The key is a symbol
56
- # representing the service (usually the hostname minus the .com,
57
- # .net, etc.) The value is an instance of Service with all the
58
- # parameters set so that when +instance+.call is invoked, the
59
- # shortened URL is returned.
60
- @@services = {
61
- :rubyurl => Service.new("rubyurl.com") { |s|
62
- s.code = 302
63
- s.method = :get
64
- s.action = "/rubyurl/create"
65
- s.field = "rubyurl[website_url]"
66
- s.block = lambda { |body| URI.extract(body)[0].gsub("rubyurl/show/", "") }
67
- },
68
-
69
- :tinyurl => Service.new("tinyurl.com") { |s|
70
- s.action = "/create.php"
71
- s.block = lambda { |body| URI.extract(body).grep(/tinyurl/)[-1] }
72
- },
73
-
74
- :shorl => Service.new("shorl.com") { |s|
75
- s.action = "/create.php"
76
- s.block = lambda { |body| URI.extract(body)[2] }
77
- },
78
-
79
- :snipurl => Service.new("snipurl.com") { |s|
80
- s.action = "/index.php"
81
- s.field = "link"
82
- s.block = lambda { |body|
83
- line = body.split("\n").grep(/txt/)[0]
84
- short_url = URI.extract(line)[1][0..-2] # Remove trailing '
55
+ class ShortURL
56
+ # Hash table of all the supported services. The key is a symbol
57
+ # representing the service (usually the hostname minus the .com,
58
+ # .net, etc.) The value is an instance of Service with all the
59
+ # parameters set so that when +instance+.call is invoked, the
60
+ # shortened URL is returned.
61
+ @@services = {
62
+ :rubyurl => Service.new("rubyurl.com") { |s|
63
+ s.code = 302
64
+ s.method = :get
65
+ s.action = "/rubyurl/create"
66
+ s.field = "rubyurl[website_url]"
67
+ s.block = lambda { |body| URI.extract(body)[0].gsub("rubyurl/show/", "") }
68
+ },
69
+
70
+ :tinyurl => Service.new("tinyurl.com") { |s|
71
+ s.action = "/create.php"
72
+ s.block = lambda { |body| URI.extract(body).grep(/tinyurl/)[-1] }
73
+ },
74
+
75
+ :shorl => Service.new("shorl.com") { |s|
76
+ s.action = "/create.php"
77
+ s.block = lambda { |body| URI.extract(body)[2] }
78
+ },
79
+
80
+ :snipurl => Service.new("snipurl.com") { |s|
81
+ s.action = "/index.php"
82
+ s.field = "link"
83
+ s.block = lambda { |body|
84
+ line = body.split("\n").grep(/txt/)[0]
85
+ short_url = URI.extract(line)[1][0..-2] # Remove trailing '
86
+ }
87
+ },
88
+
89
+ :metamark => Service.new("metamark.net") { |s|
90
+ s.action = "/add"
91
+ s.field = "long_url"
92
+ s.block = lambda { |body| URI.extract(body).grep(/xrl.us/)[0] }
93
+ },
94
+
95
+ :makeashorterlink => Service.new("makeashorterlink.com") { |s|
96
+ s.action = "/index.php"
97
+ s.block = lambda { |body| URI.extract(body).grep(/makeashorterlink/)[0] }
98
+ },
99
+
100
+ :skinnylink => Service.new("skinnylink.com") { |s|
101
+ s.block = lambda { |body| URI.extract(body).grep(/skinnylink/)[0] }
102
+ },
103
+
104
+ :linktrim => Service.new("linktrim.com") { |s|
105
+ s.method = :get
106
+ s.action = "/lt/generate"
107
+ s.block = lambda { |body| URI.extract(body).grep(/\/linktrim/)[1] }
108
+ },
109
+
110
+ :shorterlink => Service.new("shorterlink.com") { |s|
111
+ s.method = :get
112
+ s.action = "/add_url.html"
113
+ s.block = lambda { |body| URI.extract(body).grep(/shorterlink/)[0] }
114
+ },
115
+
116
+ :minilink => Service.new("minilink.org") { |s|
117
+ s.method = :get
118
+ s.block = lambda { |body| URI.extract(body)[-1] }
119
+ },
120
+
121
+ :lns => Service.new("ln-s.net") { |s|
122
+ s.method = :get
123
+ s.action = "/home/api.jsp"
124
+ s.block = lambda { |body| URI.extract(body)[0] }
125
+ },
126
+
127
+ :fyad => Service.new("fyad.org") { |s|
128
+ s.method = :get
129
+ s.block = lambda { |body| URI.extract(body).grep(/fyad.org/)[2] }
130
+ },
131
+
132
+ :d62 => Service.new("d62.net") { |s|
133
+ s.method = :get
134
+ s.block = lambda { |body| URI.extract(body)[0] }
135
+ },
136
+
137
+ :shiturl => Service.new("shiturl.com") { |s|
138
+ s.method = :get
139
+ s.action = "/make.php"
140
+ s.block = lambda { |body| URI.extract(body).grep(/shiturl/)[0] }
141
+ },
142
+
143
+ :littlink => Service.new("littlink.com") { |s|
144
+ s.block = lambda { |body| URI.extract(body).grep(/littlink/)[0] }
145
+ },
146
+
147
+ :clipurl => Service.new("clipurl.com") { |s|
148
+ s.action = "/create.asp"
149
+ s.block = lambda { |body| URI.extract(body).grep(/clipurl/)[0] }
150
+ },
151
+
152
+ :shortify => Service.new("shortify.com") { |s|
153
+ s.method = :get
154
+ s.action = "/shorten.php"
155
+ s.block = lambda { |body| URI.extract(body).grep(/shortify/)[0] }
156
+ },
157
+
158
+ :orz => Service.new("0rz.net") { |s|
159
+ s.action = "/create.php"
160
+ s.block = lambda { |body| URI.extract(body).grep(/0rz/)[0] }
85
161
  }
86
- },
87
-
88
- :metamark => Service.new("metamark.net") { |s|
89
- s.action = "/add"
90
- s.field = "long_url"
91
- s.block = lambda { |body| URI.extract(body).grep(/xrl.us/)[0] }
92
- },
93
-
94
- :makeashorterlink => Service.new("makeashorterlink.com") { |s|
95
- s.action = "/index.php"
96
- s.block = lambda { |body| URI.extract(body).grep(/makeashorterlink/)[0] }
97
- },
98
-
99
- :skinnylink => Service.new("skinnylink.com") { |s|
100
- s.block = lambda { |body| URI.extract(body).grep(/skinnylink/)[0] }
101
- },
102
-
103
- :linktrim => Service.new("linktrim.com") { |s|
104
- s.method = :get
105
- s.action = "/lt/generate"
106
- s.block = lambda { |body| URI.extract(body).grep(/\/linktrim/)[1] }
107
- },
108
-
109
- :shorterlink => Service.new("shorterlink.com") { |s|
110
- s.method = :get
111
- s.action = "/add_url.html"
112
- s.block = lambda { |body| URI.extract(body).grep(/shorterlink/)[0] }
113
- },
114
-
115
- :minilink => Service.new("minilink.org") { |s|
116
- s.method = :get
117
- s.block = lambda { |body| URI.extract(body)[-1] }
118
- },
119
-
120
- :lns => Service.new("ln-s.net") { |s|
121
- s.method = :get
122
- s.action = "/home/api.jsp"
123
- s.block = lambda { |body| URI.extract(body)[0] }
124
- },
125
-
126
- :fyad => Service.new("fyad.org") { |s|
127
- s.method = :get
128
- s.block = lambda { |body| URI.extract(body).grep(/fyad.org/)[2] }
129
- },
130
-
131
- :d62 => Service.new("d62.net") { |s|
132
- s.method = :get
133
- s.block = lambda { |body| URI.extract(body)[0] }
134
- },
135
-
136
- :shiturl => Service.new("shiturl.com") { |s|
137
- s.method = :get
138
- s.action = "/make.php"
139
- s.block = lambda { |body| URI.extract(body).grep(/shiturl/)[0] }
140
- },
141
-
142
- :littlink => Service.new("littlink.com") { |s|
143
- s.block = lambda { |body| URI.extract(body).grep(/littlink/)[0] }
144
- },
145
-
146
- :clipurl => Service.new("clipurl.com") { |s|
147
- s.action = "/create.asp"
148
- s.block = lambda { |body| URI.extract(body).grep(/clipurl/)[0] }
149
- },
150
-
151
- :shortify => Service.new("shortify.com") { |s|
152
- s.method = :get
153
- s.action = "/shorten.php"
154
- s.block = lambda { |body| URI.extract(body).grep(/shortify/)[0] }
155
- },
156
-
157
- :orz => Service.new("0rz.net") { |s|
158
- s.action = "/create.php"
159
- s.block = lambda { |body| URI.extract(body).grep(/0rz/)[0] }
160
162
  }
161
- }
162
163
 
163
- # Array containing symbols representing all the implemented URL
164
- # shortening services
165
- @@valid_services = @@services.keys
164
+ # Array containing symbols representing all the implemented URL
165
+ # shortening services
166
+ @@valid_services = @@services.keys
166
167
 
167
- # Returns @@valid_services
168
- def self.valid_services
169
- @@valid_services
170
- end
168
+ # Returns @@valid_services
169
+ def self.valid_services
170
+ @@valid_services
171
+ end
171
172
 
172
- # Main method of ShortURL, its usage is quite simple, just give an
173
- # url to shorten and an optional service. If no service is
174
- # selected, RubyURL.com will be used. An invalid service symbol
175
- # will raise an ArgumentError exception
176
- #
177
- # Valid +service+ values:
178
- #
179
- # * <tt>:rubyurl</tt>
180
- # * <tt>:tinyurl</tt>
181
- # * <tt>:shorl</tt>
182
- # * <tt>:snipurl</tt>
183
- # * <tt>:metamark</tt>
184
- # * <tt>:makeashorterlink</tt>
185
- # * <tt>:skinnylink</tt>
186
- # * <tt>:linktrim</tt>
187
- # * <tt>:shorterlink</tt>
188
- # * <tt>:minlink</tt>
189
- # * <tt>:lns</tt>
190
- # * <tt>:fyad</tt>
191
- # * <tt>:d62</tt>
192
- # * <tt>:shiturl</tt>
193
- # * <tt>:littlink</tt>
194
- # * <tt>:clipurl</tt>
195
- # * <tt>:shortify</tt>
196
- # * <tt>:orz</tt>
197
- #
198
- # call-seq:
199
- # ShortURL.shorten("http://mypage.com") => Uses RubyURL
200
- # ShortURL.shorten("http://mypage.com", :tinyurl)
201
- def self.shorten(url, service = :rubyurl)
202
- if valid_services.include? service
203
- @@services[service].call(url)
204
- else
205
- raise InvalidService
173
+ # Main method of ShortURL, its usage is quite simple, just give an
174
+ # url to shorten and an optional service. If no service is
175
+ # selected, RubyURL.com will be used. An invalid service symbol
176
+ # will raise an ArgumentError exception
177
+ #
178
+ # Valid +service+ values:
179
+ #
180
+ # * <tt>:rubyurl</tt>
181
+ # * <tt>:tinyurl</tt>
182
+ # * <tt>:shorl</tt>
183
+ # * <tt>:snipurl</tt>
184
+ # * <tt>:metamark</tt>
185
+ # * <tt>:makeashorterlink</tt>
186
+ # * <tt>:skinnylink</tt>
187
+ # * <tt>:linktrim</tt>
188
+ # * <tt>:shorterlink</tt>
189
+ # * <tt>:minlink</tt>
190
+ # * <tt>:lns</tt>
191
+ # * <tt>:fyad</tt>
192
+ # * <tt>:d62</tt>
193
+ # * <tt>:shiturl</tt>
194
+ # * <tt>:littlink</tt>
195
+ # * <tt>:clipurl</tt>
196
+ # * <tt>:shortify</tt>
197
+ # * <tt>:orz</tt>
198
+ #
199
+ # call-seq:
200
+ # ShortURL.shorten("http://mypage.com") => Uses RubyURL
201
+ # ShortURL.shorten("http://mypage.com", :tinyurl)
202
+ def self.shorten(url, service = :rubyurl)
203
+ if valid_services.include? service
204
+ @@services[service].call(url)
205
+ else
206
+ raise InvalidService
207
+ end
206
208
  end
207
209
  end
208
210
  end
data/test/tc_service.rb CHANGED
@@ -9,7 +9,7 @@ require "test/unit"
9
9
  require "shorturl"
10
10
 
11
11
 
12
- class TestService < Test::Unit::TestCase
12
+ class WWW::TestService < Test::Unit::TestCase
13
13
 
14
14
  def test_call
15
15
  service = Service.new("oasdasobf")
data/test/tc_shorturl.rb CHANGED
@@ -14,7 +14,7 @@ class String
14
14
  end
15
15
  end
16
16
 
17
- class TestShortURL < Test::Unit::TestCase
17
+ class WWW::TestShortURL < Test::Unit::TestCase
18
18
  def setup
19
19
  @url = "http://groups.google.com/group/comp.lang.ruby/"
20
20
  end
metadata CHANGED
@@ -3,7 +3,7 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: shorturl
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.8.0
6
+ version: 0.8.1
7
7
  date: 2005-10-02 00:00:00 -04:00
8
8
  summary: Shortens URLs using services such as TinyURL and RubyURL
9
9
  require_paths: