uri_shortener 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/uri_shortener.rb +38 -28
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 96f672757995879e46765694bd6134698fdc1d50
|
4
|
+
data.tar.gz: 88f7851064871f2a2c848ac99c51234d03d6e49d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 490ea2d112595b2357ea729b210e2958e94072291714255595bad631ea338729beafb554b39c649d55d83e5f4b0c37e5754445d651ca2c014b735cef011985f7
|
7
|
+
data.tar.gz: 3bcef9683245e0bbd7efeb61e33fa08628cfba911354f7e915f8af0e530e38dfd57776bb339d881dc7dc50390b98aec9f98c764a1b3da686b5a2795153ad97e2
|
data/lib/uri_shortener.rb
CHANGED
@@ -1,37 +1,47 @@
|
|
1
1
|
require 'net/http'
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
2
|
+
class UriShortener
|
3
|
+
attr :long_uri
|
4
|
+
attr :short_uri
|
5
|
+
attr_accessor :type
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
7
|
+
def initialize long_uri, type="t.cn"
|
8
|
+
@long_uri = long_uri.gsub "http://",""
|
9
|
+
@type = type # t.cn || rdcnzz || url.cn
|
10
|
+
@short_uri = ""
|
11
|
+
end
|
12
|
+
|
13
|
+
def shorten
|
14
|
+
@short_uri = case @type
|
15
|
+
when "t.cn" then t_cn @long_uri
|
16
|
+
when "url.cn" then url_cn @long_uri
|
17
|
+
when "rdcnzz" then rdcnzz @long_uri
|
18
|
+
else puts "Not correct type"
|
19
|
+
end
|
20
|
+
end
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
22
|
+
def verify
|
23
|
+
begin
|
24
|
+
res = Net::HTTP.get_response URI(@short_uri)
|
25
|
+
URI("http://"+@long_uri) == URI(res["location"])
|
26
|
+
rescue
|
27
|
+
false
|
25
28
|
end
|
29
|
+
end
|
26
30
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
+
private
|
32
|
+
def t_cn long_uri
|
33
|
+
response = Net::HTTP.post_form(URI("http://tinyurl.duapp.com/url.php"), {:url=>URI.encode_www_form_component(long_uri.gsub "http://", ""), :type=>"t.cn"})
|
34
|
+
response.body
|
35
|
+
end
|
31
36
|
|
32
|
-
|
33
|
-
|
34
|
-
|
37
|
+
def url_cn long_uri
|
38
|
+
response = Net::HTTP.get(URI("http://121.199.13.65/web/json.php?type=url.cn&xzurl=#{URI.encode_www_form_component long_uri}"))
|
39
|
+
(/http\:\/\/\w+\.\w+\/\w+/.match response).to_s
|
40
|
+
end
|
35
41
|
|
42
|
+
def rdcnzz long_uri
|
43
|
+
response = Net::HTTP.get(URI("http://rdcnzz.com/v1/data/link_conv/rd_dispatcher.php?orig_link=#{URI.encode_www_form_component long_uri}"))
|
36
44
|
end
|
45
|
+
|
46
|
+
|
37
47
|
end
|