shorturl 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog ADDED
@@ -0,0 +1,10 @@
1
+ 0.1.0:
2
+ - Refactored the tinyurl and rubyurl methods
3
+ - URI.extract to get URLs instead of regular expressions
4
+ - Added exception handling to get_short_url to return nil in case
5
+ of a network error
6
+ - More thorough unit tests
7
+ - Made tinyurl, rubyurl and get_short_url private class methods
8
+
9
+ 0.0.1:
10
+ - Initial Version
data/README CHANGED
@@ -4,10 +4,19 @@
4
4
  ShortURL is a very simple library to use URL shortening services such as
5
5
  TinyURL or RubyURL.
6
6
 
7
+ == Installation
8
+ <tt>$ gem install -r shorturl</tt>
9
+
7
10
  == Usage:
8
11
  call-seq:
9
12
  require "shorturl"
10
13
  puts ShortURL.shorten("http://mypage.com")
14
+ puts ShortURL.shorten("http://mypage.com", :tinyurl)
11
15
 
16
+ == Thanks
17
+ - Marcel Molina Jr., Devin Mullins for some ideas
18
+ - imperator from #ruby-lang (don't know your real name, sorry) for
19
+ helping me with creating and uploading a RubyGem
20
+
12
21
  == Author
13
22
  Vincent Foley
@@ -86,9 +86,10 @@
86
86
  <h3 class="section-bar">Methods</h3>
87
87
 
88
88
  <div class="name-list">
89
- <a href="#M000002">rubyurl</a>&nbsp;&nbsp;
89
+ <a href="#M000002">get_short_url</a>&nbsp;&nbsp;
90
+ <a href="#M000003">rubyurl</a>&nbsp;&nbsp;
90
91
  <a href="#M000001">shorten</a>&nbsp;&nbsp;
91
- <a href="#M000003">tinyurl</a>&nbsp;&nbsp;
92
+ <a href="#M000004">tinyurl</a>&nbsp;&nbsp;
92
93
  </div>
93
94
  </div>
94
95
 
@@ -148,26 +149,72 @@ call-seq:
148
149
  onclick="toggleCode('M000001-source');return false;">[Source]</a></p>
149
150
  <div class="method-source-code" id="M000001-source">
150
151
  <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>
152
+ <span class="ruby-comment cmt"># File lib/shorturl.rb, line 26</span>
153
+ 26: <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>)
154
+ 27: <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">service</span>
155
+ 28: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:rubyurl</span> <span class="ruby-keyword kw">then</span> <span class="ruby-identifier">rubyurl</span>(<span class="ruby-identifier">url</span>)
156
+ 29: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:tinyurl</span> <span class="ruby-keyword kw">then</span> <span class="ruby-identifier">tinyurl</span>(<span class="ruby-identifier">url</span>)
157
+ 30: <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>
158
+ 31: <span class="ruby-keyword kw">end</span>
159
+ 32: <span class="ruby-keyword kw">end</span>
159
160
  </pre>
160
161
  </div>
161
162
  </div>
162
163
  </div>
163
164
 
164
- <h3 class="section-bar">Public Instance methods</h3>
165
+ <h3 class="section-bar">Private Class methods</h3>
165
166
 
166
167
  <div id="method-M000002" class="method-detail">
167
168
  <a name="M000002"></a>
168
169
 
169
170
  <div class="method-heading">
170
171
  <a href="#M000002" class="method-signature">
