shorturl 0.8.0 → 0.8.1
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog +3 -0
- data/README +5 -3
- data/bin/shorturl +2 -0
- data/lib/shorturl.rb +187 -185
- data/test/tc_service.rb +1 -1
- data/test/tc_shorturl.rb +1 -1
- metadata +1 -1
data/ChangeLog
CHANGED
data/README
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
= ShortURL 0.8.
|
1
|
+
= ShortURL 0.8.1
|
2
2
|
|
3
3
|
== Summary
|
4
4
|
ShortURL is a very simple library to use URL shortening services such as
|
@@ -31,13 +31,14 @@ Here is the list of the services supported by ShortURL:
|
|
31
31
|
* http://clipurl.com
|
32
32
|
* http://shortify.com
|
33
33
|
* http://0rz.net
|
34
|
+
* http://skinnylink.com
|
34
35
|
|
35
36
|
== Usage:
|
36
37
|
|
37
38
|
call-seq:
|
38
39
|
require "shorturl"
|
39
|
-
puts ShortURL.shorten("http://mypage.com")
|
40
|
-
puts ShortURL.shorten("http://mypage.com", :tinyurl)
|
40
|
+
puts WWW::ShortURL.shorten("http://mypage.com")
|
41
|
+
puts WWW::ShortURL.shorten("http://mypage.com", :tinyurl)
|
41
42
|
|
42
43
|
The second parameter represents the service you want to use. These are:
|
43
44
|
* <tt>:rubyurl</tt>
|
@@ -58,6 +59,7 @@ The second parameter represents the service you want to use. These are:
|
|
58
59
|
* <tt>:clipurl</tt>
|
59
60
|
* <tt>:shortify</tt>
|
60
61
|
* <tt>:orz</tt>
|
62
|
+
* <tt>:skinnylink</tt>
|
61
63
|
|
62
64
|
|
63
65
|
You can use <tt>ShortURL.valid_services</tt> to obtain a
|
data/bin/shorturl
CHANGED
data/lib/shorturl.rb
CHANGED
@@ -7,202 +7,204 @@ require "net/http"
|
|
7
7
|
require "cgi"
|
8
8
|
require "uri"
|
9
9
|
|
10
|
-
|
11
|
-
|
10
|
+
module WWW
|
11
|
+
class InvalidService < Exception
|
12
|
+
end
|
12
13
|
|
13
|
-
class Service
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
14
|
+
class Service
|
15
|
+
attr_accessor :port, :code, :method, :action, :field, :block
|
16
|
+
|
17
|
+
# Intialize the service with a hostname (required parameter) and you
|
18
|
+
# can override the default values for the HTTP port, expected HTTP
|
19
|
+
# return code, the form method to use, the form action, the form
|
20
|
+
# field which contains the long URL, and the block of what to do
|
21
|
+
# with the HTML code you get.
|
22
|
+
def initialize(hostname) # :yield: service
|
23
|
+
@hostname = hostname
|
24
|
+
@port = 80
|
25
|
+
@code = 200
|
26
|
+
@method = :post
|
27
|
+
@action = "/"
|
28
|
+
@field = "url"
|
29
|
+
@block = lambda { |body| }
|
30
|
+
|
31
|
+
if block_given?
|
32
|
+
yield self
|
33
|
+
end
|
32
34
|
end
|
33
|
-
end
|
34
35
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
36
|
+
# Now that our service is set up, call it with all the parameters to
|
37
|
+
# (hopefully) return only the shortened URL.
|
38
|
+
def call(url)
|
39
|
+
Net::HTTP.start(@hostname, @port) { |http|
|
40
|
+
response = case @method
|
41
|
+
when :post: http.post(@action, "#{@field}=#{url}")
|
42
|
+
when :get: http.get("#{@action}?#{@field}=#{CGI.escape(url)}")
|
43
|
+
end
|
44
|
+
if response.code == @code.to_s
|
45
|
+
begin
|
46
|
+
@block.call(response.read_body)
|
47
|
+
rescue NoMethodError
|
48
|
+
nil
|
49
|
+
end
|
48
50
|
end
|
49
|
-
|
50
|
-
|
51
|
+
}
|
52
|
+
end
|
51
53
|
end
|
52
|
-
end
|
53
54
|
|
54
|
-
class ShortURL
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
55
|
+
class ShortURL
|
56
|
+
# Hash table of all the supported services. The key is a symbol
|
57
|
+
# representing the service (usually the hostname minus the .com,
|
58
|
+
# .net, etc.) The value is an instance of Service with all the
|
59
|
+
# parameters set so that when +instance+.call is invoked, the
|
60
|
+
# shortened URL is returned.
|
61
|
+
@@services = {
|
62
|
+
:rubyurl => Service.new("rubyurl.com") { |s|
|
63
|
+
s.code = 302
|
64
|
+
s.method = :get
|
65
|
+
s.action = "/rubyurl/create"
|
66
|
+
s.field = "rubyurl[website_url]"
|
67
|
+
s.block = lambda { |body| URI.extract(body)[0].gsub("rubyurl/show/", "") }
|
68
|
+
},
|
69
|
+
|
70
|
+
:tinyurl => Service.new("tinyurl.com") { |s|
|
71
|
+
s.action = "/create.php"
|
72
|
+
s.block = lambda { |body| URI.extract(body).grep(/tinyurl/)[-1] }
|
73
|
+
},
|
74
|
+
|
75
|
+
:shorl => Service.new("shorl.com") { |s|
|
76
|
+
s.action = "/create.php"
|
77
|
+
s.block = lambda { |body| URI.extract(body)[2] }
|
78
|
+
},
|
79
|
+
|
80
|
+
:snipurl => Service.new("snipurl.com") { |s|
|
81
|
+
s.action = "/index.php"
|
82
|
+
s.field = "link"
|
83
|
+
s.block = lambda { |body|
|
84
|
+
line = body.split("\n").grep(/txt/)[0]
|
85
|
+
short_url = URI.extract(line)[1][0..-2] # Remove trailing '
|
86
|
+
}
|
87
|
+
},
|
88
|
+
|
89
|
+
:metamark => Service.new("metamark.net") { |s|
|
90
|
+
s.action = "/add"
|
91
|
+
s.field = "long_url"
|
92
|
+
s.block = lambda { |body| URI.extract(body).grep(/xrl.us/)[0] }
|
93
|
+
},
|
94
|
+
|
95
|
+
:makeashorterlink => Service.new("makeashorterlink.com") { |s|
|
96
|
+
s.action = "/index.php"
|
97
|
+
s.block = lambda { |body| URI.extract(body).grep(/makeashorterlink/)[0] }
|
98
|
+
},
|
99
|
+
|
100
|
+
:skinnylink => Service.new("skinnylink.com") { |s|
|
101
|
+
s.block = lambda { |body| URI.extract(body).grep(/skinnylink/)[0] }
|
102
|
+
},
|
103
|
+
|
104
|
+
:linktrim => Service.new("linktrim.com") { |s|
|
105
|
+
s.method = :get
|
106
|
+
s.action = "/lt/generate"
|
107
|
+
s.block = lambda { |body| URI.extract(body).grep(/\/linktrim/)[1] }
|
108
|
+
},
|
109
|
+
|
110
|
+
:shorterlink => Service.new("shorterlink.com") { |s|
|
111
|
+
s.method = :get
|
112
|
+
s.action = "/add_url.html"
|
113
|
+
s.block = lambda { |body| URI.extract(body).grep(/shorterlink/)[0] }
|
114
|
+
},
|
115
|
+
|
116
|
+
:minilink => Service.new("minilink.org") { |s|
|
117
|
+
s.method = :get
|
118
|
+
s.block = lambda { |body| URI.extract(body)[-1] }
|
119
|
+
},
|
120
|
+
|
121
|
+
:lns => Service.new("ln-s.net") { |s|
|
122
|
+
s.method = :get
|
123
|
+
s.action = "/home/api.jsp"
|
124
|
+
s.block = lambda { |body| URI.extract(body)[0] }
|
125
|
+
},
|
126
|
+
|
127
|
+
:fyad => Service.new("fyad.org") { |s|
|
128
|
+
s.method = :get
|
129
|
+
s.block = lambda { |body| URI.extract(body).grep(/fyad.org/)[2] }
|
130
|
+
},
|
131
|
+
|
132
|
+
:d62 => Service.new("d62.net") { |s|
|
133
|
+
s.method = :get
|
134
|
+
s.block = lambda { |body| URI.extract(body)[0] }
|
135
|
+
},
|
136
|
+
|
137
|
+
:shiturl => Service.new("shiturl.com") { |s|
|
138
|
+
s.method = :get
|
139
|
+
s.action = "/make.php"
|
140
|
+
s.block = lambda { |body| URI.extract(body).grep(/shiturl/)[0] }
|
141
|
+
},
|
142
|
+
|
143
|
+
:littlink => Service.new("littlink.com") { |s|
|
144
|
+
s.block = lambda { |body| URI.extract(body).grep(/littlink/)[0] }
|
145
|
+
},
|
146
|
+
|
147
|
+
:clipurl => Service.new("clipurl.com") { |s|
|
148
|
+
s.action = "/create.asp"
|
149
|
+
s.block = lambda { |body| URI.extract(body).grep(/clipurl/)[0] }
|
150
|
+
},
|
151
|
+
|
152
|
+
:shortify => Service.new("shortify.com") { |s|
|
153
|
+
s.method = :get
|
154
|
+
s.action = "/shorten.php"
|
155
|
+
s.block = lambda { |body| URI.extract(body).grep(/shortify/)[0] }
|
156
|
+
},
|
157
|
+
|
158
|
+
:orz => Service.new("0rz.net") { |s|
|
159
|
+
s.action = "/create.php"
|
160
|
+
s.block = lambda { |body| URI.extract(body).grep(/0rz/)[0] }
|
85
161
|
}
|
86
|
-
},
|
87
|
-
|
88
|
-
:metamark => Service.new("metamark.net") { |s|
|
89
|
-
s.action = "/add"
|
90
|
-
s.field = "long_url"
|
91
|
-
s.block = lambda { |body| URI.extract(body).grep(/xrl.us/)[0] }
|
92
|
-
},
|
93
|
-
|
94
|
-
:makeashorterlink => Service.new("makeashorterlink.com") { |s|
|
95
|
-
s.action = "/index.php"
|
96
|
-
s.block = lambda { |body| URI.extract(body).grep(/makeashorterlink/)[0] }
|
97
|
-
},
|
98
|
-
|
99
|
-
:skinnylink => Service.new("skinnylink.com") { |s|
|
100
|
-
s.block = lambda { |body| URI.extract(body).grep(/skinnylink/)[0] }
|
101
|
-
},
|
102
|
-
|
103
|
-
:linktrim => Service.new("linktrim.com") { |s|
|
104
|
-
s.method = :get
|
105
|
-
s.action = "/lt/generate"
|
106
|
-
s.block = lambda { |body| URI.extract(body).grep(/\/linktrim/)[1] }
|
107
|
-
},
|
108
|
-
|
109
|
-
:shorterlink => Service.new("shorterlink.com") { |s|
|
110
|
-
s.method = :get
|
111
|
-
s.action = "/add_url.html"
|
112
|
-
s.block = lambda { |body| URI.extract(body).grep(/shorterlink/)[0] }
|
113
|
-
},
|
114
|
-
|
115
|
-
:minilink => Service.new("minilink.org") { |s|
|
116
|
-
s.method = :get
|
117
|
-
s.block = lambda { |body| URI.extract(body)[-1] }
|
118
|
-
},
|
119
|
-
|
120
|
-
:lns => Service.new("ln-s.net") { |s|
|
121
|
-
s.method = :get
|
122
|
-
s.action = "/home/api.jsp"
|
123
|
-
s.block = lambda { |body| URI.extract(body)[0] }
|
124
|
-
},
|
125
|
-
|
126
|
-
:fyad => Service.new("fyad.org") { |s|
|
127
|
-
s.method = :get
|
128
|
-
s.block = lambda { |body| URI.extract(body).grep(/fyad.org/)[2] }
|
129
|
-
},
|
130
|
-
|
131
|
-
:d62 => Service.new("d62.net") { |s|
|
132
|
-
s.method = :get
|
133
|
-
s.block = lambda { |body| URI.extract(body)[0] }
|
134
|
-
},
|
135
|
-
|
136
|
-
:shiturl => Service.new("shiturl.com") { |s|
|
137
|
-
s.method = :get
|
138
|
-
s.action = "/make.php"
|
139
|
-
s.block = lambda { |body| URI.extract(body).grep(/shiturl/)[0] }
|
140
|
-
},
|
141
|
-
|
142
|
-
:littlink => Service.new("littlink.com") { |s|
|
143
|
-
s.block = lambda { |body| URI.extract(body).grep(/littlink/)[0] }
|
144
|
-
},
|
145
|
-
|
146
|
-
:clipurl => Service.new("clipurl.com") { |s|
|
147
|
-
s.action = "/create.asp"
|
148
|
-
s.block = lambda { |body| URI.extract(body).grep(/clipurl/)[0] }
|
149
|
-
},
|
150
|
-
|
151
|
-
:shortify => Service.new("shortify.com") { |s|
|
152
|
-
s.method = :get
|
153
|
-
s.action = "/shorten.php"
|
154
|
-
s.block = lambda { |body| URI.extract(body).grep(/shortify/)[0] }
|
155
|
-
},
|
156
|
-
|
157
|
-
:orz => Service.new("0rz.net") { |s|
|
158
|
-
s.action = "/create.php"
|
159
|
-
s.block = lambda { |body| URI.extract(body).grep(/0rz/)[0] }
|
160
162
|
}
|
161
|
-
}
|
162
163
|
|
163
|
-
|
164
|
-
|
165
|
-
|
164
|
+
# Array containing symbols representing all the implemented URL
|
165
|
+
# shortening services
|
166
|
+
@@valid_services = @@services.keys
|
166
167
|
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
168
|
+
# Returns @@valid_services
|
169
|
+
def self.valid_services
|
170
|
+
@@valid_services
|
171
|
+
end
|
171
172
|
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
173
|
+
# Main method of ShortURL, its usage is quite simple, just give an
|
174
|
+
# url to shorten and an optional service. If no service is
|
175
|
+
# selected, RubyURL.com will be used. An invalid service symbol
|
176
|
+
# will raise an ArgumentError exception
|
177
|
+
#
|
178
|
+
# Valid +service+ values:
|
179
|
+
#
|
180
|
+
# * <tt>:rubyurl</tt>
|
181
|
+
# * <tt>:tinyurl</tt>
|
182
|
+
# * <tt>:shorl</tt>
|
183
|
+
# * <tt>:snipurl</tt>
|
184
|
+
# * <tt>:metamark</tt>
|
185
|
+
# * <tt>:makeashorterlink</tt>
|
186
|
+
# * <tt>:skinnylink</tt>
|
187
|
+
# * <tt>:linktrim</tt>
|
188
|
+
# * <tt>:shorterlink</tt>
|
189
|
+
# * <tt>:minlink</tt>
|
190
|
+
# * <tt>:lns</tt>
|
191
|
+
# * <tt>:fyad</tt>
|
192
|
+
# * <tt>:d62</tt>
|
193
|
+
# * <tt>:shiturl</tt>
|
194
|
+
# * <tt>:littlink</tt>
|
195
|
+
# * <tt>:clipurl</tt>
|
196
|
+
# * <tt>:shortify</tt>
|
197
|
+
# * <tt>:orz</tt>
|
198
|
+
#
|
199
|
+
# call-seq:
|
200
|
+
# ShortURL.shorten("http://mypage.com") => Uses RubyURL
|
201
|
+
# ShortURL.shorten("http://mypage.com", :tinyurl)
|
202
|
+
def self.shorten(url, service = :rubyurl)
|
203
|
+
if valid_services.include? service
|
204
|
+
@@services[service].call(url)
|
205
|
+
else
|
206
|
+
raise InvalidService
|
207
|
+
end
|
206
208
|
end
|
207
209
|
end
|
208
210
|
end
|
data/test/tc_service.rb
CHANGED
data/test/tc_shorturl.rb
CHANGED
metadata
CHANGED