shorturl 0.8.3 → 0.8.4

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/lib/shorturl.rb CHANGED
@@ -7,168 +7,210 @@ require "net/http"
7
7
  require "cgi"
8
8
  require "uri"
9
9
 
10
- module WWW
11
- class InvalidService < Exception
12
- end
10
+ class InvalidService < Exception
11
+ end
13
12
 
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 = :get
27
- @action = "/"
28
- @field = "url"
29
- @block = lambda { |body| }
30
-
31
- if block_given?
32
- yield self
33
- end
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
34
32
  end
33
+ end
35
34
 
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
50
- end
51
- }
52
- end
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
+ @block.call(response.read_body)
45
+ end
46
+ }
53
47
  end
48
+ end
54
49
 
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.action = "/rubyurl/remote"
65
- s.field = "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 = "/api-create.php"
71
- s.block = lambda { |body| body }
72
- },
73
-
74
- :metamark => Service.new("metamark.net") { |s|
75
- s.action = "/api/rest/simple"
76
- s.field = "long_url"
77
- s.block = lambda { |body| body }
78
- },
79
-
80
- :orz => Service.new("0rz.net") { |s|
81
- s.action = "/createget.php"
82
- s.block = lambda { |body| URI.extract(body).grep(/0rz.net/)[0] }
83
- },
84
-
85
- :linktrim => Service.new("linktrim.com") { |s|
86
- s.action = "/lt/generate"
87
- s.block = lambda { |body| URI.extract(body).grep(/\/linktrim/)[1] }
88
- },
89
-
90
- :shorterlink => Service.new("shorterlink.com") { |s|
91
- s.action = "/add_url.html"
92
- s.block = lambda { |body| URI.extract(body).grep(/shorterlink/)[0] }
93
- },
94
-
95
- :minilink => Service.new("minilink.org") { |s|
96
- s.block = lambda { |body| URI.extract(body)[-1] }
97
- },
98
-
99
- :lns => Service.new("ln-s.net") { |s|
100
- s.action = "/home/api.jsp"
101
- s.block = lambda { |body| URI.extract(body)[0] }
102
- },
103
-
104
- :fyad => Service.new("fyad.org") { |s|
105
- s.block = lambda { |body| URI.extract(body).grep(/fyad.org/)[2] }
106
- },
107
-
108
- :d62 => Service.new("d62.net") { |s|
109
- s.block = lambda { |body| URI.extract(body)[0] }
110
- },
111
-
112
- :shiturl => Service.new("shiturl.com") { |s|
113
- s.action = "/make.php"
114
- s.block = lambda { |body| URI.extract(body).grep(/shiturl/)[0] }
115
- },
116
-
117
- :littlink => Service.new("littlink.com") { |s|
118
- s.block = lambda { |body| URI.extract(body).grep(/littlink/)[0] }
119
- },
120
-
121
- :shortify => Service.new("shortify.com") { |s|
122
- s.action = "/shorten.php"
123
- s.block = lambda { |body| URI.extract(body).grep(/shortify/)[0] }
124
- },
125
-
126
- :shurl => Service.new("shurl.org") { |s|
127
- s.method = :post
128
- s.block = lambda { |body| URI.extract(body).grep(/shurl/)[0] }
50
+ class ShortURL
51
+ # Hash table of all the supported services. The key is a symbol
52
+ # representing the service (usually the hostname minus the .com,
53
+ # .net, etc.) The value is an instance of Service with all the
54
+ # parameters set so that when +instance+.call is invoked, the
55
+ # shortened URL is returned.
56
+ @@services = {
57
+ :rubyurl => Service.new("rubyurl.com") { |s|
58
+ s.action = "/rubyurl/remote"
59
+ s.field = "website_url"
60
+ s.block = lambda { |body| URI.extract(body).grep(/rubyurl/)[0] }
61
+ },
62
+
63
+ :tinyurl => Service.new("tinyurl.com") { |s|
64
+ s.action = "/create.php"
65
+ s.block = lambda { |body| URI.extract(body).grep(/tinyurl/)[-1] }
66
+ },
67
+
68
+ :shorl => Service.new("shorl.com") { |s|
69
+ s.action = "/create.php"
70
+ s.block = lambda { |body| URI.extract(body)[2] }
71
+ },
72
+
73
+ :snipurl => Service.new("snipurl.com") { |s|
74
+ s.action = "/index.php"
75
+ s.field = "link"
76
+ s.block = lambda { |body|
77
+ line = body.split("\n").grep(/txt/)[0]
78
+ short_url = URI.extract(line)[1][0..-2] # Remove trailing '
129
79
  }
80
+ },
81
+
82
+ :metamark => Service.new("metamark.net") { |s|
83
+ s.action = "/add"
84
+ s.field = "long_url"
85
+ s.block = lambda { |body| URI.extract(body).grep(/xrl.us/)[0] }
86
+ },
87
+
88
+ :makeashorterlink => Service.new("makeashorterlink.com") { |s|
89
+ s.action = "/index.php"
90
+ s.block = lambda { |body| URI.extract(body).grep(/makeashorterlink/)[0] }
91
+ },
92
+
93
+ :skinnylink => Service.new("skinnylink.com") { |s|
94
+ s.block = lambda { |body| URI.extract(body).grep(/skinnylink/)[0] }
95
+ },
96
+
97
+ :linktrim => Service.new("linktrim.com") { |s|
98
+ s.method = :get
99
+ s.action = "/lt/generate"
100
+ s.block = lambda { |body| URI.extract(body).grep(/\/linktrim/)[1] }
101
+ },
102
+
103
+ :shorterlink => Service.new("shorterlink.com") { |s|
104
+ s.method = :get
105
+ s.action = "/add_url.html"
106
+ s.block = lambda { |body| URI.extract(body).grep(/shorterlink/)[0] }
107
+ },
108
+
109
+ :minilink => Service.new("minilink.org") { |s|
110
+ s.method = :get
111
+ s.block = lambda { |body| URI.extract(body)[-1] }
112
+ },
113
+
114
+ :lns => Service.new("ln-s.net") { |s|
115
+ s.method = :get
116
+ s.action = "/home/api.jsp"
117
+ s.block = lambda { |body| URI.extract(body)[0] }
118
+ },
119
+
120
+ :fyad => Service.new("fyad.org") { |s|
121
+ s.method = :get
122
+ s.block = lambda { |body| URI.extract(body).grep(/fyad.org/)[2] }
123
+ },
124
+
125
+ :d62 => Service.new("d62.net") { |s|
126
+ s.method = :get
127
+ s.block = lambda { |body| URI.extract(body)[0] }
128
+ },
129
+
130
+ :shiturl => Service.new("shiturl.com") { |s|
131
+ s.method = :get
132
+ s.action = "/make.php"
133
+ s.block = lambda { |body| URI.extract(body).grep(/shiturl/)[0] }
134
+ },
135
+
136
+ :littlink => Service.new("littlink.com") { |s|
137
+ s.block = lambda { |body| URI.extract(body).grep(/littlink/)[0] }
138
+ },
139
+
140
+ :clipurl => Service.new("clipurl.com") { |s|
141
+ s.action = "/create.asp"
142
+ s.block = lambda { |body| URI.extract(body).grep(/clipurl/)[0] }
143
+ },
144
+
145
+ :shortify => Service.new("shortify.com") { |s|
146
+ s.method = :get
147
+ s.action = "/shorten.php"
148
+ s.block = lambda { |body| URI.extract(body).grep(/shortify/)[0] }
149
+ },
150
+
151
+ :orz => Service.new("0rz.net") { |s|
152
+ s.action = "/create.php"
153
+ s.block = lambda { |body| URI.extract(body).grep(/0rz/)[0] }
154
+ },
155
+
156
+ :moourl => Service.new("moourl.com") { |s|
157
+ s.code = 302
158
+ s.action = "/create/"
159
+ s.method = :get
160
+ s.field = "source"
161
+ s.block = lambda { |body| body.gsub('Location/woot/?moo=','http://moourl.com/') }
162
+ },
163
+
164
+ :urltea => Service.new("urltea.com") { |s|
165
+ s.method = :get
166
+ s.action = "/create/"
167
+ s.block = lambda { |body| URI.extract(body).grep(/urltea/)[6] }
130
168
  }
169
+ }
131
170
 
132
- # Array containing symbols representing all the implemented URL
133
- # shortening services
134
- @@valid_services = @@services.keys
171
+ # Array containing symbols representing all the implemented URL
172
+ # shortening services
173
+ @@valid_services = @@services.keys
135
174
 
136
- # Returns @@valid_services
137
- def self.valid_services
138
- @@valid_services
139
- end
175
+ # Returns @@valid_services
176
+ def self.valid_services
177
+ @@valid_services
178
+ end
140
179
 
141
- # Main method of ShortURL, its usage is quite simple, just give an
142
- # url to shorten and an optional service. If no service is
143
- # selected, RubyURL.com will be used. An invalid service symbol
144
- # will raise an ArgumentError exception
145
- #
146
- # Valid +service+ values:
147
- #
148
- # * <tt>:d62</tt>
149
- # * <tt>:fyad</tt>
150
- # * <tt>:linktrim</tt>
151
- # * <tt>:littlink</tt>
152
- # * <tt>:lns</tt>
153
- # * <tt>:metamark</tt>
154
- # * <tt>:minilink</tt>
155
- # * <tt>:orz</tt>
156
- # * <tt>:rubyurl</tt>
157
- # * <tt>:shiturl</tt>
158
- # * <tt>:shorterlink</tt>
159
- # * <tt>:shortify</tt>
160
- # * <tt>:shurl</tt>
161
- # * <tt>:tinyurl</tt>
162
- #
163
- # call-seq:
164
- # ShortURL.shorten("http://mypage.com") => Uses RubyURL
165
- # ShortURL.shorten("http://mypage.com", :tinyurl)
166
- def self.shorten(url, service = :rubyurl)
167
- if valid_services.include? service
168
- @@services[service].call(url)
169
- else
170
- raise InvalidService
171
- end
180
+ # Main method of ShortURL, its usage is quite simple, just give an
181
+ # url to shorten and an optional service. If no service is
182
+ # selected, RubyURL.com will be used. An invalid service symbol
183
+ # will raise an ArgumentError exception
184
+ #
185
+ # Valid +service+ values:
186
+ #
187
+ # * <tt>:rubyurl</tt>
188
+ # * <tt>:tinyurl</tt>
189
+ # * <tt>:shorl</tt>
190
+ # * <tt>:snipurl</tt>
191
+ # * <tt>:metamark</tt>
192
+ # * <tt>:makeashorterlink</tt>
193
+ # * <tt>:skinnylink</tt>
194
+ # * <tt>:linktrim</tt>
195
+ # * <tt>:shorterlink</tt>
196
+ # * <tt>:minlink</tt>
197
+ # * <tt>:lns</tt>
198
+ # * <tt>:fyad</tt>
199
+ # * <tt>:d62</tt>
200
+ # * <tt>:shiturl</tt>
201
+ # * <tt>:littlink</tt>
202
+ # * <tt>:clipurl</tt>
203
+ # * <tt>:shortify</tt>
204
+ # * <tt>:orz</tt>
205
+ #
206
+ # call-seq:
207
+ # ShortURL.shorten("http://mypage.com") => Uses RubyURL
208
+ # ShortURL.shorten("http://mypage.com", :tinyurl)
209
+ def self.shorten(url, service = :rubyurl)
210
+ if valid_services.include? service
211
+ @@services[service].call(url)
212
+ else
213
+ raise InvalidService
172
214
  end
173
215
  end
174
216
  end
data/test/CVS/Entries ADDED
@@ -0,0 +1,4 @@
1
+ /tc_service.rb/1.1.1.1/Mon Sep 26 23:19:07 2005//
2
+ /ts_all.rb/1.1.1.1/Mon Sep 26 23:19:07 2005//
3
+ /tc_shorturl.rb/1.1.1.1/Sun Jan 6 22:26:06 2008//
4
+ D
@@ -0,0 +1 @@
1
+ support/test
data/test/CVS/Root ADDED
@@ -0,0 +1 @@
1
+ :pserver:anonymous@rubyforge.org:/var/cvs/shorturl
data/test/tc_service.rb CHANGED
@@ -11,8 +11,6 @@ require "shorturl"
11
11
 
12
12
  class TestService < Test::Unit::TestCase
13
13
 
14
- include WWW
15
-
16
14
  def test_call
17
15
  service = Service.new("oasdasobf")
18
16
  assert_raise(SocketError) { service.call(nil) }
@@ -31,7 +29,7 @@ class TestService < Test::Unit::TestCase
31
29
  service = Service.new("rubyurl.com")
32
30
  assert_equal(service.port, 80)
33
31
  assert_equal(service.code, 200)
34
- assert_equal(service.method, :get)
32
+ assert_equal(service.method, :post)
35
33
  assert_equal(service.action, "/")
36
34
  assert_equal(service.field, "url")
37
35
 
data/test/tc_shorturl.rb CHANGED
@@ -15,7 +15,6 @@ class String
15
15
  end
16
16
 
17
17
  class TestShortURL < Test::Unit::TestCase
18
- include WWW
19
18
  def setup
20
19
  @url = "http://groups.google.com/group/comp.lang.ruby/"
21
20
  end
metadata CHANGED
@@ -1,64 +1,82 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.8.11
3
- specification_version: 1
4
2
  name: shorturl
5
3
  version: !ruby/object:Gem::Version
6
- version: 0.8.3
7
- date: 2006-03-15 00:00:00 -05:00
8
- summary: Shortens URLs using services such as TinyURL and RubyURL
9
- require_paths:
10
- - lib
11
- email: vfoleybourgon@yahoo.ca
12
- homepage: http://shorturl.rubyforge.org
13
- rubyforge_project:
14
- description:
15
- autorequire: shorturl
16
- default_executable: shorturl
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:
4
+ version: 0.8.4
25
5
  platform: ruby
26
- signing_key:
27
- cert_chain:
28
6
  authors:
29
7
  - Vincent Foley
8
+ autorequire: shorturl
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-01-06 00:00:00 -08:00
13
+ default_executable: shorturl
14
+ dependencies: []
15
+
16
+ description:
17
+ email: vfoleybourgon@yahoo.ca
18
+ executables:
19
+ - shorturl
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ - TODO
25
+ - MIT-LICENSE
26
+ - ChangeLog
30
27
  files:
28
+ - bin/CVS
29
+ - bin/CVS/Entries
30
+ - bin/CVS/Repository
31
+ - bin/CVS/Root
31
32
  - bin/shorturl
33
+ - lib/CVS
34
+ - lib/CVS/Entries
35
+ - lib/CVS/Repository
36
+ - lib/CVS/Root
32
37
  - lib/shorturl.rb
38
+ - doc/classes
39
+ - doc/classes/InvalidService.html
40
+ - doc/classes/Service.html
41
+ - doc/classes/Service.src
42
+ - doc/classes/Service.src/M000003.html
43
+ - doc/classes/Service.src/M000004.html
44
+ - doc/classes/ShortURL.html
45
+ - doc/classes/ShortURL.src
46
+ - doc/classes/ShortURL.src/M000001.html
47
+ - doc/classes/ShortURL.src/M000002.html
33
48
  - doc/created.rid
34
- - doc/rdoc-style.css
35
49
  - doc/files
36
- - doc/classes
37
- - doc/fr_file_index.html
38
- - doc/fr_class_index.html
39
- - doc/fr_method_index.html
40
- - doc/index.html
41
- - doc/files/README.html
42
- - doc/files/TODO.html
43
- - doc/files/MIT-LICENSE.html
44
50
  - doc/files/ChangeLog.html
45
51
  - doc/files/lib
46
52
  - doc/files/lib/shorturl_rb.html
47
- - doc/classes/WWW.html
48
- - doc/classes/WWW
49
- - doc/classes/WWW/ShortURL.html
50
- - doc/classes/WWW/InvalidService.html
51
- - doc/classes/WWW/Service.html
53
+ - doc/files/MIT-LICENSE.html
54
+ - doc/files/README.html
55
+ - doc/files/TODO.html
56
+ - doc/fr_class_index.html
57
+ - doc/fr_file_index.html
58
+ - doc/fr_method_index.html
59
+ - doc/index.html
60
+ - doc/rdoc-style.css
61
+ - test/CVS
62
+ - test/CVS/Entries
63
+ - test/CVS/Repository
64
+ - test/CVS/Root
52
65
  - test/tc_service.rb
53
66
  - test/tc_shorturl.rb
54
67
  - test/ts_all.rb
68
+ - examples/CVS
69
+ - examples/CVS/Entries
70
+ - examples/CVS/Repository
71
+ - examples/CVS/Root
55
72
  - examples/shorten.rb
56
73
  - README
57
74
  - TODO
58
75
  - MIT-LICENSE
59
76
  - ChangeLog
60
- test_files:
61
- - test/ts_all.rb
77
+ has_rdoc: true
78
+ homepage: http://shorturl.rubyforge.org
79
+ post_install_message:
62
80
  rdoc_options:
63
81
  - --title
64
82
  - ShortURL Documentation
@@ -67,16 +85,26 @@ rdoc_options:
67
85
  - -S
68
86
  - -N
69
87
  - --all
70
- extra_rdoc_files:
71
- - README
72
- - TODO
73
- - MIT-LICENSE
74
- - ChangeLog
75
- executables:
76
- - shorturl
77
- extensions: []
78
-
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: "0"
95
+ version:
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: "0"
101
+ version:
79
102
  requirements: []
80
103
 
81
- dependencies: []
82
-
104
+ rubyforge_project:
105
+ rubygems_version: 1.0.1
106
+ signing_key:
107
+ specification_version: 2
108
+ summary: Shortens URLs using services such as RubyURL, urlTea, and TinyURL
109
+ test_files:
110
+ - test/ts_all.rb
data/doc/classes/WWW.html DELETED
@@ -1,113 +0,0 @@
1
- <?xml version="1.0" encoding="iso-8859-1"?>
2
- <!DOCTYPE html
3
- PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
-
6
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
7
- <head>
8
- <title>Module: WWW</title>
9
- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
10
- <meta http-equiv="Content-Script-Type" content="text/javascript" />
11
- <link rel="stylesheet" href=".././rdoc-style.css" type="text/css" media="screen" />
12
- <script type="text/javascript">
13
- // <![CDATA[
14
-
15
- function popupCode( url ) {
16
- window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
17
- }
18
-
19
- function toggleCode( id ) {
20
- if ( document.getElementById )
21
- elem = document.getElementById( id );
22
- else if ( document.all )
23
- elem = eval( "document.all." + id );
24
- else
25
- return false;
26
-
27
- elemStyle = elem.style;
28
-
29
- if ( elemStyle.display != "block" ) {
30
- elemStyle.display = "block"
31
- } else {
32
- elemStyle.display = "none"
33
- }
34
-
35
- return true;
36
- }
37
-
38
- // Make codeblocks hidden by default
39
- document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
40
-
41
- // ]]>
42
- </script>
43
-
44
- </head>
45
- <body>
46
-
47
-
48
-
49
- <div id="classHeader">
50
- <table class="header-table">
51
- <tr class="top-aligned-row">
52
- <td><strong>Module</strong></td>
53
- <td class="class-name-in-header">WWW</td>
54
- </tr>
55
- <tr class="top-aligned-row">
56
- <td><strong>In:</strong></td>
57
- <td>
58
- <a href="../files/lib/shorturl_rb.html">
59
- lib/shorturl.rb
60
- </a>
61
- <br />
62
- </td>
63
- </tr>
64
-
65
- </table>
66
- </div>
67
- <!-- banner header -->
68
-
69
- <div id="bodyContent">
70
-
71
-
72
-
73
- <div id="contextContent">
74
-
75
-
76
-
77
- </div>
78
-
79
-
80
- </div>
81
-
82
-
83
- <!-- if includes -->
84
-
85
- <div id="section">
86
-
87
- <div id="class-list">
88
- <h3 class="section-bar">Classes and Modules</h3>
89
-
90
- Class <a href="WWW/InvalidService.html" class="link">WWW::InvalidService</a><br />
91
- Class <a href="WWW/Service.html" class="link">WWW::Service</a><br />
92
- Class <a href="WWW/ShortURL.html" class="link">WWW::ShortURL</a><br />
93
-
94
- </div>
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
- <!-- if method_list -->
103
-
104
-
105
- </div>
106
-
107
-
108
- <div id="validator-badges">
109
- <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
110
- </div>
111
-
112
- </body>
113
- </html>