shorturl 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog CHANGED
@@ -1,3 +1,6 @@
1
+ 0.3.0:
2
+ - Added linktrim.com and shorterlink.com
3
+ - Replaced get_short_url and all the private class methods in favor of a Service class, which makes things clearer
1
4
  0.2.1:
2
5
  - Added makeashorterlink.com and skinnylink.com
3
6
  - Refactored get_short_url
data/README CHANGED
@@ -5,8 +5,12 @@ ShortURL is a very simple library to use URL shortening services such as
5
5
  TinyURL or RubyURL.
6
6
 
7
7
  == Installation
8
+ ShortURL is available as a gem, installation is just like any other gem:
8
9
  <tt>$ gem install -r shorturl</tt>
9
10
 
11
+ You can also download the tar.bz2 archive at
12
+ http://rubyforge.org/frs/?group_id=732 if you do not want to use the gem.
13
+
10
14
  == Usage:
11
15
  call-seq:
12
16
  require "shorturl"
@@ -21,6 +25,12 @@ The second parameter represents the service you want to use. These are:
21
25
  - <tt>:metamark</tt>
22
26
  - <tt>:makeashorterlink</tt>
23
27
  - <tt>:skinnylink</tt>
28
+ - <tt>:linktrim</tt>
29
+ - <tt>:shorterlink</tt>
30
+
31
+ Note that you can use <tt>ShortURL.valid_services</tt> to obtain a
32
+ list of the valid services (in case I forget to update the
33
+ documentation)
24
34
 
25
35
  == Thanks
26
36
  - Marcel Molina Jr., Devin Mullins for some ideas
data/TODO CHANGED
@@ -1,5 +1,4 @@
1
1
  = Todo
2
2
 
