expandurl 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # ExpandUrl
2
+
3
+ Expand urls shortened by URL shorteners, it supports all well-known shorteners. It balances the load randomly over multiple APIs.
4
+
5
+ ```ruby
6
+ gem 'expandurl'
7
+ ```
8
+
9
+ ## Usage
10
+ ```ruby
11
+ url = ExpandUrl.new 'http://bit.ly/Wdz42Dz'
12
+ url.service # => bit.ly
13
+ url.expand # => http://google.com/
14
+
15
+ # Some APIs prefer that you specify the User Agent to identify your app
16
+ # Set it using #user_agent:
17
+ url = ExpandUrl.new 'http://bit.ly/RYlFh'
18
+ url.service # => bit.ly
19
+ url.user_agent = 'ApplicationName/1.0'
20
+ url.expand # => http://yehudakatz.com/
21
+ ```
22
+
23
+ ## Behaviour
24
+ Like mentioned before, this gem uses multiple remote APIs that expand the given url. It chooses one randomly, and if the first one fails, it tries the second.
25
+
26
+ When a url is already a full url, both `#service` and `#expand` will return false.
27
+ When a url is unvalid, like `htp:/abc`, both `#service` and `#expand` will return false.
28
+
29
+ ## URL shortener services supported
30
+ For a full list, check `lib/services.yml`
data/lib/expandurl.rb ADDED
@@ -0,0 +1,63 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'addressable/uri'
4
+ require 'net/http'
5
+ require 'json'
6
+
7
+ class ExpandUrl
8
+ attr_accessor :service, :url, :user_agent
9
+
10
+ def initialize(url)
11
+ self.url = url
12
+ self.user_agent = 'ExpandUrlGem/1.0'
13
+ self.service = find_service
14
+ end
15
+
16
+ def find_service
17
+ host = Addressable::URI.parse(self.url).host
18
+ services = YAML::load_file(File.join(File.dirname(File.expand_path(__FILE__)), 'services.yml'))
19
+ services.each do |service|
20
+ return host if service == host
21
+ end
22
+ return false
23
+ end
24
+
25
+ def expand
26
+ if self.service
27
+ [1,2].shuffle.each do |api|
28
+ url = expand_by_api(api)
29
+ return url if url
30
+ end
31
+ end
32
+ return false
33
+ end
34
+
35
+ private
36
+ def expand_by_api(number)
37
+ case number
38
+ when 1
39
+ # Try longurl.org
40
+ uri = Addressable::URI.parse('http://api.longurl.org/v2/expand')
41
+ uri.query_values = { 'url' => self.url, 'format' => 'json' }
42
+
43
+ begin
44
+ url = JSON.parse(Net::HTTP.get(uri, {'User-Agent' => self.user_agent}))['long-url']
45
+ rescue
46
+ url = false
47
+ end
48
+ when 2
49
+ # Try longurlplease.com
50
+ uri = Addressable::URI.parse('http://www.longurlplease.com/api/v1.1')
51
+ uri.query_values = { 'q' => self.url }
52
+
53
+ begin
54
+ url = JSON.parse(Net::HTTP.get(uri)).first[1]
55
+ rescue
56
+ url = false
57
+ end
58
+ else
59
+ raise 'API'
60
+ end
61
+ return url
62
+ end
63
+ end
data/lib/services.yml ADDED
@@ -0,0 +1,341 @@
1
+ ---
2
+ - 0rz.tw
3
+ - 1link.in
4
+ - 1url.com
5
+ - 2.gp
6
+ - 2big.at
7
+ - 2tu.us
8
+ - 3.ly
9
+ - 307.to
10
+ - 4ms.me
11
+ - 4sq.com
12
+ - 4url.cc
13
+ - 6url.com
14
+ - 7.ly
15
+ - a.gg
16
+ - a.nf
17
+ - aa.cx
18
+ - abcurl.net
19
+ - ad.vu
20
+ - adf.ly
21
+ - adjix.com
22
+ - afx.cc
23
+ - all.fuseurl.com
24
+ - alturl.com
25
+ - amzn.to
26
+ - ar.gy
27
+ - arst.ch
28
+ - atu.ca
29
+ - azc.cc
30
+ - b23.ru
31
+ - b2l.me
32
+ - bacn.me
33
+ - bcool.bz
34
+ - binged.it
35
+ - bit.ly
36
+ - bizj.us
37
+ - bloat.me
38
+ - bravo.ly
39
+ - bsa.ly
40
+ - budurl.com
41
+ - canurl.com
42
+ - chilp.it
43
+ - chzb.gr
44
+ - cl.lk
45
+ - cl.ly
46
+ - clck.ru
47
+ - cli.gs
48
+ - cliccami.info
49
+ - clickthru.ca
50
+ - clop.in
51
+ - conta.cc
52
+ - cort.as
53
+ - cot.ag
54
+ - crks.me
55
+ - ctvr.us
56
+ - cutt.us
57
+ - dai.ly
58
+ - decenturl.com
59
+ - dfl8.me
60
+ - digbig.com
61
+ - digg.com
62
+ - disq.us
63
+ - dld.bz
64
+ - dlvr.it
65
+ - do.my
66
+ - doiop.com
67
+ - dopen.us
68
+ - easyuri.com
69
+ - easyurl.net
70
+ - eepurl.com
71
+ - eweri.com
72
+ - fa.by
73
+ - fav.me
74
+ - fb.me
75
+ - fbshare.me
76
+ - ff.im
77
+ - fff.to
78
+ - fire.to
79
+ - firsturl.de
80
+ - firsturl.net
81
+ - flic.kr
82
+ - flq.us
83
+ - fly2.ws
84
+ - fon.gs
85
+ - freak.to
86
+ - fuseurl.com
87
+ - fuzzy.to
88
+ - fwd4.me
89
+ - fwib.net
90
+ - g.ro.lt
91
+ - gizmo.do
92
+ - gl.am
93
+ - go.9nl.com
94
+ - go.ign.com
95
+ - go.usa.gov
96
+ - goo.gl
97
+ - goshrink.com
98
+ - gurl.es
99
+ - hex.io
100
+ - hiderefer.com
101
+ - hmm.ph
102
+ - href.in
103
+ - hsblinks.com
104
+ - htxt.it
105
+ - huff.to
106
+ - hulu.com
107
+ - hurl.me
108
+ - hurl.ws
109
+ - icanhaz.com
110
+ - idek.net
111
+ - ilix.in
112
+ - is.gd
113
+ - its.my
114
+ - ix.lt
115
+ - j.mp
116
+ - jijr.com
117
+ - kl.am
118
+ - klck.me
119
+ - korta.nu
120
+ - krunchd.com
121
+ - l9k.net
122
+ - lat.ms
123
+ - liip.to
124
+ - liltext.com
125
+ - linkbee.com
126
+ - linkbun.ch
127
+ - liurl.cn
128
+ - ln-s.net
129
+ - ln-s.ru
130
+ - lnk.gd
131
+ - lnk.ms
132
+ - lnkd.in
133
+ - lnkurl.com
134
+ - lru.jp
135
+ - lt.tl
136
+ - lurl.no
137
+ - macte.ch
138
+ - mash.to
139
+ - merky.de
140
+ - migre.me
141
+ - miniurl.com
142
+ - minurl.fr
143
+ - mke.me
144
+ - moby.to
145
+ - moourl.com
146
+ - mrte.ch
147
+ - myloc.me
148
+ - myurl.in
149
+ - n.pr
150
+ - nbc.co
151
+ - nblo.gs
152
+ - nn.nf
153
+ - not.my
154
+ - notlong.com
155
+ - nsfw.in
156
+ - nutshellurl.com
157
+ - nxy.in
158
+ - nyti.ms
159
+ - o-x.fr
160
+ - oc1.us
161
+ - om.ly
162
+ - omf.gd
163
+ - omoikane.net
164
+ - on.cnn.com
165
+ - on.mktw.net
166
+ - onforb.es
167
+ - orz.se
168
+ - ow.ly
169
+ - ping.fm
170
+ - pli.gs
171
+ - pnt.me
172
+ - politi.co
173
+ - post.ly
174
+ - pp.gg
175
+ - profile.to
176
+ - ptiturl.com
177
+ - pub.vitrue.com
178
+ - qlnk.net
179
+ - qte.me
180
+ - qu.tc
181
+ - qy.fi
182
+ - r.im
183
+ - rb6.me
184
+ - read.bi
185
+ - readthis.ca
186
+ - reallytinyurl.com
187
+ - redir.ec
188
+ - redirects.ca
189
+ - redirx.com
190
+ - retwt.me
191
+ - ri.ms
192
+ - rickroll.it
193
+ - riz.gd
194
+ - rt.nu
195
+ - ru.ly
196
+ - rubyurl.com
197
+ - rurl.org
198
+ - rww.tw
199
+ - s4c.in
200
+ - s7y.us
201
+ - safe.mn
202
+ - sameurl.com
203
+ - sdut.us
204
+ - shar.es
205
+ - shink.de
206
+ - shorl.com
207
+ - short.ie
208
+ - short.to
209
+ - shortlinks.co.uk
210
+ - shorturl.com
211
+ - shout.to
212
+ - show.my
213
+ - shrinkify.com
214
+ - shrinkr.com
215
+ - shrt.fr
216
+ - shrt.st
217
+ - shrten.com
218
+ - shrunkin.com
219
+ - simurl.com
220
+ - slate.me
221
+ - smallr.com
222
+ - smsh.me
223
+ - smurl.name
224
+ - sn.im
225
+ - snipr.com
226
+ - snipurl.com
227
+ - snurl.com
228
+ - sp2.ro
229
+ - spedr.com
230
+ - srnk.net
231
+ - srs.li
232
+ - starturl.com
233
+ - su.pr
234
+ - surl.co.uk
235
+ - surl.hu
236
+ - t.cn
237
+ - t.co
238
+ - t.lh.com
239
+ - ta.gd
240
+ - tbd.ly
241
+ - tcrn.ch
242
+ - tgr.me
243
+ - tgr.ph
244
+ - tighturl.com
245
+ - tiniuri.com
246
+ - tiny.cc
247
+ - tiny.ly
248
+ - tiny.pl
249
+ - tinylink.in
250
+ - tinyuri.ca
251
+ - tinyurl.com
252
+ - tk.
253
+ - tl.gd
254
+ - tmi.me
255
+ - tnij.org
256
+ - tnw.to
257
+ - tny.com
258
+ - to.
259
+ - to.ly
260
+ - togoto.us
261
+ - totc.us
262
+ - toysr.us
263
+ - tpm.ly
264
+ - tr.im
265
+ - tra.kz
266
+ - trunc.it
267
+ - twhub.com
268
+ - twirl.at
269
+ - twitclicks.com
270
+ - twitterurl.net
271
+ - twitterurl.org
272
+ - twiturl.de
273
+ - twurl.cc
274
+ - twurl.nl
275
+ - u.mavrev.com
276
+ - u.nu
277
+ - u76.org
278
+ - ub0.cc
279
+ - ulu.lu
280
+ - updating.me
281
+ - ur1.ca
282
+ - url.az
283
+ - url.co.uk
284
+ - url.ie
285
+ - url360.me
286
+ - url4.eu
287
+ - urlborg.com
288
+ - urlbrief.com
289
+ - urlcover.com
290
+ - urlcut.com
291
+ - urlenco.de
292
+ - urli.nl
293
+ - urls.im
294
+ - urlshorteningservicefortwitter.com
295
+ - urlx.ie
296
+ - urlzen.com
297
+ - usat.ly
298
+ - use.my
299
+ - vb.ly
300
+ - vgn.am
301
+ - vl.am
302
+ - vm.lc
303
+ - w55.de
304
+ - wapo.st
305
+ - wapurl.co.uk
306
+ - wipi.es
307
+ - wp.me
308
+ - x.vu
309
+ - xr.com
310
+ - xrl.in
311
+ - xrl.us
312
+ - xurl.es
313
+ - xurl.jp
314
+ - y.ahoo.it
315
+ - yatuc.com
316
+ - ye.pe
317
+ - yep.it
318
+ - yfrog.com
319
+ - yhoo.it
320
+ - yiyd.com
321
+ - youtu.be
322
+ - yuarel.com
323
+ - z0p.de
324
+ - zi.ma
325
+ - zi.mu
326
+ - zipmyurl.com
327
+ - zud.me
328
+ - zurl.ws
329
+ - zz.gd
330
+ - zzang.kr
331
+ - ›.ws
332
+ - ✩.ws
333
+ - ✿.ws
334
+ - ❥.ws
335
+ - ➔.ws
336
+ - ➞.ws
337
+ - ➡.ws
338
+ - ➨.ws
339
+ - ➯.ws
340
+ - ➹.ws
341
+ - ➽.ws
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ describe ExpandUrl do
4
+ context 'valid short url' do
5
+ before :each do
6
+ @url = ExpandUrl.new 'http://goo.gl/Tt6yT' # yehudakatz.com
7
+ end
8
+ it 'recognises the service' do
9
+ @url.service.should == 'goo.gl'
10
+ end
11
+ it 'expands the url' do
12
+ @url.expand.should == 'http://yehudakatz.com/'
13
+ end
14
+ end
15
+
16
+ context 'invalid short url' do
17
+ before :each do
18
+ @url = ExpandUrl.new 'http://bit.ly/12345678910111213XXXXXXXXXXXXXXXXXXXXXX' # invalid
19
+ end
20
+ it 'recognises the service' do
21
+ @url.service.should == 'bit.ly'
22
+ end
23
+ it 'does not expand the url' do
24
+ @url.expand.should == false
25
+ end
26
+ end
27
+
28
+ context 'full url' do
29
+ before :each do
30
+ @url = ExpandUrl.new 'http://yehudakatz.com'
31
+ end
32
+ it 'does not recognise the service' do
33
+ @url.service.should == false
34
+ end
35
+ it 'does not expand it' do
36
+ @url.expand.should == false
37
+ end
38
+ end
39
+
40
+ context 'invalid url' do
41
+ it 'does not error out' do
42
+ url = ExpandUrl.new 'htpp://abc'
43
+ url.service.should == false
44
+ url.expand.should == false
45
+ end
46
+ end
47
+ end
48
+
49
+
@@ -0,0 +1 @@
1
+ require_relative '../lib/expandurl'
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: expandurl
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Bart Olsthoorn
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: addressable
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Expands URLS shortened by public services. Supports all the well-known
47
+ providers.
48
+ email:
49
+ - bartolsthoorn@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - lib/expandurl.rb
55
+ - lib/services.yml
56
+ - spec/expandurl_spec.rb
57
+ - spec/spec_helper.rb
58
+ - README.md
59
+ homepage: https://github.com/pointerbp/expandurl
60
+ licenses: []
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 1.8.25
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: Expands URLs shortened by public services
83
+ test_files: []