172
+ <span class="method-name">get_short_url</span><span class="method-args">(hostname, code, action) {|html_body| ...}</span>
173
+ </a>
174
+ </div>
175
+
176
+ <div class="method-description">
177
+ <p>
178
+ Abstract method to shorten a URL. Respects the DRY principle. In case of a
179
+ network-related exception, return <tt>nil</tt>.
180
+ </p>
181
+ <p>
182
+ Parameters:
183
+ </p>
184
+ <pre>
185
+ hostname: hostname of the service
186
+ code: expected return code (200, 302, etc.) Converted to a string
187
+ action: string representing the action to be executed on the HTTP object with +instance_eval+
188
+ block: block of code which scrapes the HTML and returns the shortened URL. Takes one parameter.
189
+ </pre>
190
+ <p><a class="source-toggle" href="#"
191
+ onclick="toggleCode('M000002-source');return false;">[Source]</a></p>
192
+ <div class="method-source-code" id="M000002-source">
193
+ <pre>
194
+ <span class="ruby-comment cmt"># File lib/shorturl.rb, line 42</span>
195
+ 42: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">get_short_url</span>(<span class="ruby-identifier">hostname</span>, <span class="ruby-identifier">code</span>, <span class="ruby-identifier">action</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>) <span class="ruby-comment cmt"># :yields: html_body</span>
196
+ 43: <span class="ruby-keyword kw">begin</span>
197
+ 44: <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-identifier">hostname</span>, <span class="ruby-value">80</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">http</span><span class="ruby-operator">|</span>
198
+ 45: <span class="ruby-identifier">response</span> = <span class="ruby-identifier">http</span>.<span class="ruby-identifier">instance_eval</span>(<span class="ruby-identifier">action</span>)
199
+ 46:
200
+ 47: <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-identifier">code</span>.<span class="ruby-identifier">to_s</span>
201
+ 48: <span class="ruby-identifier">block</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">response</span>.<span class="ruby-identifier">read_body</span>)
202
+ 49: <span class="ruby-keyword kw">end</span>
203
+ 50: }
204
+ 51: <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">SocketError</span>, <span class="ruby-constant">Net</span><span class="ruby-operator">::</span><span class="ruby-constant">HTTPExceptions</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">e</span>
205
+ 52: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">nil</span>
206
+ 53: <span class="ruby-keyword kw">end</span>
207
+ 54: <span class="ruby-keyword kw">end</span>
208
+ </pre>
209
+ </div>
210
+ </div>
211
+ </div>
212
+
213
+ <div id="method-M000003" class="method-detail">
214
+ <a name="M000003"></a>
215
+
216
+ <div class="method-heading">
217
+ <a href="#M000003" class="method-signature">
171
218
  <span class="method-name">rubyurl</span><span class="method-args">(url)</span>
172
219
  </a>
173
220
  </div>
@@ -181,32 +228,27 @@ our shortened URL plus <em>/rubyurl/show</em>, so we manually remove that
181
228
  with gsub.
182
229
  </p>
183
230
  <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">
231
+ onclick="toggleCode('M000003-source');return false;">[Source]</a></p>
232
+ <div class="method-source-code" id="M000003-source">
186
233
  <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>
234
+ <span class="ruby-comment cmt"># File lib/shorturl.rb, line 62</span>
235
+ 62: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">rubyurl</span>(<span class="ruby-identifier">url</span>)
236
+ 63: <span class="ruby-identifier">get_short_url</span>(<span class="ruby-value str">&quot;rubyurl.com&quot;</span>, <span class="ruby-value">302</span>,
237
+ 64: <span class="ruby-node">&quot;get('/rubyurl/create?rubyurl[website_url]=#{CGI.escape(url)}')&quot;</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">body</span><span class="ruby-operator">|</span>
238
+ 65: <span class="ruby-identifier">short_url</span> = <span class="ruby-constant">URI</span>.<span class="ruby-identifier">extract</span>(<span class="ruby-identifier">body</span>)[<span class="ruby-value">0</span>].<span class="ruby-identifier">gsub</span>(<span class="ruby-value str">&quot;rubyurl/show/&quot;</span>, <span class="ruby-value str">&quot;&quot;</span>)
239
+ 66: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">short_url</span>
240
+ 67: }
241
+ 68: <span class="ruby-keyword kw">end</span>
200
242
  </pre>
201
243
  </div>
202
244
  </div>
203
245
  </div>
204
246
 
205
- <div id="method-M000003" class="method-detail">
206
- <a name="M000003"></a>
247
+ <div id="method-M000004" class="method-detail">
248
+ <a name="M000004"></a>
207
249
 
208
250
  <div class="method-heading">
209
- <a href="#M000003" class="method-signature">
251
+ <a href="#M000004" class="method-signature">
210
252
  <span class="method-name">tinyurl</span><span class="method-args">(url)</span>
211
253
  </a>
212
254
  </div>
@@ -219,23 +261,18 @@ HTML code, split it into an array, find an element that contains our
219
261
  shortened URL and extract it.
220
262
  </p>
221
263
  <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">
264
+ onclick="toggleCode('M000004-source');return false;">[Source]</a></p>
265
+ <div class="method-source-code" id="M000004-source">
224
266
  <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>