3
- * Catch exceptions
4
- * More tests
5
- * More robust HTML scraping
3
+ * Make services an instance of a Service class
4
+ * Check out WWW::Mecanize
@@ -0,0 +1,238 @@
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: Service</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">Service</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="#M000004">call</a>&nbsp;&nbsp;
90
+ <a href="#M000003">new</a>&nbsp;&nbsp;
91
+ </div>
92
+ </div>
93
+
94
+ </div>
95
+
96
+
97
+ <!-- if includes -->
98
+
99
+ <div id="section">
100
+
101
+
102
+
103
+
104
+
105
+ <div id="attribute-list">
106
+ <h3 class="section-bar">Attributes</h3>
107
+
108
+ <div class="name-list">
109
+ <table>
110
+ <tr class="top-aligned-row context-row">
111
+ <td class="context-item-name">action</td>
112
+ <td class="context-item-value">&nbsp;[RW]&nbsp;</td>
113
+ <td class="context-item-desc"></td>
114
+ </tr>
115
+ <tr class="top-aligned-row context-row">
116
+ <td class="context-item-name">block</td>
117
+ <td class="context-item-value">&nbsp;[RW]&nbsp;</td>
118
+ <td class="context-item-desc"></td>
119
+ </tr>
120
+ <tr class="top-aligned-row context-row">
121
+ <td class="context-item-name">code</td>
122
+ <td class="context-item-value">&nbsp;[RW]&nbsp;</td>
123
+ <td class="context-item-desc"></td>
124
+ </tr>
125
+ <tr class="top-aligned-row context-row">
126
+ <td class="context-item-name">field</td>
127
+ <td class="context-item-value">&nbsp;[RW]&nbsp;</td>
128
+ <td class="context-item-desc"></td>
129
+ </tr>
130
+ <tr class="top-aligned-row context-row">
131
+ <td class="context-item-name">method</td>
132
+ <td class="context-item-value">&nbsp;[RW]&nbsp;</td>
133
+ <td class="context-item-desc"></td>
134
+ </tr>
135
+ <tr class="top-aligned-row context-row">
136
+ <td class="context-item-name">port</td>
137
+ <td class="context-item-value">&nbsp;[RW]&nbsp;</td>
138
+ <td class="context-item-desc"></td>
139
+ </tr>
140
+ </table>
141
+ </div>
142
+ </div>
143
+
144
+
145
+
146
+ <!-- if method_list -->
147
+ <div id="methods">
148
+ <h3 class="section-bar">Public Class methods</h3>
149
+
150
+ <div id="method-M000003" class="method-detail">
151
+ <a name="M000003"></a>
152
+
153
+ <div class="method-heading">
154
+ <a href="#M000003" class="method-signature">
155
+ <span class="method-name">new</span><span class="method-args">(hostname) {|service| ...}</span>
156
+ </a>
157
+ </div>
158
+
159
+ <div class="method-description">
160
+ <p>
161
+ Intialize the service with a hostname (required parameter) and you can
162
+ override the default values for the HTTP port, expected HTTP return code,
163
+ the form method to use, the form action, the form field which contains the
164
+ long URL and the block of what to do with the HTML code you get.
165
+ </p>
166
+ <p><a class="source-toggle" href="#"
167
+ onclick="toggleCode('M000003-source');return false;">[Source]</a></p>
168
+ <div class="method-source-code" id="M000003-source">
169
+ <pre>
170
+ <span class="ruby-comment cmt"># File lib/shorturl.rb, line 18</span>
171
+ 18: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">hostname</span>) <span class="ruby-comment cmt"># :yield: service</span>
172
+ 19: <span class="ruby-ivar">@hostname</span> = <span class="ruby-identifier">hostname</span>
173
+ 20: <span class="ruby-ivar">@port</span> = <span class="ruby-value">80</span>
174
+ 21: <span class="ruby-ivar">@code</span> = <span class="ruby-value">200</span>
175
+ 22: <span class="ruby-ivar">@method</span> = <span class="ruby-identifier">:post</span>
176
+ 23: <span class="ruby-ivar">@action</span> = <span class="ruby-value str">&quot;/&quot;</span>
177
+ 24: <span class="ruby-ivar">@field</span> = <span class="ruby-value str">&quot;url&quot;</span>
178
+ 25: <span class="ruby-ivar">@block</span> = <span class="ruby-identifier">lambda</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">body</span><span class="ruby-operator">|</span> }
179
+ 26:
180
+ 27: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">block_given?</span>
181
+ 28: <span class="ruby-keyword kw">yield</span> <span class="ruby-keyword kw">self</span>
182
+ 29: <span class="ruby-keyword kw">end</span>
183
+ 30: <span class="ruby-keyword kw">end</span>
184
+ </pre>
185
+ </div>
186
+ </div>
187
+ </div>
188
+
189
+ <h3 class="section-bar">Public Instance methods</h3>
190
+
191
+ <div id="method-M000004" class="method-detail">
192
+ <a name="M000004"></a>
193
+
194
+ <div class="method-heading">
195
+ <a href="#M000004" class="method-signature">
196
+ <span class="method-name">call</span><span class="method-args">(url)</span>
197
+ </a>
198
+ </div>
199
+
200
+ <div class="method-description">
201
+ <p>
202
+ Now that our service is set up, call it with all the parameters to
203
+ (hopefully) return only the shortened URL.
204
+ </p>
205
+ <p><a class="source-toggle" href="#"
206
+ onclick="toggleCode('M000004-source');return false;">[Source]</a></p>
207
+ <div class="method-source-code" id="M000004-source">
208
+ <pre>
209
+ <span class="ruby-comment cmt"># File lib/shorturl.rb, line 34</span>
210
+ 34: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">call</span>(<span class="ruby-identifier">url</span>)
211
+ 35: <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-ivar">@hostname</span>, <span class="ruby-ivar">@port</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">http</span><span class="ruby-operator">|</span>
212
+ 36: <span class="ruby-identifier">response</span> = <span class="ruby-keyword kw">case</span> <span class="ruby-ivar">@method</span>
213
+ 37: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:post</span><span class="ruby-operator">:</span> <span class="ruby-identifier">http</span>.<span class="ruby-identifier">send</span>(<span class="ruby-ivar">@method</span>, <span class="ruby-ivar">@action</span>, <span class="ruby-node">&quot;#@field=#{url}&quot;</span>)
214
+ 38: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:get</span><span class="ruby-operator">:</span> <span class="ruby-identifier">http</span>.<span class="ruby-identifier">send</span>(<span class="ruby-ivar">@method</span>, <span class="ruby-node">&quot;#@action?#@field=#{CGI.escape(url)}&quot;</span>)
215
+ 39: <span class="ruby-keyword kw">end</span>
216
+ 40: <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-ivar">@code</span>.<span class="ruby-identifier">to_s</span>
217
+ 41: <span class="ruby-ivar">@block</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">response</span>.<span class="ruby-identifier">read_body</span>)
218
+ 42: <span class="ruby-keyword kw">end</span>
219
+ 43: }
220
+ 44: <span class="ruby-keyword kw">end</span>
221
+ </pre>
222
+ </div>
223
+ </div>
224
+ </div>
225
+
226
+
227
+ </div>
228
+
229
+
230
+ </div>
231
+
232
+
233
+ <div id="validator-badges">
234
+ <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
235
+ </div>
236
+
237
+ </body>
238
+ </html>
@@ -86,14 +86,7 @@
86
86
  <h3 class="section-bar">Methods</h3>
87
87
 
88
88
  <div class="name-list">
89
- <a href="#M000003">get_short_url</a>&nbsp;&nbsp;
90
- <a href="#M000009">makeashorterlink</a>&nbsp;&nbsp;
91
- <a href="#M000008">metamark</a>&nbsp;&nbsp;
92
- <a href="#M000004">rubyurl</a>&nbsp;&nbsp;
93
- <a href="#M000006">shorl</a>&nbsp;&nbsp;
94
89
  <a href="#M000002">shorten</a>&nbsp;&nbsp;
95
- <a href="#M000007">snipurl</a>&nbsp;&nbsp;
96
- <a href="#M000005">tinyurl</a>&nbsp;&nbsp;
97
90
  <a href="#M000001">valid_services</a>&nbsp;&nbsp;
98
91
  </div>
99
92
  </div>
@@ -116,131 +109,6 @@
116
109
  <div id="methods">
117
110
  <h3 class="section-bar">Public Class methods</h3>
118
111
 
119
- <div id="method-M000009" class="method-detail">
120
- <a name="M000009"></a>
121
-
122
- <div class="method-heading">
123
- <a href="#M000009" class="method-signature">
124
- <span class="method-name">makeashorterlink</span><span class="method-args">(url)</span>
125
- </a>
126
- </div>
127
-
128
- <div class="method-description">
129
- <p>
130
- Shorten an URL with makeashortlink.com.
131
- </p>
132
- <p><a class="source-toggle" href="#"
133
- onclick="toggleCode('M000009-source');return false;">[Source]</a></p>
134
- <div class="method-source-code" id="M000009-source">
135
- <pre>
136
- <span class="ruby-comment cmt"># File lib/shorturl.rb, line 123</span>
137
- 123: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">makeashorterlink</span>(<span class="ruby-identifier">url</span>)
138
- 124: <span class="ruby-identifier">get_short_url</span>(<span class="ruby-value str">&quot;makeashorterlink.com&quot;</span>, <span class="ruby-value">200</span>,
139
- 125: <span class="ruby-node">&quot;post('/index.php', 'url=#{url}')&quot;</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">body</span><span class="ruby-operator">|</span>
140
- 126: <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-identifier">grep</span>(<span class="ruby-regexp re">/makeashorter/</span>)[<span class="ruby-value">0</span>]
141
- 127: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">short_url</span>
142
- 128: }
143
- 129: <span class="ruby-keyword kw">end</span>
144
- </pre>
145
- </div>
146
- </div>
147
- </div>
148
-
149
- <div id="method-M000008" class="method-detail">
150
- <a name="M000008"></a>
151
-
152
- <div class="method-heading">
153
- <a href="#M000008" class="method-signature">
154
- <span class="method-name">metamark</span><span class="method-args">(url)</span>
155
- </a>
156
- </div>
157
-
158
- <div class="method-description">
159
- <p>
160
- Shorten an URL with metamark.com. Same technique as TinyURL
161
- </p>
162
- <p><a class="source-toggle" href="#"
163
- onclick="toggleCode('M000008-source');return false;">[Source]</a></p>
164
- <div class="method-source-code" id="M000008-source">
165
- <pre>
166
- <span class="ruby-comment cmt"># File lib/shorturl.rb, line 113</span>
167
- 113: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">metamark</span>(<span class="ruby-identifier">url</span>)
168
- 114: <span class="ruby-identifier">get_short_url</span>(<span class="ruby-value str">&quot;metamark.net&quot;</span>, <span class="ruby-value">200</span>,
169
- 115: <span class="ruby-node">&quot;post('/add', 'long_url=#{url}')&quot;</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">body</span><span class="ruby-operator">|</span>
170
- 116: <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">/xrl\.us/</span> }
171
- 117: <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>]
172
- 118: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">short_url</span>
173
- 119: }
174
- 120: <span class="ruby-keyword kw">end</span>
175
- </pre>
176
- </div>
177
- </div>
178
- </div>
179
-
180
- <div id="method-M000004" class="method-detail">
181
- <a name="M000004"></a>
182
-
183
- <div class="method-heading">
184
- <a href="#M000004" class="method-signature">
185
- <span class="method-name">rubyurl</span><span class="method-args">(url)</span>
186
- </a>
187
- </div>
188
-
189
- <div class="method-description">
190
- <p>
191
- Shortens an URL with RubyURL.com. Since RubyURL uses a redirection (code
192
- 302), instead of using HTTP#post, we send our data directly to the
193
- <em>create</em> action. The response is a small HTML line which contains
194
- our shortened URL plus <em>/rubyurl/show</em>, so we manually remove that
195
- with gsub.
196
- </p>
197
- <p><a class="source-toggle" href="#"
198
- onclick="toggleCode('M000004-source');return false;">[Source]</a></p>
199
- <div class="method-source-code" id="M000004-source">
200
- <pre>
201
- <span class="ruby-comment cmt"># File lib/shorturl.rb, line 71</span>
202
- 71: <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>)
203
- 72: <span class="ruby-identifier">get_short_url</span>(<span class="ruby-value str">&quot;rubyurl.com&quot;</span>, <span class="ruby-value">302</span>,
204
- 73: <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>
205
- 74: <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>)
206
- 75: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">short_url</span>
207
- 76: }
208
- 77: <span class="ruby-keyword kw">end</span>
209
- </pre>
210
- </div>
211
- </div>
212
- </div>
213
-
214
- <div id="method-M000006" class="method-detail">
215
- <a name="M000006"></a>
216
-
217
- <div class="method-heading">
218
- <a href="#M000006" class="method-signature">
219
- <span class="method-name">shorl</span><span class="method-args">(url)</span>
220
- </a>
221
- </div>
222
-
223
- <div class="method-description">
224
- <p>
225
- Shorten an URL through with shorl.com. Same technique as TinyURL
226
- </p>
227
- <p><a class="source-toggle" href="#"
228
- onclick="toggleCode('M000006-source');return false;">[Source]</a></p>
229
- <div class="method-source-code" id="M000006-source">
230
- <pre>
231
- <span class="ruby-comment cmt"># File lib/shorturl.rb, line 94</span>
232
- 94: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">shorl</span>(<span class="ruby-identifier">url</span>)
233
- 95: <span class="ruby-identifier">get_short_url</span>(<span class="ruby-value str">&quot;shorl.com&quot;</span>, <span class="ruby-value">200</span>,
234
- 96: <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>
235
- 97: <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">2</span>]
236
- 98: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">short_url</span>
237
- 99: }
238
- 100: <span class="ruby-keyword kw">end</span>
239
- </pre>
240
- </div>
241
- </div>
242
- </div>
243
-
244
112
  <div id="method-M000002" class="method-detail">
