shorturl 0.0.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/MIT-LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2005, Vincent Foley
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7
+ of the Software, and to permit persons to whom the Software is furnished to do
8
+ so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
data/README ADDED
@@ -0,0 +1,13 @@
1
+ = ShortURL
2
+
3
+ == Summary
4
+ ShortURL is a very simple library to use URL shortening services such as
5
+ TinyURL or RubyURL.
6
+
7
+ == Usage:
8
+ call-seq:
9
+ require "shorturl"
10
+ puts ShortURL.shorten("http://mypage.com")
11
+
12
+ == Author
13
+ Vincent Foley
data/TODO ADDED
@@ -0,0 +1,5 @@
1
+ = Todo
2
+
3
+ * Catch exceptions
4
+ * More tests
5
+ * More robust HTML scraping
@@ -0,0 +1,256 @@
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>Class: ShortURL</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>Class</strong></td>
53
+ <td class="class-name-in-header">ShortURL</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
+ <tr class="top-aligned-row">
66
+ <td><strong>Parent:</strong></td>
67
+ <td>
68
+ Object
69
+ </td>
70
+ </tr>
71
+ </table>
72
+ </div>
73
+ <!-- banner header -->
74
+
75
+ <div id="bodyContent">
76
+
77
+
78
+
79
+ <div id="contextContent">
80
+
81
+
82
+
83
+ </div>
84
+
85
+ <div id="method-list">
86
+ <h3 class="section-bar">Methods</h3>
87
+
88
+ <div class="name-list">
89
+ <a href="#M000002">rubyurl</a>&nbsp;&nbsp;
90
+ <a href="#M000001">shorten</a>&nbsp;&nbsp;
91
+ <a href="#M000003">tinyurl</a>&nbsp;&nbsp;
92
+ </div>
93
+ </div>
94
+
95
+ </div>
96
+
97
+
98
+ <!-- if includes -->
99
+
100
+ <div id="section">
101
+
102
+
103
+
104
+
105
+
106
+
107
+
108
+
109
+ <!-- if method_list -->
110
+ <div id="methods">
111
+ <h3 class="section-bar">Public Class methods</h3>
112
+
113
+ <div id="method-M000001" class="method-detail">
114
+ <a name="M000001"></a>
115
+
116
+ <div class="method-heading">
117
+ <a href="#M000001" class="method-signature">
118
+ <span class="method-name">shorten</span><span class="method-args">(url, service = :rubyurl)</span>
119
+ </a>
120
+ </div>
121
+
122
+ <div class="method-description">
123
+ <p>
124
+ Main method of <a href="ShortURL.html">ShortURL</a>, its usage is quite
125
+ simple, just give a url to shorten and an optional service. If no service
126
+ is selected, RubyURL.com will be used. An invalid service symbol will raise
127
+ an ArgumentError exception
128
+ </p>
129
+ <p>
130
+ Valid <tt>service</tt> values:
131
+ </p>
132
+ <table>
133
+ <tr><td valign="top"><tt>:rubyurl</tt>:</td><td>uses RubyURL.com
134
+
135
+ </td></tr>
136
+ <tr><td valign="top"><tt>:tinyurl</tt>:</td><td>uses TinyURL.com
137
+
138
+ </td></tr>
139
+ </table>
140
+ <p>
141
+ call-seq:
142
+ </p>
143
+ <pre>
144
+ ShortURL.shorten(&quot;http://mypage.com&quot;) =&gt; Uses RubyURL
145
+ ShortURL.shorten(&quot;http://mypage.com&quot;, :tinyurl)
146
+ </pre>
147
+ <p><a class="source-toggle" href="#"
148
+ onclick="toggleCode('M000001-source');return false;">[Source]</a></p>
149
+ <div class="method-source-code" id="M000001-source">
150
+ <pre>
151
+ <span class="ruby-comment cmt"># File lib/shorturl.rb, line 19</span>
152
+ 19: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">shorten</span>(<span class="ruby-identifier">url</span>, <span class="ruby-identifier">service</span> = <span class="ruby-identifier">:rubyurl</span>)
153
+ 20: <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">service</span>
154
+ 21: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:rubyurl</span> <span class="ruby-keyword kw">then</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">new</span>.<span class="ruby-identifier">rubyurl</span>(<span class="ruby-identifier">url</span>)
155
+ 22: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:tinyurl</span> <span class="ruby-keyword kw">then</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">new</span>.<span class="ruby-identifier">tinyurl</span>(<span class="ruby-identifier">url</span>)
156
+ 23: <span class="ruby-keyword kw">else</span> <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-value str">&quot;Invalid service&quot;</span>
157
+ 24: <span class="ruby-keyword kw">end</span>
158
+ 25: <span class="ruby-keyword kw">end</span>
159
+ </pre>
160
+ </div>
161
+ </div>
162
+ </div>
163
+
164
+ <h3 class="section-bar">Public Instance methods</h3>
165
+
166
+ <div id="method-M000002" class="method-detail">
167
+ <a name="M000002"></a>
168
+
169
+ <div class="method-heading">
170
+ <a href="#M000002" class="method-signature">
171
+ <span class="method-name">rubyurl</span><span class="method-args">(url)</span>
172
+ </a>
173
+ </div>
174
+
175
+ <div class="method-description">
176
+ <p>
177
+ Shortens an URL with RubyURL.com. Since RubyURL uses a redirection (code
178
+ 302), instead of using HTTP#post, we send our data directly to the
179
+ <em>create</em> action. The response is a small HTML line which contains
180
+ our shortened URL plus <em>/rubyurl/show</em>, so we manually remove that
181
+ with gsub.
182
+ </p>
183
+ <p><a class="source-toggle" href="#"
184
+ onclick="toggleCode('M000002-source');return false;">[Source]</a></p>
185
+ <div class="method-source-code" id="M000002-source">
186
+ <pre>
187
+ <span class="ruby-comment cmt"># File lib/shorturl.rb, line 32</span>
188
+ 32: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">rubyurl</span>(<span class="ruby-identifier">url</span>)
189
+ 33: <span class="ruby-constant">Net</span><span class="ruby-operator">::</span><span class="ruby-constant">HTTP</span>.<span class="ruby-identifier">start</span>(<span class="ruby-value str">&quot;rubyurl.com&quot;</span>, <span class="ruby-value">80</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">http</span><span class="ruby-operator">|</span>
190
+ 34: <span class="ruby-identifier">response</span> = <span class="ruby-identifier">http</span>.<span class="ruby-identifier">get</span>(<span class="ruby-node">&quot;/rubyurl/create?rubyurl[website_url]=#{CGI.escape(url)}&quot;</span>)
191
+ 35:
192
+ 36: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">response</span>.<span class="ruby-identifier">code</span> <span class="ruby-operator">==</span> <span class="ruby-value str">&quot;302&quot;</span>
193
+ 37: <span class="ruby-identifier">body</span> = <span class="ruby-identifier">response</span>.<span class="ruby-identifier">read_body</span>
194
+ 38: <span class="ruby-identifier">regex</span> = <span class="ruby-regexp re">/&lt;a href=&quot;(.+)&quot;&gt;/</span>
195
+ 39: <span class="ruby-identifier">short_url</span> = <span class="ruby-identifier">regex</span>.<span class="ruby-identifier">match</span>(<span class="ruby-identifier">body</span>)[<span class="ruby-value">1</span>].<span class="ruby-identifier">gsub</span>(<span class="ruby-regexp re">/rubyurl\/show\//</span>, <span class="ruby-value str">&quot;&quot;</span>)
196
+ 40: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">short_url</span>
197
+ 41: <span class="ruby-keyword kw">end</span>
198
+ 42: }
199
+ 43: <span class="ruby-keyword kw">end</span>
200
+ </pre>
201
+ </div>
202
+ </div>
203
+ </div>
204
+
205
+ <div id="method-M000003" class="method-detail">
206
+ <a name="M000003"></a>
207
+
208
+ <div class="method-heading">
209
+ <a href="#M000003" class="method-signature">
210
+ <span class="method-name">tinyurl</span><span class="method-args">(url)</span>
211
+ </a>
212
+ </div>
213
+
214
+ <div class="method-description">
215
+ <p>
216
+ Shortens an URL with TinyURL.com. We post a request to the
217
+ <em>create.php</em> action with our URL as a parameter. We then read the
218
+ HTML code, split it into an array, find an element that contains our
219
+ shortened URL and extract it.
220
+ </p>
221
+ <p><a class="source-toggle" href="#"
222
+ onclick="toggleCode('M000003-source');return false;">[Source]</a></p>
223
+ <div class="method-source-code" id="M000003-source">
224
+ <pre>
225
+ <span class="ruby-comment cmt"># File lib/shorturl.rb, line 50</span>
226
+ 50: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">tinyurl</span>(<span class="ruby-identifier">url</span>)
227
+ 51: <span class="ruby-constant">Net</span><span class="ruby-operator">::</span><span class="ruby-constant">HTTP</span>.<span class="ruby-identifier">start</span>(<span class="ruby-value str">&quot;tinyurl.com&quot;</span>, <span class="ruby-value">80</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">http</span><span class="ruby-operator">|</span>
228
+ 52: <span class="ruby-identifier">response</span> = <span class="ruby-identifier">http</span>.<span class="ruby-identifier">post</span>(<span class="ruby-value str">&quot;/create.php&quot;</span>, <span class="ruby-node">&quot;url=#{url}&quot;</span>)
229
+ 53:
230
+ 54: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">response</span>.<span class="ruby-identifier">code</span> <span class="ruby-operator">==</span> <span class="ruby-value str">&quot;200&quot;</span>
231
+ 55: <span class="ruby-identifier">body</span> = <span class="ruby-identifier">response</span>.<span class="ruby-identifier">read_body</span>
232
+ 56: <span class="ruby-identifier">line</span> = <span class="ruby-identifier">body</span>.<span class="ruby-identifier">split</span>(<span class="ruby-value str">&quot;\n&quot;</span>).<span class="ruby-identifier">find</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">l</span><span class="ruby-operator">|</span> <span class="ruby-identifier">l</span> <span class="ruby-operator">=~</span> <span class="ruby-regexp re">/hidden name=tinyurl/</span> }
233
+ 57: <span class="ruby-identifier">i1</span> = <span class="ruby-identifier">line</span>.<span class="ruby-identifier">index</span>(<span class="ruby-value str">&quot;http&quot;</span>)
234
+ 58: <span class="ruby-identifier">i2</span> = <span class="ruby-identifier">line</span>.<span class="ruby-identifier">rindex</span>(<span class="ruby-value str">&quot;\&quot;&quot;</span>)
235
+ 59: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">line</span>[<span class="ruby-identifier">i1</span><span class="ruby-operator">...</span><span class="ruby-identifier">i2</span>]
236
+ 60: <span class="ruby-keyword kw">end</span>
237
+ 61: }
238
+ 62: <span class="ruby-keyword kw">end</span>
239
+ </pre>
240
+ </div>
241
+ </div>
242
+ </div>
243
+
244
+
245
+ </div>
246
+
247
+
248
+ </div>
249
+
250
+
251
+ <div id="validator-badges">
252
+ <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
253
+ </div>
254
+
255
+ </body>
256
+ </html>
data/doc/created.rid ADDED
@@ -0,0 +1 @@
1
+ Wed Jun 01 11:08:51 EDT 2005
@@ -0,0 +1,129 @@
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>File: MIT-LICENSE</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="fileHeader">
50
+ <h1>MIT-LICENSE</h1>
51
+ <table class="header-table">
52
+ <tr class="top-aligned-row">
53
+ <td><strong>Path:</strong></td>
54
+ <td>MIT-LICENSE
55
+ </td>
56
+ </tr>
57
+ <tr class="top-aligned-row">
58
+ <td><strong>Last Update:</strong></td>
59
+ <td>Wed Jun 01 08:34:05 EDT 2005</td>
60
+ </tr>
61
+ </table>
62
+ </div>
63
+ <!-- banner header -->
64
+
65
+ <div id="bodyContent">
66
+
67
+
68
+
69
+ <div id="contextContent">
70
+
71
+ <div id="description">
72
+ <p>
73
+ Copyright &#169; 2005, Vincent Foley
74
+ </p>
75
+ <p>
76
+ Permission is hereby granted, free of charge, to any person obtaining a
77
+ copy of this software and associated documentation files (the
78
+ &quot;Software&quot;), to deal in the Software without restriction,
79
+ including without limitation the rights to use, copy, modify, merge,
80
+ publish, distribute, sublicense, and/or sell copies of the Software, and to
81
+ permit persons to whom the Software is furnished to do so, subject to the
82
+ following conditions:
83
+ </p>
84
+ <p>
85
+ The above copyright notice and this permission notice shall be included in
86
+ all copies or substantial portions of the Software.
87
+ </p>
88
+ <p>
89
+ THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND,
90
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
91
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
92
+ NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
93
+ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
94
+ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
95
+ USE OR OTHER DEALINGS IN THE SOFTWARE.
96
+ </p>
97
+
98
+ </div>
99
+
100
+
101
+ </div>
102
+
103
+
104
+ </div>
105
+
106
+
107
+ <!-- if includes -->
108
+
109
+ <div id="section">
110
+
111
+
112
+
113
+
114
+
115
+
116
+
117
+
118
+ <!-- if method_list -->
119
+
120
+
121
+ </div>
122
+
123
+
124
+ <div id="validator-badges">
125
+ <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
126
+ </div>
127
+
128
+ </body>
129
+ </html>
@@ -0,0 +1,118 @@
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>File: README</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="fileHeader">
50
+ <h1>README</h1>
51
+ <table class="header-table">
52
+ <tr class="top-aligned-row">
53
+ <td><strong>Path:</strong></td>
54
+ <td>README
55
+ </td>
56
+ </tr>
57
+ <tr class="top-aligned-row">
58
+ <td><strong>Last Update:</strong></td>
59
+ <td>Wed Jun 01 10:23:54 EDT 2005</td>
60
+ </tr>
61
+ </table>
62
+ </div>
63
+ <!-- banner header -->
64
+
65
+ <div id="bodyContent">
66
+
67
+
68
+
69
+ <div id="contextContent">
70
+
71
+ <div id="description">
72
+ <h1><a href="../classes/ShortURL.html">ShortURL</a></h1>
73
+ <h2>Summary</h2>
74
+ <p>
75
+ <a href="../classes/ShortURL.html">ShortURL</a> is a very simple library to
76
+ use URL shortening services such as TinyURL or RubyURL.
77
+ </p>
78
+ <h2>Usage:</h2>
79
+ <p>
80
+ call-seq:
81
+ </p>
82
+ <pre>
83
+ require &quot;shorturl&quot;
84
+ puts ShortURL.shorten(&quot;http://mypage.com&quot;)
85
+ </pre>
86
+
87
+ </div>
88
+
89
+
90
+ </div>
91
+
92
+
93
+ </div>
94
+
95
+
96
+ <!-- if includes -->
97
+
98
+ <div id="section">
99
+
100
+
101
+
102
+
103
+
104
+
105
+
106
+
107
+ <!-- if method_list -->
108
+
109
+
110
+ </div>
111
+
112
+
113
+ <div id="validator-badges">
114
+ <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
115
+ </div>
116
+
117
+ </body>
118
+ </html>
@@ -0,0 +1,116 @@
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>File: TODO</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="fileHeader">
50
+ <h1>TODO</h1>
51
+ <table class="header-table">
52
+ <tr class="top-aligned-row">
53
+ <td><strong>Path:</strong></td>
54
+ <td>TODO
55
+ </td>
56
+ </tr>
57
+ <tr class="top-aligned-row">
58
+ <td><strong>Last Update:</strong></td>
59
+ <td>Wed Jun 01 08:35:56 EDT 2005</td>
60
+ </tr>
61
+ </table>
62
+ </div>
63
+ <!-- banner header -->
64
+
65
+ <div id="bodyContent">
66
+
67
+
68
+
69
+ <div id="contextContent">
70
+
71
+ <div id="description">
72
+ <h1>Todo</h1>
73
+ <ul>
74
+ <li>Catch exceptions
75
+
76
+ </li>
77
+ <li>More tests
78
+
79
+ </li>
80
+ <li>More robust HTML scraping
81
+
82
+ </li>
83
+ </ul>
84
+
85
+ </div>
86
+
87
+
88
+ </div>
89
+
90
+
91
+ </div>
92
+
93
+
94
+ <!-- if includes -->
95
+
96
+ <div id="section">
97
+
98
+
99
+
100
+
101
+
102
+
103
+
104
+
105
+ <!-- if method_list -->
106
+
107
+
108
+ </div>
109
+
110
+
111
+ <div id="validator-badges">
112
+ <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
113
+ </div>
114
+
115
+ </body>
116
+ </html>
@@ -0,0 +1,109 @@
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>File: shorturl.rb</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="fileHeader">
50
+ <h1>shorturl.rb</h1>
51
+ <table class="header-table">
52
+ <tr class="top-aligned-row">
53
+ <td><strong>Path:</strong></td>
54
+ <td>lib/shorturl.rb
55
+ </td>
56
+ </tr>
57
+ <tr class="top-aligned-row">
58
+ <td><strong>Last Update:</strong></td>
59
+ <td>Wed Jun 01 08:47:02 EDT 2005</td>
60
+ </tr>
61
+ </table>
62
+ </div>
63
+ <!-- banner header -->
64
+
65
+ <div id="bodyContent">
66
+
67
+
68
+
69
+ <div id="contextContent">
70
+
71
+
72
+ <div id="requires-list">
73
+ <h3 class="section-bar">Required files</h3>
74
+
75
+ <div class="name-list">
76
+ net/http&nbsp;&nbsp;
77
+ cgi&nbsp;&nbsp;
78
+ </div>
79
+ </div>
80
+
81
+ </div>
82
+
83
+
84
+ </div>
85
+
86
+
87
+ <!-- if includes -->
88
+
89
+ <div id="section">
90
+
91
+
92
+
93
+
94
+
95
+
96
+
97
+
98
+ <!-- if method_list -->
99
+
100
+
101
+ </div>
102
+
103
+
104
+ <div id="validator-badges">
105
+ <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
106
+ </div>
107
+
108
+ </body>
109
+ </html>
@@ -0,0 +1,27 @@
1
+
2
+ <?xml version="1.0" encoding="iso-8859-1"?>
3
+ <!DOCTYPE html
4
+ PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
5
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
6
+
7
+ <!--
8
+
9
+ Classes
10
+
11
+ -->
12
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
13
+ <head>
14
+ <title>Classes</title>
15
+ <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
16
+ <link rel="stylesheet" href="rdoc-style.css" type="text/css" />
17
+ <base target="docwin" />
18
+ </head>
19
+ <body>
20
+ <div id="index">
21
+ <h1 class="section-bar">Classes</h1>
22
+ <div id="index-entries">
23
+ <a href="classes/ShortURL.html">ShortURL</a><br />
24
+ </div>
25
+ </div>
26
+ </body>
27
+ </html>
@@ -0,0 +1,30 @@
1
+
2
+ <?xml version="1.0" encoding="iso-8859-1"?>
3
+ <!DOCTYPE html
4
+ PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
5
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
6
+
7
+ <!--
8
+
9
+ Files
10
+
11
+ -->
12
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
13
+ <head>
14
+ <title>Files</title>
15
+ <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
16
+ <link rel="stylesheet" href="rdoc-style.css" type="text/css" />
17
+ <base target="docwin" />
18
+ </head>
19
+ <body>
20
+ <div id="index">
21
+ <h1 class="section-bar">Files</h1>
22
+ <div id="index-entries">
23
+ <a href="files/MIT-LICENSE.html">MIT-LICENSE</a><br />
24
+ <a href="files/README.html">README</a><br />
25
+ <a href="files/TODO.html">TODO</a><br />
26
+ <a href="files/lib/shorturl_rb.html">lib/shorturl.rb</a><br />
27
+ </div>
28
+ </div>
29
+ </body>
30
+ </html>
@@ -0,0 +1,29 @@
1
+
2
+ <?xml version="1.0" encoding="iso-8859-1"?>
3
+ <!DOCTYPE html
4
+ PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
5
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
6
+
7
+ <!--
8
+
9
+ Methods
10
+
11
+ -->
12
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
13
+ <head>
14
+ <title>Methods</title>
15
+ <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
16
+ <link rel="stylesheet" href="rdoc-style.css" type="text/css" />
17
+ <base target="docwin" />
18
+ </head>
19
+ <body>
20
+ <div id="index">
21
+ <h1 class="section-bar">Methods</h1>
22
+ <div id="index-entries">
23
+ <a href="classes/ShortURL.html#M000002">rubyurl (ShortURL)</a><br />
24
+ <a href="classes/ShortURL.html#M000001">shorten (ShortURL)</a><br />
25
+ <a href="classes/ShortURL.html#M000003">tinyurl (ShortURL)</a><br />
26
+ </div>
27
+ </div>
28
+ </body>
29
+ </html>
data/doc/index.html ADDED
@@ -0,0 +1,24 @@
1
+ <?xml version="1.0" encoding="iso-8859-1"?>
2
+ <!DOCTYPE html
3
+ PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"
4
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
5
+
6
+ <!--
7
+
8
+ ShortURL Documentation
9
+
10
+ -->
11
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
12
+ <head>
13
+ <title>ShortURL Documentation</title>
14
+ <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
15
+ </head>
16
+ <frameset rows="20%, 80%">
17
+ <frameset cols="25%,35%,45%">
18
+ <frame src="fr_file_index.html" title="Files" name="Files" />
19
+ <frame src="fr_class_index.html" name="Classes" />
20
+ <frame src="fr_method_index.html" name="Methods" />
21
+ </frameset>
22
+ <frame src="files/README.html" name="docwin" />
23
+ </frameset>
24
+ </html>
@@ -0,0 +1,208 @@
1
+
2
+ body {
3
+ font-family: Verdana,Arial,Helvetica,sans-serif;
4
+ font-size: 90%;
5
+ margin: 0;
6
+ margin-left: 40px;
7
+ padding: 0;
8
+ background: white;
9
+ }
10
+
11
+ h1,h2,h3,h4 { margin: 0; color: #efefef; background: transparent; }
12
+ h1 { font-size: 150%; }
13
+ h2,h3,h4 { margin-top: 1em; }
14
+
15
+ a { background: #eef; color: #039; text-decoration: none; }
16
+ a:hover { background: #039; color: #eef; }
17
+
18
+ /* Override the base stylesheet's Anchor inside a table cell */
19
+ td > a {
20
+ background: transparent;
21
+ color: #039;
22
+ text-decoration: none;
23
+ }
24
+
25
+ /* and inside a section title */
26
+ .section-title > a {
27
+ background: transparent;
28
+ color: #eee;
29
+ text-decoration: none;
30
+ }
31
+
32
+ /* === Structural elements =================================== */
33
+
34
+ div#index {
35
+ margin: 0;
36
+ margin-left: -40px;
37
+ padding: 0;
38
+ font-size: 90%;
39
+ }
40
+
41
+
42
+ div#index a {
43
+ margin-left: 0.7em;
44
+ }
45
+
46
+ div#index .section-bar {
47
+ margin-left: 0px;
48
+ padding-left: 0.7em;
49
+ background: #ccc;
50
+ font-size: small;
51
+ }
52
+
53
+
54
+ div#classHeader, div#fileHeader {
55
+ width: auto;
56
+ color: white;
57
+ padding: 0.5em 1.5em 0.5em 1.5em;
58
+ margin: 0;
59
+ margin-left: -40px;
60
+ border-bottom: 3px solid #006;
61
+ }
62
+
63
+ div#classHeader a, div#fileHeader a {
64
+ background: inherit;
65
+ color: white;
66
+ }
67
+
68
+ div#classHeader td, div#fileHeader td {
69
+ background: inherit;
70
+ color: white;
71
+ }
72
+
73
+
74
+ div#fileHeader {
75
+ background: #057;
76
+ }
77
+
78
+ div#classHeader {
79
+ background: #048;
80
+ }
81
+
82
+
83
+ .class-name-in-header {
84
+ font-size: 180%;
85
+ font-weight: bold;
86
+ }
87
+
88
+
89
+ div#bodyContent {
90
+ padding: 0 1.5em 0 1.5em;
91
+ }
92
+
93
+ div#description {
94
+ padding: 0.5em 1.5em;
95
+ background: #efefef;
96
+ border: 1px dotted #999;
97
+ }
98
+
99
+ div#description h1,h2,h3,h4,h5,h6 {
100
+ color: #125;;
101
+ background: transparent;
102
+ }
103
+
104
+ div#validator-badges {
105
+ text-align: center;
106
+ }
107
+ div#validator-badges img { border: 0; }
108
+
109
+ div#copyright {
110
+ color: #333;
111
+ background: #efefef;
112
+ font: 0.75em sans-serif;
113
+ margin-top: 5em;
114
+ margin-bottom: 0;
115
+ padding: 0.5em 2em;
116
+ }
117
+
118
+
119
+ /* === Classes =================================== */
120
+
121
+ table.header-table {
122
+ color: white;
123
+ font-size: small;
124
+ }
125
+
126
+ .type-note {
127
+ font-size: small;
128
+ color: #DEDEDE;
129
+ }
130
+
131
+ .xxsection-bar {
132
+ background: #eee;
133
+ color: #333;
134
+ padding: 3px;
135
+ }
136
+
137
+ .section-bar {
138
+ color: #333;
139
+ border-bottom: 1px solid #999;
140
+ margin-left: -20px;
141
+ }
142
+
143
+
144
+ .section-title {
145
+ background: #79a;
146
+ color: #eee;
147
+ padding: 3px;
148
+ margin-top: 2em;
149
+ margin-left: -30px;
150
+ border: 1px solid #999;
151
+ }
152
+
153
+ .top-aligned-row { vertical-align: top }
154
+ .bottom-aligned-row { vertical-align: bottom }
155
+
156
+ /* --- Context section classes ----------------------- */
157
+
158
+ .context-row { }
159
+ .context-item-name { font-family: monospace; font-weight: bold; color: black; }
160
+ .context-item-value { font-size: small; color: #448; }
161
+ .context-item-desc { color: #333; padding-left: 2em; }
162
+
163
+ /* --- Method classes -------------------------- */
164
+ .method-detail {
165
+ background: #efefef;
166
+ padding: 0;
167
+ margin-top: 0.5em;
168
+ margin-bottom: 1em;
169
+ border: 1px dotted #ccc;
170
+ }
171
+ .method-heading {
172
+ color: black;
173
+ background: #ccc;
174
+ border-bottom: 1px solid #666;
175
+ padding: 0.2em 0.5em 0 0.5em;
176
+ }
177
+ .method-signature { color: black; background: inherit; }
178
+ .method-name { font-weight: bold; }
179
+ .method-args { font-style: italic; }
180
+ .method-description { padding: 0 0.5em 0 0.5em; }
181
+
182
+ /* --- Source code sections -------------------- */
183
+
184
+ a.source-toggle { font-size: 90%; }
185
+ div.method-source-code {
186
+ background: #262626;
187
+ color: #ffdead;
188
+ margin: 1em;
189
+ padding: 0.5em;
190
+ border: 1px dashed #999;
191
+ overflow: hidden;
192
+ }
193
+
194
+ div.method-source-code pre { color: #ffdead; overflow: hidden; }
195
+
196
+ /* --- Ruby keyword styles --------------------- */
197
+
198
+ .standalone-code { background: #221111; color: #ffdead; overflow: hidden; }
199
+
200
+ .ruby-constant { color: #7fffd4; background: transparent; }
201
+ .ruby-keyword { color: #00ffff; background: transparent; }
202
+ .ruby-ivar { color: #eedd82; background: transparent; }
203
+ .ruby-operator { color: #00ffee; background: transparent; }
204
+ .ruby-identifier { color: #ffdead; background: transparent; }
205
+ .ruby-node { color: #ffa07a; background: transparent; }
206
+ .ruby-comment { color: #b22222; font-weight: bold; background: transparent; }
207
+ .ruby-regexp { color: #ffa07a; background: transparent; }
208
+ .ruby-value { color: #7fffd4; background: transparent; }
data/lib/shorturl.rb ADDED
@@ -0,0 +1,63 @@
1
+ require "net/http"
2
+ require "cgi"
3
+
4
+ class ShortURL
5
+
6
+ # Main method of ShortURL, its usage is quite simple, just give a
7
+ # url to shorten and an optional service. If no service is
8
+ # selected, RubyURL.com will be used. An invalid service symbol
9
+ # will raise an ArgumentError exception
10
+ #
11
+ # Valid +service+ values:
12
+ #
13
+ # <tt>:rubyurl</tt>:: uses RubyURL.com
14
+ # <tt>:tinyurl</tt>:: uses TinyURL.com
15
+ #
16
+ # call-seq:
17
+ # ShortURL.shorten("http://mypage.com") => Uses RubyURL
18
+ # ShortURL.shorten("http://mypage.com", :tinyurl)
19
+ def self.shorten(url, service = :rubyurl)
20
+ case service
21
+ when :rubyurl then self.new.rubyurl(url)
22
+ when :tinyurl then self.new.tinyurl(url)
23
+ else raise ArgumentError, "Invalid service"
24
+ end
25
+ end
26
+
27
+ # Shortens an URL with RubyURL.com. Since RubyURL uses a
28
+ # redirection (code 302), instead of using HTTP#post, we send our
29
+ # data directly to the _create_ action. The response is a small
30
+ # HTML line which contains our shortened URL plus
31
+ # <em>/rubyurl/show</em>, so we manually remove that with gsub.
32
+ def rubyurl(url)
33
+ Net::HTTP.start("rubyurl.com", 80) { |http|
34
+ response = http.get("/rubyurl/create?rubyurl[website_url]=#{CGI.escape(url)}")
35
+
36
+ if response.code == "302"
37
+ body = response.read_body
38
+ regex = /<a href="(.+)">/
39
+ short_url = regex.match(body)[1].gsub(/rubyurl\/show\//, "")
40
+ return short_url
41
+ end
42
+ }
43
+ end
44
+
45
+
46
+ # Shortens an URL with TinyURL.com. We post a request to the
47
+ # <em>create.php</em> action with our URL as a parameter. We then
48
+ # read the HTML code, split it into an array, find an element that
49
+ # contains our shortened URL and extract it.
50
+ def tinyurl(url)
51
+ Net::HTTP.start("tinyurl.com", 80) { |http|
52
+ response = http.post("/create.php", "url=#{url}")
53
+
54
+ if response.code == "200"
55
+ body = response.read_body
56
+ line = body.split("\n").find { |l| l =~ /hidden name=tinyurl/ }
57
+ i1 = line.index("http")
58
+ i2 = line.rindex("\"")
59
+ return line[i1...i2]
60
+ end
61
+ }
62
+ end
63
+ end
@@ -0,0 +1,22 @@
1
+ # tc_shortcut.rb
2
+ #
3
+ # Created by Vincent Foley on 2005-06-01
4
+
5
+ $test_lib_dir = File.join(File.dirname(__FILE__), "..", "lib")
6
+ $:.unshift($test_lib_dir)
7
+
8
+ require "test/unit"
9
+ require "shorturl"
10
+
11
+ class TestShortURL < Test::Unit::TestCase
12
+ def setup
13
+ @url = "http://darkhost.mine.nu:81/~vince/rails/tutorial.html"
14
+ end
15
+
16
+ def test_shorten
17
+ assert ShortURL.shorten(@url) == "http://rubyurl.com/Q9ToW"
18
+ assert ShortURL.shorten(@url, :rubyurl) == "http://rubyurl.com/Q9ToW"
19
+ assert ShortURL.shorten(@url, :tinyurl) == "http://tinyurl.com/9mop8"
20
+ assert_raise(ArgumentError) { ShortURL.shorten(@url, :foobar) }
21
+ end
22
+ end
data/test/ts_all.rb ADDED
@@ -0,0 +1,10 @@
1
+ # ts_all.rb
2
+ #
3
+ # Created by Vincent Foley on 2005-06-01
4
+
5
+ $test_lib_dir = File.join(File.dirname(__FILE__))
6
+ $:.unshift($test_lib_dir)
7
+
8
+ require "test/unit"
9
+
10
+ require "tc_shorturl"
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.10
3
+ specification_version: 1
4
+ name: shorturl
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.0.1
7
+ date: 2005-06-01
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:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ -
22
+ - ">"
23
+ - !ruby/object:Gem::Version
24
+ version: 0.0.0
25
+ version:
26
+ platform: ruby
27
+ authors:
28
+ - Vincent Foley
29
+ files:
30
+ - lib/shorturl.rb
31
+ - doc/classes
32
+ - doc/created.rid
33
+ - doc/files
34
+ - doc/fr_class_index.html
35
+ - doc/fr_file_index.html
36
+ - doc/fr_method_index.html
37
+ - doc/index.html
38
+ - doc/rdoc-style.css
39
+ - doc/classes/ShortURL.html
40
+ - doc/files/lib
41
+ - doc/files/MIT-LICENSE.html
42
+ - doc/files/README.html
43
+ - doc/files/TODO.html
44
+ - doc/files/lib/shorturl_rb.html
45
+ - test/tc_shorturl.rb
46
+ - test/ts_all.rb
47
+ - README
48
+ - TODO
49
+ - MIT-LICENSE
50
+ test_files:
51
+ - test/ts_all.rb
52
+ rdoc_options:
53
+ - "--title"
54
+ - ShortURL Documentation
55
+ - "--main"
56
+ - README
57
+ - "-S"
58
+ - "-N"
59
+ extra_rdoc_files:
60
+ - README
61
+ - TODO
62
+ - MIT-LICENSE
63
+ executables: []
64
+ extensions: []
65
+ requirements: []
66
+ dependencies: []