shorturl 0.2.0 → 0.2.1

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.2.1:
2
+ - Added makeashorterlink.com and skinnylink.com
3
+ - Refactored get_short_url
1
4
  0.2.0:
2
5
  - Added shorl.com, snipurl.com and metamark.net
3
6
  0.1.0:
data/README CHANGED
@@ -19,6 +19,8 @@ The second parameter represents the service you want to use. These are:
19
19
  - <tt>:snorl</tt>
20
20
  - <tt>:snipurl</tt>
21
21
  - <tt>:metamark</tt>
22
+ - <tt>:makeashorterlink</tt>
23
+ - <tt>:skinnylink</tt>
22
24
 
23
25
  == Thanks
24
26
  - Marcel Molina Jr., Devin Mullins for some ideas
@@ -86,13 +86,15 @@
86
86
  <h3 class="section-bar">Methods</h3>
87
87
 
88
88
  <div class="name-list">
89
- <a href="#M000002">get_short_url</a>&nbsp;&nbsp;
90
- <a href="#M000007">metamark</a>&nbsp;&nbsp;
91
- <a href="#M000003">rubyurl</a>&nbsp;&nbsp;
92
- <a href="#M000005">shorl</a>&nbsp;&nbsp;
93
- <a href="#M000001">shorten</a>&nbsp;&nbsp;
94
- <a href="#M000006">snipurl</a>&nbsp;&nbsp;
95
- <a href="#M000004">tinyurl</a>&nbsp;&nbsp;
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
+ <a href="#M000002">shorten</a>&nbsp;&nbsp;
95
+ <a href="#M000007">snipurl</a>&nbsp;&nbsp;
96
+ <a href="#M000005">tinyurl</a>&nbsp;&nbsp;
97
+ <a href="#M000001">valid_services</a>&nbsp;&nbsp;
96
98
  </div>
97
99
  </div>
98
100
 
@@ -114,168 +116,208 @@
114
116
  <div id="methods">
115
117
  <h3 class="section-bar">Public Class methods</h3>
116
118
 
117
- <div id="method-M000001" class="method-detail">
118
- <a name="M000001"></a>
119
+ <div id="method-M000009" class="method-detail">
120
+ <a name="M000009"></a>
119
121
 
120
122
  <div class="method-heading">
121
- <a href="#M000001" class="method-signature">
122
- <span class="method-name">shorten</span><span class="method-args">(url, service = :rubyurl)</span>
123
+ <a href="#M000009" class="method-signature">
124
+ <span class="method-name">makeashorterlink</span><span class="method-args">(url)</span>
123
125
  </a>
124
126
  </div>
125
127
 
126
128
  <div class="method-description">
127
129
  <p>
128
- Main method of <a href="ShortURL.html">ShortURL</a>, its usage is quite
129
- simple, just give a url to shorten and an optional service. If no service
130
- is selected, RubyURL.com will be used. An invalid service symbol will raise
131
- an ArgumentError exception
132
- </p>
133
- <p>
134
- Valid <tt>service</tt> values:
130
+ Shorten an URL with makeashortlink.com.
135
131
  </p>
136
- <table>
137
- <tr><td valign="top"><tt>:rubyurl</tt>:</td><td>uses RubyURL.com
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>
138
148
 
139
- </td></tr>
140
- <tr><td valign="top"><tt>:tinyurl</tt>:</td><td>uses TinyURL.com
149
+ <div id="method-M000008" class="method-detail">
150
+ <a name="M000008"></a>
141
151
 
142
- </td></tr>
143
- </table>
144
- <p>
145
- call-seq:
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
146
161
  </p>