245
113
  <a name="M000002"></a>
246
114
 
@@ -260,14 +128,35 @@ an ArgumentError exception
260
128
  <p>
261
129
  Valid <tt>service</tt> values:
262
130
  </p>
263
- <table>
264
- <tr><td valign="top"><tt>:rubyurl</tt>:</td><td>uses RubyURL.com
131
+ <ul>
132
+ <li><tt>:rubyurl</tt>
133
+
134
+ </li>
135
+ <li><tt>:tinyurl</tt>
136
+
137
+ </li>
138
+ <li><tt>:shorl</tt>
139
+
140
+ </li>
141
+ <li><tt>:snipurl</tt>
142
+
143
+ </li>
144
+ <li><tt>:metamark</tt>
145
+
146
+ </li>
147
+ <li><tt>:makeashorterlink</tt>
148
+
149
+ </li>
150
+ <li><tt>:skinnylink</tt>
151
+
152
+ </li>
153
+ <li><tt>:linktrim</tt>
265
154
 
266
- </td></tr>
267
- <tr><td valign="top"><tt>:tinyurl</tt>:</td><td>uses TinyURL.com
155
+ </li>
156
+ <li><tt>:shorterlink</tt>
268
157
 
269
- </td></tr>
270
- </table>
158
+ </li>
159
+ </ul>
271
160
  <p>