267
+ <span class="ruby-comment cmt"># File lib/shorturl.rb, line 75</span>
268
+ 75: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">tinyurl</span>(<span class="ruby-identifier">url</span>)
269
+ 76: <span class="ruby-identifier">get_short_url</span>(<span class="ruby-value str">&quot;tinyurl.com&quot;</span>, <span class="ruby-value">200</span>,
270
+ 77: <span class="ruby-node">&quot;post('/create.php', 'url=#{url}')&quot;</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">body</span><span class="ruby-operator">|</span>
271
+ 78: <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> }
272
+ 79: <span class="ruby-identifier">short_url</span> = <span class="ruby-constant">URI</span>.<span class="ruby-identifier">extract</span>(<span class="ruby-identifier">line</span>)[<span class="ruby-value">0</span>]
273
+ 80: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">short_url</span>
274
+ 81: }
275
+ 82: <span class="ruby-keyword kw">end</span>
239
276
  </pre>
240
277
  </div>
241
278
  </div>
data/doc/created.rid CHANGED
@@ -1 +1 @@
1
- Wed Jun 01 11:08:51 EDT 2005
1
+ Thu Jun 02 18:25:20 EDT 2005
@@ -0,0 +1,121 @@
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: ChangeLog</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>ChangeLog</h1>
51
+ <table class="header-table">
52
+ <tr class="top-aligned-row">
53
+ <td><strong>Path:</strong></td>
54
+ <td>ChangeLog
55
+ </td>
56
+ </tr>
57
+ <tr class="top-aligned-row">
58
+ <td><strong>Last Update:</strong></td>
59
+ <td>Thu Jun 02 17:27:57 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
+ 0.1.0:
74
+ </p>
75
+ <pre>
76
+ - Refactored the tinyurl and rubyurl methods
77
+ - URI.extract to get URLs instead of regular expressions
78
+ - Added exception handling to get_short_url to return nil in case
79
+ of a network error
80
+ - More thorough unit tests
81
+ - Made tinyurl, rubyurl and get_short_url private class methods
82
+ </pre>
83
+ <p>
84
+ 0.0.1:
85
+ </p>
86
+ <pre>
87
+ - Initial Version
88
+ </pre>
89
+
90
+ </div>
91
+
92
+
93
+ </div>
94
+
95
+
96
+ </div>
97
+
98
+
99
+ <!-- if includes -->
100
+
101
+ <div id="section">
102
+
103
+
104
+
105
+
106
+
107
+
108
+
109
+
110
+ <!-- if method_list -->
111
+
112
+
113
+ </div>
114
+
115
+
116
+ <div id="validator-badges">
117
+ <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
118
+ </div>
119
+
120
+ </body>
121
+ </html>
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Wed Jun 01 10:23:54 EDT 2005</td>
59
+ <td>Thu Jun 02 18:25:07 EDT 2005</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -75,6 +75,10 @@
75
75
  <a href="../classes/ShortURL.html">ShortURL</a> is a very simple library to
76
76
  use URL shortening services such as TinyURL or RubyURL.
77
77
  </p>
78
+ <h2>Installation</h2>
79
+ <p>
80
+ <tt>$ gem install -r shorturl</tt>
81
+ </p>
78
82
  <h2>Usage:</h2>
79
83
  <p>
80
84
  call-seq:
@@ -82,7 +86,22 @@ call-seq:
82
86
  <pre>
83
87
  require &quot;shorturl&quot;