147
- <pre>
148
- ShortURL.shorten(&quot;http://mypage.com&quot;) =&gt; Uses RubyURL
149
- ShortURL.shorten(&quot;http://mypage.com&quot;, :tinyurl)
150
- </pre>
151
162
  <p><a class="source-toggle" href="#"
152
- onclick="toggleCode('M000001-source');return false;">[Source]</a></p>
153
- <div class="method-source-code" id="M000001-source">
163
+ onclick="toggleCode('M000008-source');return false;">[Source]</a></p>
164
+ <div class="method-source-code" id="M000008-source">
154
165
  <pre>
155
- <span class="ruby-comment cmt"># File lib/shorturl.rb, line 26</span>
156
- 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>)
157
- 27: <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">service</span>
158
- 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>)
159
- 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>)
160
- 30: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:shorl</span> <span class="ruby-keyword kw">then</span> <span class="ruby-identifier">shorl</span>(<span class="ruby-identifier">url</span>)
161
- 31: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:snipurl</span> <span class="ruby-keyword kw">then</span> <span class="ruby-identifier">snipurl</span>(<span class="ruby-identifier">url</span>)
162
- 32: <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:metamark</span> <span class="ruby-keyword kw">then</span> <span class="ruby-identifier">metamark</span>(<span class="ruby-identifier">url</span>)
163
- 33: <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>
164
- 34: <span class="ruby-keyword kw">end</span>
165
- 35: <span class="ruby-keyword kw">end</span>
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>
166
175
  </pre>
167
176
  </div>
168
177
  </div>
169
178
  </div>
170
179
 
171
- <h3 class="section-bar">Private Class methods</h3>
172
-
173
- <div id="method-M000002" class="method-detail">
174
- <a name="M000002"></a>
180
+ <div id="method-M000004" class="method-detail">
181
+ <a name="M000004"></a>
175
182
 
176
183
  <div class="method-heading">
177
- <a href="#M000002" class="method-signature">
178
- <span class="method-name">get_short_url</span><span class="method-args">(hostname, code, action) {|html_body| ...}</span>
184
+ <a href="#M000004" class="method-signature">
185
+ <span class="method-name">rubyurl</span><span class="method-args">(url)</span>
179
186
  </a>
180
187
  </div>
181
188
 
182
189
  <div class="method-description">
183
190
  <p>
184
- Abstract method to shorten a URL. Respects the DRY principle. In case of a
185
- network-related exception, return <tt>nil</tt>.
186
- </p>
187
- <p>
188
- Parameters:
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.
189
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">
190
200
  <pre>
191
- hostname: hostname of the service
192
- code: expected return code (200, 302, etc.) Converted to a string
193
- action: string representing the action to be executed on the HTTP object with +instance_eval+
194
- block: block of code which scrapes the HTML and returns the shortened URL. Takes one parameter.
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>
195
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>
196
227
  <p><a class="source-toggle" href="#"
197
- onclick="toggleCode('M000002-source');return false;">[Source]</a></p>
198
- <div class="method-source-code" id="M000002-source">
228
+ onclick="toggleCode('M000006-source');return false;">[Source]</a></p>
229
+ <div class="method-source-code" id="M000006-source">
199
230
  <pre>
200
- <span class="ruby-comment cmt"># File lib/shorturl.rb, line 45</span>
201
- 45: <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>
202
- 46: <span class="ruby-keyword kw">begin</span>
203
- 47: <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>
204
- 48: <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>)
205
- 49:
206
- 50: <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>
207
- 51: <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>)
208
- 52: <span class="ruby-keyword kw">end</span>
209
- 53: }
210
- 54: <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>
211
- 55: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">nil</span>
212
- 56: <span class="ruby-keyword kw">end</span>
213
- 57: <span class="ruby-keyword kw">end</span>
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>
214
239
  </pre>
215
240
  </div>
216
241
  </div>
217
242
  </div>
218
243
 
219
- <div id="method-M000007" class="method-detail">
220
- <a name="M000007"></a>
244
+ <div id="method-M000002" class="method-detail">
245
+ <a name="M000002"></a>
221
246
 
222
247
  <div class="method-heading">
223
- <a href="#M000007" class="method-signature">
224
- <span class="method-name">metamark</span><span class="method-args">(url)</span>
248
+ <a href="#M000002" class="method-signature">
249
+ <span class="method-name">shorten</span><span class="method-args">(url, service = :rubyurl)</span>
225
250
  </a>
226
251
  </div>
227
252
 