272
161
  call-seq:
273
162
  </p>
@@ -279,79 +168,14 @@ call-seq:
279
168
  onclick="toggleCode('M000002-source');return false;">[Source]</a></p>
280
169
  <div class="method-source-code" id="M000002-source">
281
170
  <pre>
282
- <span class="ruby-comment cmt"># File lib/shorturl.rb, line 35</span>
283
- 35: <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>)
284
- 36: <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@@valid_services</span>.<span class="ruby-identifier">include?</span> <span class="ruby-identifier">service</span>
285
- 37: <span class="ruby-identifier">send</span>(<span class="ruby-identifier">service</span>, <span class="ruby-identifier">url</span>)
286
- 38: <span class="ruby-keyword kw">else</span>
287
- 39: <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-value str">&quot;Invalid service&quot;</span>
288
- 40: <span class="ruby-keyword kw">end</span>
289
- 41: <span class="ruby-keyword kw">end</span>
290
- </pre>
291
- </div>
292
- </div>
293
- </div>
294
-
295
- <div id="method-M000007" class="method-detail">
296
- <a name="M000007"></a>
297
-
298
- <div class="method-heading">
299
- <a href="#M000007" class="method-signature">
300
- <span class="method-name">snipurl</span><span class="method-args">(url)</span>
301
- </a>
302
- </div>
303
-
304
- <div class="method-description">
305
- <p>
306
- Shorten an URL with snipurl.com. Same technique as TinyURL
307
- </p>
308
- <p><a class="source-toggle" href="#"
309
- onclick="toggleCode('M000007-source');return false;">[Source]</a></p>
310
- <div class="method-source-code" id="M000007-source">
311
- <pre>
312
- <span class="ruby-comment cmt"># File lib/shorturl.rb, line 103</span>
313
- 103: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">snipurl</span>(<span class="ruby-identifier">url</span>)
314
- 104: <span class="ruby-identifier">get_short_url</span>(<span class="ruby-value str">&quot;snipurl.com&quot;</span>, <span class="ruby-value">200</span>,
315
- 105: <span class="ruby-node">&quot;post('/index.php', 'link=#{url}')&quot;</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">body</span><span class="ruby-operator">|</span>
316
- 106: <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">grep</span>(<span class="ruby-regexp re">/txt/</span>)[<span class="ruby-value">0</span>]
317
- 107: <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">1</span>][<span class="ruby-value">0</span><span class="ruby-operator">..</span><span class="ruby-value">-2</span>] <span class="ruby-comment cmt"># Remove trailing '</span>
318
- 108: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">short_url</span>
319
- 109: }
320
- 110: <span class="ruby-keyword kw">end</span>
321
- </pre>
322
- </div>
323
- </div>
324
- </div>
325
-
326
- <div id="method-M000005" class="method-detail">
327
- <a name="M000005"></a>
328
-
329
- <div class="method-heading">
330
- <a href="#M000005" class="method-signature">
331
- <span class="method-name">tinyurl</span><span class="method-args">(url)</span>
332
- </a>
333
- </div>
334
-
335
- <div class="method-description">
336
- <p>
337
- Shortens an URL with TinyURL.com. We post a request to the
338
- <em>create.php</em> action with our URL as a parameter. We then read the
339
- HTML code, split it into an array, find an element that contains our
340
- shortened URL and extract it.
341
- </p>
342
- <p><a class="source-toggle" href="#"
343
- onclick="toggleCode('M000005-source');return false;">[Source]</a></p>
344
- <div class="method-source-code" id="M000005-source">
345
- <pre>
346
- <span class="ruby-comment cmt"># File lib/shorturl.rb, line 84</span>
347
- 84: <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>)
348
- 85: <span class="ruby-identifier">get_short_url</span>(<span class="ruby-value str">&quot;tinyurl.com&quot;</span>, <span class="ruby-value">200</span>,
349
- 86: <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>
350
- 87: <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> }
351
- 88: <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>]
352
- 89: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">short_url</span>
353
- 90: }
354
- 91: <span class="ruby-keyword kw">end</span>
171
+ <span class="ruby-comment cmt"># File lib/shorturl.rb, line 137</span>
172
+ 137: <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>)
173
+ 138: <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@@valid_services</span>.<span class="ruby-identifier">include?</span> <span class="ruby-identifier">service</span>
174
+ 139: <span class="ruby-ivar">@@services</span>[<span class="ruby-identifier">service</span>].<span class="ruby-identifier">call</span>(<span class="ruby-identifier">url</span>)
175
+ 140: <span class="ruby-keyword kw">else</span>
176
+ 141: <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-value str">&quot;Invalid service&quot;</span>
177
+ 142: <span class="ruby-keyword kw">end</span>
178
+ 143: <span class="ruby-keyword kw">end</span>
355
179
  </pre>
356
180
  </div>
357
181
  </div>
@@ -374,58 +198,10 @@ Returns @@<a href="ShortURL.html#M000001">valid_services</a>
374
198
  onclick="toggleCode('M000001-source');return false;">[Source]</a></p>
375
199
  <div class="method-source-code" id="M000001-source">
376
200
  <pre>