84
88
  puts ShortURL.shorten(&quot;http://mypage.com&quot;)
89
+ puts ShortURL.shorten(&quot;http://mypage.com&quot;, :tinyurl)
85
90
  </pre>
91
+ <h2>Thanks</h2>
92
+ <ul>
93
+ <li>Marcel Molina Jr., Devin Mullins for some ideas
94
+
95
+ </li>
96
+ <li>imperator from ruby-lang (don&#8217;t know your real name, sorry) for
97
+ helping me with creating and uploading a RubyGem
98
+
99
+ </li>
100
+ </ul>
101
+ <h2>Author</h2>
102
+ <p>
103
+ Vincent Foley
104
+ </p>
86
105
 
87
106
  </div>
88
107
 
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Wed Jun 01 08:47:02 EDT 2005</td>
59
+ <td>Thu Jun 02 18:23:38 EDT 2005</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -68,6 +68,15 @@
68
68
 
69
69
  <div id="contextContent">
70
70
 
71
+ <div id="description">
72
+ <p>
73
+ shorturl.rb
74
+ </p>
75
+ <pre>
76
+ Created by Vincent Foley on 2005-06-02
77
+ </pre>
78
+
79
+ </div>
71
80
 
72
81
  <div id="requires-list">
73
82
  <h3 class="section-bar">Required files</h3>
@@ -75,6 +84,7 @@
75
84
  <div class="name-list">
76
85
  net/http&nbsp;&nbsp;
77
86
  cgi&nbsp;&nbsp;
87
+ uri&nbsp;&nbsp;
78
88
  </div>
79
89
  </div>
80
90
 
@@ -20,6 +20,7 @@
20
20
  <div id="index">
21
21
  <h1 class="section-bar">Files</h1>
22
22
  <div id="index-entries">
23
+ <a href="files/ChangeLog.html">ChangeLog</a><br />
23
24
  <a href="files/MIT-LICENSE.html">MIT-LICENSE</a><br />
24
25
  <a href="files/README.html">README</a><br />
25
26
  <a href="files/TODO.html">TODO</a><br />
@@ -20,9 +20,10 @@
20
20
  <div id="index">
21
21
  <h1 class="section-bar">Methods</h1>
22
22
  <div id="index-entries">
23
- <a href="classes/ShortURL.html#M000002">rubyurl (ShortURL)</a><br />
23
+ <a href="classes/ShortURL.html#M000002">get_short_url (ShortURL)</a><br />
24
+ <a href="classes/ShortURL.html#M000003">rubyurl (ShortURL)</a><br />
24
25
  <a href="classes/ShortURL.html#M000001">shorten (ShortURL)</a><br />
25
- <a href="classes/ShortURL.html#M000003">tinyurl (ShortURL)</a><br />
26
+ <a href="classes/ShortURL.html#M000004">tinyurl (ShortURL)</a><br />
26
27
  </div>
27
28
  </div>
28
29
  </body>
data/lib/shorturl.rb CHANGED
@@ -1,8 +1,15 @@
1
+ # shorturl.rb
2
+ #
3
+ # Created by Vincent Foley on 2005-06-02
4
+ #
5
+
1
6
  require "net/http"
2
7
  require "cgi"
8
+ require "uri"
3
9
 
4
- class ShortURL
5
10
 
11
+ class ShortURL
12
+
6
13
  # Main method of ShortURL, its usage is quite simple, just give a
7
14
  # url to shorten and an optional service. If no service is
8
15
  # selected, RubyURL.com will be used. An invalid service symbol
@@ -18,27 +25,45 @@ class ShortURL
18
25
  # ShortURL.shorten("http://mypage.com", :tinyurl)
19
26
  def self.shorten(url, service = :rubyurl)
20
27
  case service
21
- when :rubyurl then self.new.rubyurl(url)
22
- when :tinyurl then self.new.tinyurl(url)
28
+ when :rubyurl then rubyurl(url)
29
+ when :tinyurl then tinyurl(url)
23
30
  else raise ArgumentError, "Invalid service"
24
31
  end
25
32
  end
26
33
 
34
+ # Abstract method to shorten a URL. Respects the DRY principle. In
35
+ # case of a network-related exception, return +nil+.
36
+ #
37
+ # Parameters:
38
+ # hostname: hostname of the service
39
+ # code: expected return code (200, 302, etc.) Converted to a string
40
+ # action: string representing the action to be executed on the HTTP object with +instance_eval+
41
+ # block: block of code which scrapes the HTML and returns the shortened URL. Takes one parameter.
42
+ def self.get_short_url(hostname, code, action, &block) # :yields: html_body
43
+ begin
44
+ Net::HTTP.start(hostname, 80) { |http|
45
+ response = http.instance_eval(action)
46
+
47
+ if response.code == code.to_s
48
+ block.call(response.read_body)
49
+ end
50
+ }
51
+ rescue SocketError, Net::HTTPExceptions => e
52
+ return nil
53
+ end
54
+ end
55
+
56
+
27
57
  # Shortens an URL with RubyURL.com. Since RubyURL uses a
28
58
  # redirection (code 302), instead of using HTTP#post, we send our
29
59
  # data directly to the _create_ action. The response is a small
30
60
  # HTML line which contains our shortened URL plus
31
61
  # <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
62
+ def self.rubyurl(url)
63
+ get_short_url("rubyurl.com", 302,
64
+ "get('/rubyurl/create?rubyurl[website_url]=#{CGI.escape(url)}')") { |body|
65
+ short_url = URI.extract(body)[0].gsub("rubyurl/show/", "")
66
+ return short_url
42
67
  }
43
68
  end
44
69
 
@@ -47,17 +72,14 @@ class ShortURL
47
72
  # <em>create.php</em> action with our URL as a parameter. We then
48
73
  # read the HTML code, split it into an array, find an element that
49
74
  # 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
75
+ def self.tinyurl(url)
76
+ get_short_url("tinyurl.com", 200,
77
+ "post('/create.php', 'url=#{url}')") { |body|
78
+ line = body.split("\n").find { |l| l =~ /hidden name=tinyurl/ }
79
+ short_url = URI.extract(line)[0]
80
+ return short_url
61
81
  }
62
82
  end
83
+
84
+ private_class_method :get_short_url, :tinyurl, :rubyurl
63
85
  end
data/test/tc_shorturl.rb CHANGED
@@ -8,6 +8,13 @@ $:.unshift($test_lib_dir)
8
8
  require "test/unit"
9
9
  require "shorturl"
10
10
 
11
+ # Make +get_short_url+ public so we can test it
12
+ class ShortURLModified < ShortURL
13
+ public_class_method :get_short_url
14
+ end
15
+
16
+
17
+
11
18
  class TestShortURL < Test::Unit::TestCase
12
19
  def setup
13
20
  @url = "http://darkhost.mine.nu:81/~vince/rails/tutorial.html"
@@ -19,4 +26,35 @@ class TestShortURL < Test::Unit::TestCase
19
26
  assert ShortURL.shorten(@url, :tinyurl) == "http://tinyurl.com/9mop8"
20
27
  assert_raise(ArgumentError) { ShortURL.shorten(@url, :foobar) }
21
28
  end
29
+
30
+ def test_get_short_url
31
+ post_string = "post('/create.php', 'url=#@url')"
32
+ assert_raise(NoMethodError) { ShortURL.get_short_url(nil, nil, nil) {} }
33
+
34
+ assert_nothing_raised do
35
+ ShortURLModified.get_short_url("tinyurl.com",
36
+ 200,
37
+ post_string) { |body| }
38
+ end
39
+
40
+ assert_nil(ShortURLModified.get_short_url("askjldakjsbfk",
41
+ 200,
42
+ "") { |body| } )
43
+
44
+ assert_nil(ShortURLModified.get_short_url("tinyurl.com",
45
+ 404,
46
+ post_string) { |body| } )
47
+
48
+ assert_nil(ShortURLModified.get_short_url("tinyurl.com",
49
+ 200,
50
+ "post('/foobar.php', nil)") { |body| } )
51
+ end
52
+
53
+ def test_rubyurl
54
+ assert_raise(NoMethodError) { ShortURL.rubyurl(@url) }
55
+ end
56
+
57
+ def test_tinyurl
58
+ assert_raise(NoMethodError) { ShortURL.tinyurl(@url) }
59
+ end
22
60
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.10
3
3
  specification_version: 1
4
4
  name: shorturl
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.0.1
7
- date: 2005-06-01
6
+ version: 0.1.0
7
+ date: 2005-06-02
8
8
  summary: Shortens URLs using services such as TinyURL and RubyURL
9
9
  require_paths:
10
10
  - lib
@@ -37,6 +37,7 @@ files:
37
37
  - doc/index.html
38
38
  - doc/rdoc-style.css
39
39
  - doc/classes/ShortURL.html
40
+ - doc/files/ChangeLog.html
40
41
  - doc/files/lib
41
42
  - doc/files/MIT-LICENSE.html
42
43
  - doc/files/README.html
@@ -47,6 +48,7 @@ files:
47
48
  - README
48
49
  - TODO
49
50
  - MIT-LICENSE
51
+ - ChangeLog
50
52
  test_files:
51
53
  - test/ts_all.rb
52
54
  rdoc_options:
@@ -56,10 +58,12 @@ rdoc_options:
56
58
  - README
57
59
  - "-S"
58
60
  - "-N"
61
+ - "--all"
59
62
  extra_rdoc_files:
60
63
  - README
61
64
  - TODO
62
65
  - MIT-LICENSE
66
+ - ChangeLog
63
67
  executables: []
64
68
  extensions: []
65
69
  requirements: []