228
253
  <div class="method-description">
229
254
  <p>
230
- Shorten a URL with metamark.com. Same technique as TinyURL
255
+ Main method of <a href="ShortURL.html">ShortURL</a>, its usage is quite
256
+ simple, just give an url to shorten and an optional service. If no service
257
+ is selected, RubyURL.com will be used. An invalid service symbol will raise
258
+ an ArgumentError exception
259
+ </p>
260
+ <p>
261
+ Valid <tt>service</tt> values:
231
262
  </p>
263
+ <table>
264
+ <tr><td valign="top"><tt>:rubyurl</tt>:</td><td>uses RubyURL.com
265
+
266
+ </td></tr>
267
+ <tr><td valign="top"><tt>:tinyurl</tt>:</td><td>uses TinyURL.com
268
+
269
+ </td></tr>
270
+ </table>
271
+ <p>
272
+ call-seq:
273
+ </p>
274
+ <pre>
275
+ ShortURL.shorten(&quot;http://mypage.com&quot;) =&gt; Uses RubyURL
276
+ ShortURL.shorten(&quot;http://mypage.com&quot;, :tinyurl)
277
+ </pre>
232
278
  <p><a class="source-toggle" href="#"
233
- onclick="toggleCode('M000007-source');return false;">[Source]</a></p>
234
- <div class="method-source-code" id="M000007-source">
279
+ onclick="toggleCode('M000002-source');return false;">[Source]</a></p>
280
+ <div class="method-source-code" id="M000002-source">
235
281
  <pre>
236
- <span class="ruby-comment cmt"># File lib/shorturl.rb, line 107</span>
237
- 107: <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>)
238
- 108: <span class="ruby-identifier">get_short_url</span>(<span class="ruby-value str">&quot;metamark.net&quot;</span>, <span class="ruby-value">200</span>,
239
- 109: <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>
240
- 110: <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> }
241
- 111: <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>]
242
- 112: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">short_url</span>
243
- 113: }
244
- 114: <span class="ruby-keyword kw">end</span>
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>
245
290
  </pre>
246
291
  </div>
247
292
  </div>
248
293
  </div>
249
294
 
250
- <div id="method-M000003" class="method-detail">
251
- <a name="M000003"></a>
295
+ <div id="method-M000007" class="method-detail">
296
+ <a name="M000007"></a>
252
297
 
253
298
  <div class="method-heading">
254
- <a href="#M000003" class="method-signature">
255
- <span class="method-name">rubyurl</span><span class="method-args">(url)</span>
299
+ <a href="#M000007" class="method-signature">
300
+ <span class="method-name">snipurl</span><span class="method-args">(url)</span>
256
301
  </a>
257
302
  </div>
258
303
 
259
304
  <div class="method-description">
260
305
  <p>
261
- Shortens an URL with RubyURL.com. Since RubyURL uses a redirection (code
262
- 302), instead of using HTTP#post, we send our data directly to the
263
- <em>create</em> action. The response is a small HTML line which contains
264
- our shortened URL plus <em>/rubyurl/show</em>, so we manually remove that
265
- with gsub.
306
+ Shorten an URL with snipurl.com. Same technique as TinyURL
266
307
  </p>
267
308
  <p><a class="source-toggle" href="#"
268
- onclick="toggleCode('M000003-source');return false;">[Source]</a></p>
269
- <div class="method-source-code" id="M000003-source">
309
+ onclick="toggleCode('M000007-source');return false;">[Source]</a></p>
310
+ <div class="method-source-code" id="M000007-source">
270
311
  <pre>
271
- <span class="ruby-comment cmt"># File lib/shorturl.rb, line 65</span>
272
- 65: <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>)
273
- 66: <span class="ruby-identifier">get_short_url</span>(<span class="ruby-value str">&quot;rubyurl.com&quot;</span>, <span class="ruby-value">302</span>,
274
- 67: <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>
275
- 68: <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>)
276
- 69: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">short_url</span>
277
- 70: }
278
- 71: <span class="ruby-keyword kw">end</span>
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>
279
321
  </pre>