377
- <span class="ruby-comment cmt"># File lib/shorturl.rb, line 18</span>
378
- 18: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">valid_services</span>
379
- 19: <span class="ruby-ivar">@@valid_services</span>
380
- 20: <span class="ruby-keyword kw">end</span>
381
- </pre>
382
- </div>
383
- </div>
384
- </div>
385
-
386
- <h3 class="section-bar">Private Class methods</h3>
387
-
388
- <div id="method-M000003" class="method-detail">
389
- <a name="M000003"></a>
390
-
391
- <div class="method-heading">
392
- <a href="#M000003" class="method-signature">
393
- <span class="method-name">get_short_url</span><span class="method-args">(hostname, code, action) {|html_body| ...}</span>
394
- </a>
395
- </div>
396
-
397
- <div class="method-description">
398
- <p>
399
- Abstract method to shorten an URL. Respects the DRY principle. In case of a
400
- network-related exception, return <tt>nil</tt>.
401
- </p>
402
- <p>
403
- Parameters:
404
- </p>
405
- <pre>
406
- hostname: hostname of the service
407
- code: expected return code (200, 302, etc.) Converted to a string
408
- action: string representing the action to be executed on the HTTP object with +instance_eval+
409
- block: block of code which scrapes the HTML and returns the shortened URL. Takes one parameter.
410
- </pre>
411
- <p><a class="source-toggle" href="#"
412
- onclick="toggleCode('M000003-source');return false;">[Source]</a></p>
413
- <div class="method-source-code" id="M000003-source">
414
- <pre>
415
- <span class="ruby-comment cmt"># File lib/shorturl.rb, line 51</span>
416
- 51: <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>
417
- 52: <span class="ruby-keyword kw">begin</span>
418
- 53: <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>
419
- 54: <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>)
420
- 55:
421
- 56: <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>
422
- 57: <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>)
423
- 58: <span class="ruby-keyword kw">end</span>
424
- 59: }
425
- 60: <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>
426
- 61: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">nil</span>
427
- 62: <span class="ruby-keyword kw">end</span>
428
- 63: <span class="ruby-keyword kw">end</span>
201
+ <span class="ruby-comment cmt"># File lib/shorturl.rb, line 113</span>
202
+ 113: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">valid_services</span>
203
+ 114: <span class="ruby-ivar">@@valid_services</span>
204
+ 115: <span class="ruby-keyword kw">end</span>
429
205
  </pre>
430
206
  </div>
431
207
  </div>
data/doc/created.rid CHANGED
@@ -1 +1 @@
1
- Sat Jun 04 15:14:15 EDT 2005
1
+ Sun Jun 05 15:37:07 EDT 2005
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Sat Jun 04 15:06:47 EDT 2005</td>
59
+ <td>Sun Jun 05 15:31:15 EDT 2005</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -70,10 +70,17 @@
70
70
 
71
71
  <div id="description">
72
72
  <p>
73
+ 0.3.0:
74
+ </p>
75
+ <pre>
76
+ - Added linktrim.com and shorterlink.com
77
+ - Replaced get_short_url and all the private class methods in favor of a Service class, which makes things clearer
78
+ </pre>
79
+ <p>
73
80
  0.2.1:
74
81
  </p>
75
82
  <pre>
76
- - Added makeashorterlink.com
83
+ - Added makeashorterlink.com and skinnylink.com
77
84
  - Refactored get_short_url
78
85
  </pre>
79
86
  <p>
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Sat Jun 04 15:07:27 EDT 2005</td>
59
+ <td>Sun Jun 05 15:37:02 EDT 2005</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -77,7 +77,13 @@ use URL shortening services such as TinyURL or RubyURL.
77
77
  </p>
78
78
  <h2>Installation</h2>
79
79
  <p>
80
- <tt>$ gem install -r shorturl</tt>
80
+ <a href="../classes/ShortURL.html">ShortURL</a> is available as a gem,
81
+ installation is just like any other gem: <tt>$ gem install -r shorturl</tt>
82
+ </p>
83
+ <p>
84
+ You can also download the tar.bz2 archive at <a
85
+ href="http://rubyforge.org/frs/?group_id=732">rubyforge.org/frs/?group_id=732</a>
86
+ if you do not want to use the gem.
81
87
  </p>
82
88
  <h2>Usage:</h2>
83
89
  <p>
@@ -109,8 +115,23 @@ The second parameter represents the service you want to use. These are:
109
115
  </li>
110
116
  <li><tt>:makeashorterlink</tt>
111
117
 
118
+ </li>
119
+ <li><tt>:skinnylink</tt>
120
+
121
+ </li>
122
+ <li><tt>:linktrim</tt>
123
+
124
+ </li>
125
+ <li><tt>:shorterlink</tt>
126
+
112
127
  </li>
113
128
  </ul>
129
+ <p>
130
+ Note that you can use <tt><a
131
+ href="../classes/ShortURL.html#M000001">ShortURL.valid_services</a></tt> to
132
+ obtain a list of the valid services (in case I forget to update the
133
+ documentation)
134
+ </p>
114
135
  <h2>Thanks</h2>
115
136
  <ul>
116
137
  <li>Marcel Molina Jr., Devin Mullins for some ideas
data/doc/files/TODO.html CHANGED
@@ -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:35:56 EDT 2005</td>
59
+ <td>Sat Jun 04 23:12:24 EDT 2005</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -71,13 +71,11 @@
71
71
  <div id="description">
72
72
  <h1>Todo</h1>
73
73
  <ul>
74
- <li>Catch exceptions
74
+ <li>Make services an instance of a <a
75
+ href="../classes/Service.html">Service</a> class
75
76
 
76
77
  </li>
77
- <li>More tests
78
-
79
- </li>
80
- <li>More robust HTML scraping
78
+ <li>Check out WWW::Mecanize
81
79
 
82
80
  </li>
83
81
  </ul>
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Sat Jun 04 15:05:31 EDT 2005</td>
59
+ <td>Sun Jun 05 15:32:04 EDT 2005</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -20,6 +20,7 @@
20
20
  <div id="index">
21
21
  <h1 class="section-bar">Classes</h1>
22
22
  <div id="index-entries">
23
+ <a href="classes/Service.html">Service</a><br />
23
24
  <a href="classes/ShortURL.html">ShortURL</a><br />
24
25
  </div>
25
26
  </div>
@@ -20,14 +20,9 @@
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#M000003">get_short_url (ShortURL)</a><br />
24
- <a href="classes/ShortURL.html#M000009">makeashorterlink (ShortURL)</a><br />
25
- <a href="classes/ShortURL.html#M000008">metamark (ShortURL)</a><br />
26
- <a href="classes/ShortURL.html#M000004">rubyurl (ShortURL)</a><br />
27
- <a href="classes/ShortURL.html#M000006">shorl (ShortURL)</a><br />
23
+ <a href="classes/Service.html#M000004">call (Service)</a><br />
24
+ <a href="classes/Service.html#M000003">new (Service)</a><br />
28
25
  <a href="classes/ShortURL.html#M000002">shorten (ShortURL)</a><br />
29
- <a href="classes/ShortURL.html#M000007">snipurl (ShortURL)</a><br />
30
- <a href="classes/ShortURL.html#M000005">tinyurl (ShortURL)</a><br />
31
26
  <a href="classes/ShortURL.html#M000001">valid_services (ShortURL)</a><br />
32
27
  </div>
33
28
  </div>
data/lib/shorturl.rb CHANGED
@@ -7,12 +7,107 @@ require "net/http"
7
7
  require "cgi"
8
8
  require "uri"
9
9
 
10
+ class Service
11
+ attr_accessor :port, :code, :method, :action, :field, :block
12
+
13
+ # Intialize the service with a hostname (required parameter) and you
14
+ # can override the default values for the HTTP port, expected HTTP
15
+ # return code, the form method to use, the form action, the form
16
+ # field which contains the long URL and the block of what to do with
17
+ # the HTML code you get.
18
+ def initialize(hostname) # :yield: service
19
+ @hostname = hostname
20
+ @port = 80
21
+ @code = 200
22
+ @method = :post
23
+ @action = "/"
24
+ @field = "url"
25
+ @block = lambda { |body| }
26
+
27
+ if block_given?
28
+ yield self
29
+ end
30
+ end
31
+
32
+ # Now that our service is set up, call it with all the parameters to
33
+ # (hopefully) return only the shortened URL.
34
+ def call(url)
35
+ Net::HTTP.start(@hostname, @port) { |http|
36
+ response = case @method
37
+ when :post: http.send(@method, @action, "#@field=#{url}")
38
+ when :get: http.send(@method, "#@action?#@field=#{CGI.escape(url)}")
39
+ end
40
+ if response.code == @code.to_s
41
+ @block.call(response.read_body)
42
+ end
43
+ }
44
+ end
45
+ end
10
46
 
11
47
  class ShortURL
12
-
48
+
49
+ @@services = {
50
+ :rubyurl => Service.new("rubyurl.com") { |s|
51
+ s.code = 302
52
+ s.method = :get
53
+ s.action = "/rubyurl/create"
54
+ s.field = "rubyurl[website_url]"
55
+ s.block = lambda { |body| URI.extract(body)[0].gsub("rubyurl/show/", "") }
56
+ },
57
+
58
+ :tinyurl => Service.new("tinyurl.com") { |s|
59
+ s.action = "/create.php"
60
+ s.block = lambda { |body|
61
+ URI.extract(body).grep(/tinyurl/)[-1]
62
+ }
63
+ },
64
+
65
+ :shorl => Service.new("shorl.com") { |s|
66
+ s.action = "/create.php"
67
+ s.block = lambda { |body| URI.extract(body)[2] }
68
+ },
69
+
70
+ :snipurl => Service.new("snipurl.com") { |s|
71
+ s.action = "/index.php"
72
+ s.field = "link"
73
+ s.block = lambda { |body|
74
+ line = body.split("\n").grep(/txt/)[0]
75
+ short_url = URI.extract(line)[1][0..-2] # Remove trailing '
76
+ }
77
+ },
78
+
79
+ :metamark => Service.new("metamark.net") { |s|
80
+ s.action = "/add"
81
+ s.field = "long_url"
82
+ s.block = lambda { |body|
83
+ URI.extract(body).grep(/xrl.us/)[0]
84
+ }
85
+ },
86
+
87
+ :makeashorterlink => Service.new("makeashorterlink.com") { |s|
88
+ s.action = "/index.php"
89
+ s.block = lambda { |body| URI.extract(body).grep(/makeashorterlink/)[0] }
90
+ },
91
+
92
+ :skinnylink => Service.new("skinnylink.com") { |s|
93
+ s.block = lambda { |body| URI.extract(body).grep(/skinnylink/)[0] }
94
+ },
95
+
96
+ :linktrim => Service.new("linktrim.com") { |s|
97
+ s.method = :get
98
+ s.action = "/lt/generate"
99
+ s.block = lambda { |body| URI.extract(body).grep(/\/linktrim/)[1] }
100
+ },
101
+
102
+ :shorterlink => Service.new("shorterlink.com") { |s|
103
+ s.method = :get
104
+ s.action = "/add_url.html"
105
+ s.block = lambda { |body| URI.extract(body).grep(/shorterlink/)[0] }
106
+ }
107
+ }
13
108
  # Array containing symbols representing all the implemented URL
14
109
  # shortening services