280
322
  </div>
281
323
  </div>
@@ -286,91 +328,104 @@ with gsub.
286
328
 
287
329
  <div class="method-heading">
288
330
  <a href="#M000005" class="method-signature">
289
- <span class="method-name">shorl</span><span class="method-args">(url)</span>
331
+ <span class="method-name">tinyurl</span><span class="method-args">(url)</span>
290
332
  </a>
291
333
  </div>
292
334
 
293
335
  <div class="method-description">
294
336
  <p>
295
- Shorten a URL through with shorl.com. Same technique as TinyURL
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.
296
341
  </p>
297
342
  <p><a class="source-toggle" href="#"
298
343
  onclick="toggleCode('M000005-source');return false;">[Source]</a></p>
299
344
  <div class="method-source-code" id="M000005-source">
300
345
  <pre>
301
- <span class="ruby-comment cmt"># File lib/shorturl.rb, line 88</span>
302
- 88: <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>)
303
- 89: <span class="ruby-identifier">get_short_url</span>(<span class="ruby-value str">&quot;shorl.com&quot;</span>, <span class="ruby-value">200</span>,
304
- 90: <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>
305
- 91: <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>]
306
- 92: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">short_url</span>
307
- 93: }
308
- 94: <span class="ruby-keyword kw">end</span>
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>
309
355
  </pre>
310
356
  </div>
311
357
  </div>
312
358
  </div>
313
359
 
314
- <div id="method-M000006" class="method-detail">
315
- <a name="M000006"></a>
360
+ <div id="method-M000001" class="method-detail">
361
+ <a name="M000001"></a>
316
362
 
317
363
  <div class="method-heading">
318
- <a href="#M000006" class="method-signature">
319
- <span class="method-name">snipurl</span><span class="method-args">(url)</span>
364
+ <a href="#M000001" class="method-signature">
365
+ <span class="method-name">valid_services</span><span class="method-args">()</span>
320
366
  </a>
321
367
  </div>
322
368
 
323
369
  <div class="method-description">
324
370
  <p>
325
- Shorten a URL with snipurl.com. Same technique as TinyURL
371
+ Returns @@<a href="ShortURL.html#M000001">valid_services</a>
326
372
  </p>
327
373
  <p><a class="source-toggle" href="#"
328
- onclick="toggleCode('M000006-source');return false;">[Source]</a></p>
329
- <div class="method-source-code" id="M000006-source">
374
+ onclick="toggleCode('M000001-source');return false;">[Source]</a></p>
375
+ <div class="method-source-code" id="M000001-source">
330
376
  <pre>
331
- <span class="ruby-comment cmt"># File lib/shorturl.rb, line 97</span>
332
- 97: <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>)
333
- 98: <span class="ruby-identifier">get_short_url</span>(<span class="ruby-value str">&quot;snipurl.com&quot;</span>, <span class="ruby-value">200</span>,
334
- 99: <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>
335
- 100: <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>]
336
- 101: <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>
337
- 102: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">short_url</span>
338
- 103: }
339
- 104: <span class="ruby-keyword kw">end</span>
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>
340
381
  </pre>
341
382
  </div>
342
383
  </div>
343
384
  </div>
344
385
 
345
- <div id="method-M000004" class="method-detail">
346
- <a name="M000004"></a>
386
+ <h3 class="section-bar">Private Class methods</h3>
387
+
388
+ <div id="method-M000003" class="method-detail">
389
+ <a name="M000003"></a>
347
390
 
348
391
  <div class="method-heading">
349
- <a href="#M000004" class="method-signature">
350
- <span class="method-name">tinyurl</span><span class="method-args">(url)</span>
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>
351
394
  </a>
352
395
  </div>
353
396
 
354
397
  <div class="method-description">
355
398
  <p>
356
- Shortens an URL with TinyURL.com. We post a request to the
357
- <em>create.php</em> action with our URL as a parameter. We then read the
358
- HTML code, split it into an array, find an element that contains our
359
- shortened URL and extract it.
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:
360
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>
361
411
  <p><a class="source-toggle" href="#"