15
- @@valid_services = [:rubyurl, :tinyurl, :shorl, :snipurl, :metamark, :makeashorterlink, :skinnylink]
110
+ @@valid_services = @@services.keys
16
111
 
17
112
  # Returns @@valid_services
18
113
  def self.valid_services
@@ -26,116 +121,24 @@ class ShortURL
26
121
  #
27
122
  # Valid +service+ values:
28
123
  #
29
- # <tt>:rubyurl</tt>:: uses RubyURL.com
30
- # <tt>:tinyurl</tt>:: uses TinyURL.com
124
+ # * <tt>:rubyurl</tt>
125
+ # * <tt>:tinyurl</tt>
126
+ # * <tt>:shorl</tt>
127
+ # * <tt>:snipurl</tt>
128
+ # * <tt>:metamark</tt>
129
+ # * <tt>:makeashorterlink</tt>
130
+ # * <tt>:skinnylink</tt>
131
+ # * <tt>:linktrim</tt>
132
+ # * <tt>:shorterlink</tt>
31
133
  #
32
134
  # call-seq:
33
135
  # ShortURL.shorten("http://mypage.com") => Uses RubyURL
34
136
  # ShortURL.shorten("http://mypage.com", :tinyurl)
35
137
  def self.shorten(url, service = :rubyurl)
36
138
  if @@valid_services.include? service
37
- send(service, url)
139
+ @@services[service].call(url)
38
140
  else
39
141
  raise ArgumentError, "Invalid service"
40
142
  end
41
143
  end
42
-
43
- # Abstract method to shorten an URL. Respects the DRY principle. In
44
- # case of a network-related exception, return +nil+.
45
- #
46
- # Parameters:
47
- # hostname: hostname of the service
48
- # code: expected return code (200, 302, etc.) Converted to a string
49
- # action: string representing the action to be executed on the HTTP object with +instance_eval+
50
- # block: block of code which scrapes the HTML and returns the shortened URL. Takes one parameter.
51
- def self.get_short_url(hostname, code, action, &block) # :yields: html_body
52
- begin
53
- Net::HTTP.start(hostname, 80) { |http|
54
- response = http.instance_eval(action)
55
-
56
- if response.code == code.to_s
57
- block.call(response.read_body)
58
- end
59
- }
60
- rescue SocketError, Net::HTTPExceptions => e
61
- return nil
62
- end
63
- end
64
-
65
-
66
- # Shortens an URL with RubyURL.com. Since RubyURL uses a
67
- # redirection (code 302), instead of using HTTP#post, we send our
68
- # data directly to the _create_ action. The response is a small
69
- # HTML line which contains our shortened URL plus
70
- # <em>/rubyurl/show</em>, so we manually remove that with gsub.
71
- def self.rubyurl(url)
72
- get_short_url("rubyurl.com", 302,
73
- "get('/rubyurl/create?rubyurl[website_url]=#{CGI.escape(url)}')") { |body|
74
- short_url = URI.extract(body)[0].gsub("rubyurl/show/", "")
75
- return short_url
76
- }
77
- end
78
-
79
-
80
- # Shortens an URL with TinyURL.com. We post a request to the
81
- # <em>create.php</em> action with our URL as a parameter. We then
82
- # read the HTML code, split it into an array, find an element that
83
- # contains our shortened URL and extract it.
84
- def self.tinyurl(url)
85
- get_short_url("tinyurl.com", 200,
86
- "post('/create.php', 'url=#{url}')") { |body|
87
- line = body.split("\n").find { |l| l =~ /hidden name=tinyurl/ }
88
- short_url = URI.extract(line)[0]
89
- return short_url
90
- }
91
- end
92
-
93
- # Shorten an URL through with shorl.com. Same technique as TinyURL
94
- def self.shorl(url)
95
- get_short_url("shorl.com", 200,
96
- "post('/create.php', 'url=#{url}')") { |body|
97
- short_url = URI.extract(body)[2]
98
- return short_url
99
- }
100
- end
101
-
102
- # Shorten an URL with snipurl.com. Same technique as TinyURL
103
- def self.snipurl(url)
104
- get_short_url("snipurl.com", 200,
105
- "post('/index.php', 'link=#{url}')") { |body|
106
- line = body.split("\n").grep(/txt/)[0]
107
- short_url = URI.extract(line)[1][0..-2] # Remove trailing '
108
- return short_url
109
- }
110
- end
111
-
112
- # Shorten an URL with metamark.com. Same technique as TinyURL
113
- def self.metamark(url)
114
- get_short_url("metamark.net", 200,
115
- "post('/add', 'long_url=#{url}')") { |body|
116
- line = body.split("\n").find { |l| l =~ /xrl\.us/ }
117
- short_url = URI.extract(line)[0]
118
- return short_url
119
- }
120
- end
121
-
122
- # Shorten an URL with makeashortlink.com.
123
- def self.makeashorterlink(url)
124
- get_short_url("makeashorterlink.com", 200,
125
- "post('/index.php', 'url=#{url}')") { |body|
126
- short_url = URI.extract(body).grep(/makeashorter/)[0]
127
- return short_url
128
- }
129
- end
130
-
131
- # Shorten an URL with skinnylink.com.
132
- def self.skinnylink(url)
133
- get_short_url("skinnylink.com", 200,
134
- "post('/', 'url=#{url}')") { |body|
135
- short_url = URI.extract(body).grep(/skinnylink/)[0]
136
- return short_url
137
- }
138
- end
139
-
140
- private_class_method(:get_short_url, *@@valid_services)
141
144
  end
@@ -0,0 +1,41 @@
1
+ # tc_ervicerb
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
+
12
+ class TestService < Test::Unit::TestCase
13
+
14
+ def test_call
15
+ service = Service.new("oasdasobf")
16
+ assert_raise(SocketError) { service.call(nil) }
17
+ end
18
+
19
+ def test_initialize
20
+ service = Service.new("rubyurl.com")
21
+ assert_equal(service.port, 80)
22
+ assert_equal(service.code, 200)
23
+ assert_equal(service.method, :post)
24
+ assert_equal(service.action, "/")
25
+ assert_equal(service.field, "url")
26
+
27
+ service = Service.new("rubyurl.com") { |s|
28
+ s.port = 8080
29
+ s.code = 302
30
+ s.method = :get
31
+ s.action = "/create.php"
32
+ s.field = "link"
33
+ }
34
+ assert_equal(service.port, 8080)
35
+ assert_equal(service.code, 302)
36
+ assert_equal(service.method, :get)
37
+ assert_equal(service.action, "/create.php")
38
+ assert_equal(service.field, "link")
39
+ end
40
+
41
+ end
data/test/tc_shorturl.rb CHANGED
@@ -8,12 +8,6 @@ $:.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
12
  class TestShortURL < Test::Unit::TestCase
19
13
  def setup
@@ -24,67 +18,14 @@ class TestShortURL < Test::Unit::TestCase
24
18
  assert ShortURL.shorten(@url) == "http://rubyurl.com/Q9ToW"
25
19
  assert ShortURL.shorten(@url, :rubyurl) == "http://rubyurl.com/Q9ToW"
26
20
  assert ShortURL.shorten(@url, :tinyurl) == "http://tinyurl.com/9mop8"
27
-
28
- # The short URL for shorl is never the same, so I just check to
29
- # see if the URL begins with their domain name
30
- assert ShortURL.shorten(@url, :shorl) =~ /^http:\/\/shorl.com\//
21
+ assert ShortURL.shorten(@url, :shorl) =~ /^http:\/\/shorl.com/ # Never the same URL
31
22
  assert ShortURL.shorten(@url, :snipurl) == "http://snipurl.com/fbkl"
32
23
  assert ShortURL.shorten(@url, :metamark) == "http://xrl.us/ga8c"
33
24
  assert ShortURL.shorten(@url, :makeashorterlink) == "http://makeashorterlink.com/?O1752235A"
34
25
  assert ShortURL.shorten(@url, :skinnylink) == "http://skinnylink.com?nqvcce"
26
+ assert ShortURL.shorten(@url, :linktrim) =~ /http:\/\/linktrim/ # Never the same URL
27
+ assert ShortURL.shorten(@url, :shorterlink) == "http://shorterlink.com/?PQD096"
35
28
 
36
29
  assert_raise(ArgumentError) { ShortURL.shorten(@url, :foobar) }
37
30
  end
38
-
39
- def test_get_short_url
40
- post_string = "post('/create.php', 'url=#@url')"
41
- assert_raise(NoMethodError) { ShortURL.get_short_url(nil, nil, nil) {} }
42
-
43
- assert_nothing_raised do
44
- ShortURLModified.get_short_url("tinyurl.com",
45
- 200,
46
- post_string) { |body| }
47
- end
48
-
49
- assert_nil(ShortURLModified.get_short_url("askjldakjsbfk",
50
- 200,
51
- "") { |body| } )
52
-
53
- assert_nil(ShortURLModified.get_short_url("tinyurl.com",
54
- 404,
55
- post_string) { |body| } )
56
-
57
- assert_nil(ShortURLModified.get_short_url("tinyurl.com",
58
- 200,
59
- "post('/foobar.php', nil)") { |body| } )
60
- end
61
-
62
- def test_rubyurl
63
- assert_raise(NoMethodError) { ShortURL.rubyurl(@url) }
64
- end
65
-
66
- def test_tinyurl
67
- assert_raise(NoMethodError) { ShortURL.tinyurl(@url) }
68
- end
69
-
70
- def test_shorl
71
- assert_raise(NoMethodError) { ShortURL.shorl(@url) }
72
- end
73
-
74
- def test_snipurl
75
- assert_raise(NoMethodError) { ShortURL.snipurl(@url) }
76
- end
77
-
78
- def test_metamark
79
- assert_raise(NoMethodError) { ShortURL.metamark(@url) }
80
- end
81
-
82
- def test_makeashorterlink
83
- assert_raise(NoMethodError) { ShortURL.makeashorterlink(@url) }
84
- end
85
-
86
- def test_skinnylink
87
- assert_raise(NoMethodError) { ShortURL.skinnylink(@url) }
88
- end
89
-
90
31
  end
data/test/ts_all.rb CHANGED
@@ -8,3 +8,4 @@ $:.unshift($test_lib_dir)
8
8
  require "test/unit"
9
9
 
10
10
  require "tc_shorturl"
11
+ require "tc_service"
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.2.1
7
- date: 2005-06-04
6
+ version: 0.3.0
7
+ date: 2005-06-05
8
8
  summary: Shortens URLs using services such as TinyURL and RubyURL
9
9
  require_paths:
10
10
  - lib
@@ -36,6 +36,7 @@ files:
36
36
  - doc/fr_method_index.html
37
37
  - doc/index.html
38
38
  - doc/rdoc-style.css
39
+ - doc/classes/Service.html
39
40
  - doc/classes/ShortURL.html
40
41
  - doc/files/ChangeLog.html
41
42
  - doc/files/lib
@@ -43,6 +44,7 @@ files:
43
44
  - doc/files/README.html
44
45
  - doc/files/TODO.html
45
46
  - doc/files/lib/shorturl_rb.html
47
+ - test/tc_service.rb
46
48
  - test/tc_shorturl.rb
47
49
  - test/ts_all.rb
48
50
  - README