362
- onclick="toggleCode('M000004-source');return false;">[Source]</a></p>
363
- <div class="method-source-code" id="M000004-source">
412
+ onclick="toggleCode('M000003-source');return false;">[Source]</a></p>
413
+ <div class="method-source-code" id="M000003-source">
364
414
  <pre>
365
- <span class="ruby-comment cmt"># File lib/shorturl.rb, line 78</span>
366
- 78: <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>)
367
- 79: <span class="ruby-identifier">get_short_url</span>(<span class="ruby-value str">&quot;tinyurl.com&quot;</span>, <span class="ruby-value">200</span>,
368
- 80: <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>
369
- 81: <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> }
370
- 82: <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>]
371
- 83: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">short_url</span>
372
- 84: }
373
- 85: <span class="ruby-keyword kw">end</span>
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>
374
429
  </pre>
375
430
  </div>
376
431
  </div>
data/doc/created.rid CHANGED
@@ -1 +1 @@
1
- Fri Jun 03 21:04:57 EDT 2005
1
+ Sat Jun 04 15:14:15 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>Fri Jun 03 20:59:42 EDT 2005</td>
59
+ <td>Sat Jun 04 15:06:47 EDT 2005</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -70,6 +70,13 @@
70
70
 
71
71
  <div id="description">
72
72
  <p>
73
+ 0.2.1:
74
+ </p>
75
+ <pre>
76
+ - Added makeashorterlink.com
77
+ - Refactored get_short_url
78
+ </pre>
79
+ <p>
73
80
  0.2.0:
74
81
  </p>
75
82
  <pre>
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Fri Jun 03 21:01:00 EDT 2005</td>
59
+ <td>Sat Jun 04 15:07:27 EDT 2005</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -106,6 +106,9 @@ The second parameter represents the service you want to use. These are:
106
106
  </li>
107
107
  <li><tt>:metamark</tt>
108
108
 
109
+ </li>
110
+ <li><tt>:makeashorterlink</tt>
111
+
109
112
  </li>
110
113
  </ul>
111
114
  <h2>Thanks</h2>
@@ -56,7 +56,7 @@
56
56
  </tr>
57
57
  <tr class="top-aligned-row">
58
58
  <td><strong>Last Update:</strong></td>
59
- <td>Fri Jun 03 21:03:56 EDT 2005</td>
59
+ <td>Sat Jun 04 15:05:31 EDT 2005</td>
60
60
  </tr>
61
61
  </table>
62
62
  </div>
@@ -20,13 +20,15 @@
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">get_short_url (ShortURL)</a><br />
24
- <a href="classes/ShortURL.html#M000007">metamark (ShortURL)</a><br />
25
- <a href="classes/ShortURL.html#M000003">rubyurl (ShortURL)</a><br />
26
- <a href="classes/ShortURL.html#M000005">shorl (ShortURL)</a><br />
27
- <a href="classes/ShortURL.html#M000001">shorten (ShortURL)</a><br />
28
- <a href="classes/ShortURL.html#M000006">snipurl (ShortURL)</a><br />
29
- <a href="classes/ShortURL.html#M000004">tinyurl (ShortURL)</a><br />
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 />
28
+ <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
+ <a href="classes/ShortURL.html#M000001">valid_services (ShortURL)</a><br />
30
32
  </div>
31
33
  </div>
32
34
  </body>
data/lib/shorturl.rb CHANGED
@@ -10,7 +10,16 @@ require "uri"
10
10
 
11
11
  class ShortURL
12
12
 
13
- # Main method of ShortURL, its usage is quite simple, just give a
13
+ # Array containing symbols representing all the implemented URL
14
+ # shortening services
15
+ @@valid_services = [:rubyurl, :tinyurl, :shorl, :snipurl, :metamark, :makeashorterlink, :skinnylink]
16
+
17
+ # Returns @@valid_services
18
+ def self.valid_services
19
+ @@valid_services
20
+ end
21
+
22
+ # Main method of ShortURL, its usage is quite simple, just give an
14
23
  # url to shorten and an optional service. If no service is
15
24
  # selected, RubyURL.com will be used. An invalid service symbol
16
25
  # will raise an ArgumentError exception
@@ -24,17 +33,14 @@ class ShortURL
24
33
  # ShortURL.shorten("http://mypage.com") => Uses RubyURL
25
34
  # ShortURL.shorten("http://mypage.com", :tinyurl)
26
35
  def self.shorten(url, service = :rubyurl)
27
- case service
28
- when :rubyurl then rubyurl(url)
29
- when :tinyurl then tinyurl(url)
30
- when :shorl then shorl(url)
31
- when :snipurl then snipurl(url)
32
- when :metamark then metamark(url)
33
- else raise ArgumentError, "Invalid service"
36
+ if @@valid_services.include? service
37
+ send(service, url)
38
+ else
39
+ raise ArgumentError, "Invalid service"
34
40
  end
35
41
  end
36
42
 
37
- # Abstract method to shorten a URL. Respects the DRY principle. In
43
+ # Abstract method to shorten an URL. Respects the DRY principle. In
38
44
  # case of a network-related exception, return +nil+.
39
45
  #
40
46
  # Parameters:
@@ -84,7 +90,7 @@ class ShortURL
84
90
  }
85
91
  end
86
92
 
87
- # Shorten a URL through with shorl.com. Same technique as TinyURL
93
+ # Shorten an URL through with shorl.com. Same technique as TinyURL
88
94
  def self.shorl(url)
89
95
  get_short_url("shorl.com", 200,
90
96
  "post('/create.php', 'url=#{url}')") { |body|
@@ -93,7 +99,7 @@ class ShortURL
93
99
  }
94
100
  end
95
101
 
96
- # Shorten a URL with snipurl.com. Same technique as TinyURL
102
+ # Shorten an URL with snipurl.com. Same technique as TinyURL
97
103
  def self.snipurl(url)
98
104
  get_short_url("snipurl.com", 200,
99
105
  "post('/index.php', 'link=#{url}')") { |body|
@@ -103,7 +109,7 @@ class ShortURL
103
109
  }
104
110
  end
105
111
 
106
- # Shorten a URL with metamark.com. Same technique as TinyURL
112
+ # Shorten an URL with metamark.com. Same technique as TinyURL
107
113
  def self.metamark(url)
108
114
  get_short_url("metamark.net", 200,
109
115
  "post('/add', 'long_url=#{url}')") { |body|
@@ -112,7 +118,24 @@ class ShortURL
112
118
  return short_url
113
119
  }
114
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
115
139
 
116
- private_class_method(:get_short_url, :tinyurl, :rubyurl, :shorl,
117
- :snipurl, :metamark)
140
+ private_class_method(:get_short_url, *@@valid_services)
118
141
  end
data/test/tc_shorturl.rb CHANGED
@@ -30,6 +30,8 @@ class TestShortURL < Test::Unit::TestCase
30
30
  assert ShortURL.shorten(@url, :shorl) =~ /^http:\/\/shorl.com\//
31
31
  assert ShortURL.shorten(@url, :snipurl) == "http://snipurl.com/fbkl"
32
32
  assert ShortURL.shorten(@url, :metamark) == "http://xrl.us/ga8c"
33
+ assert ShortURL.shorten(@url, :makeashorterlink) == "http://makeashorterlink.com/?O1752235A"
34
+ assert ShortURL.shorten(@url, :skinnylink) == "http://skinnylink.com?nqvcce"
33
35
 
34
36
  assert_raise(ArgumentError) { ShortURL.shorten(@url, :foobar) }
35
37
  end
@@ -76,4 +78,13 @@ class TestShortURL < Test::Unit::TestCase
76
78
  def test_metamark
77
79
  assert_raise(NoMethodError) { ShortURL.metamark(@url) }
78
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
+
79
90
  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.2.0
7
- date: 2005-06-03
6
+ version: 0.2.1
7
+ date: 2005-06-04
8
8
  summary: Shortens URLs using services such as TinyURL and RubyURL
9
9
  require_paths:
10
10
  - lib