uri_shortener 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/uri_shortener.rb +57 -57
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b966cb6946f7e50e39bd74caa459f726f1803b69
4
- data.tar.gz: c9284af74a8a93eb6ba30cc4857e4ff88c85b096
3
+ metadata.gz: 0ad13d4ed5e40c667c053523fc9f15478805d62b
4
+ data.tar.gz: b7cdf105e506126770046406c965d34d1f586ca5
5
5
  SHA512:
6
- metadata.gz: 8d7f43d0a9af03db43240e62788e99491e18e8fe0cd190d563ff4b9140ddf2cbc9290e1ca94a8e698337b5bca53bebaea89d18bb6b3faeeb844f3061b7753328
7
- data.tar.gz: db294d9d8545862262cc4165274157249bfca3da1db7f464617070bdf7cc8bbc78554c976454d886b6ff237930d9a9f7dbc1bd8a3a03d2858b851dc9554e1f52
6
+ metadata.gz: ef5783e737f9fd034dc235ce18a31cd4cc786ab23d6217c6686d9b17fcb3ee07edfff8e4cd7aa6d251bc67990943be8af91880e13c45b3b144969f5e1bb6b703
7
+ data.tar.gz: d4d34746b76f25151d773792a493a681983469d95bac502c7fae55c5367dcbe11bfbcadee7bd9c2cd0d7a93097b3400a8b461dee9ff99a1b76ae6377e61e552a
data/lib/uri_shortener.rb CHANGED
@@ -1,57 +1,57 @@
1
- # -*- coding: utf-8 -*-
2
- require 'net/http'
3
- require 'json'
4
- class UriShortener
5
- attr :long_uri
6
- attr :short_uri
7
- attr_accessor :type
8
-
9
- def initialize long_uri, type="t.cn"
10
- @long_uri = URI.encode(long_uri.gsub /http:\/\/|https:\/\//,"")
11
- @type = type # t.cn || rdcnzz || url.cn || 126.am
12
- @short_uri = ""
13
- end
14
-
15
- def shorten
16
- @short_uri = case @type
17
- when "t.cn" then t_cn @long_uri
18
- when "url.cn" then url_cn @long_uri
19
- when "rdcnzz" then rdcnzz @long_uri
20
- when "126.am" then am @long_uri
21
- else puts "Not correct type"
22
- end
23
- end
24
-
25
- def verify
26
- begin
27
- res = Net::HTTP.get_response URI(@short_uri)
28
- URI("http://"+@long_uri) == URI(URI.encode res["location"])
29
- rescue
30
- false
31
- end
32
- end
33
-
34
- private
35
- def t_cn long_uri
36
- response = Net::HTTP.post_form(URI("http://tinyurl.duapp.com/create.php"), {:url=>(URI.encode_www_form_component URI.decode(long_uri)), :type=>"t.cn"})
37
- result = JSON.parse response.body
38
- result['data']
39
- end
40
-
41
- def url_cn long_uri
42
- response = Net::HTTP.get(URI("http://121.199.13.65/web/json.php?type=url.cn&xzurl=#{long_uri}"))
43
- (/http\:\/\/\w+\.\w+\/\w+/.match response).to_s
44
- end
45
-
46
- def rdcnzz long_uri
47
- response = Net::HTTP.get(URI("http://rdcnzz.com/v1/data/link_conv/rd_dispatcher.php?orig_link=#{long_uri}"))
48
- end
49
-
50
- def am long_uri
51
- response = Net::HTTP.post_form(URI("http://126.am/api!shorten.action"), {:key=>"d1f04c518037412988e26e449469106f", :longUrl=>(URI.decode "http://"+long_uri)})
52
- result = JSON.parse response.body
53
- "http://"+result["url"]
54
- end
55
-
56
- end
57
- # 126.am api key: d1f04c518037412988e26e449469106f
1
+ # -*- coding: utf-8 -*-
2
+ require 'net/http'
3
+ require 'json'
4
+ class UriShortener
5
+ attr :long_uri
6
+ attr :short_uri
7
+ attr_accessor :type
8
+
9
+ def initialize long_uri, type="t.cn"
10
+ @long_uri = URI.encode(long_uri.gsub /^http:\/\/|https:\/\//,"")
11
+ @type = type # t.cn || rdcnzz || url.cn || 126.am
12
+ @short_uri = ""
13
+ end
14
+
15
+ def shorten
16
+ @short_uri = case @type
17
+ when "t.cn" then t_cn @long_uri
18
+ when "url.cn" then url_cn @long_uri
19
+ when "rdcnzz" then rdcnzz @long_uri
20
+ when "126.am" then am @long_uri
21
+ else puts "Not correct type"
22
+ end
23
+ end
24
+
25
+ def verify
26
+ begin
27
+ res = Net::HTTP.get_response URI(@short_uri)
28
+ URI("http://"+@long_uri) == URI(URI.encode res["location"])
29
+ rescue
30
+ false
31
+ end
32
+ end
33
+
34
+ private
35
+ def t_cn long_uri
36
+ response = Net::HTTP.post_form(URI("http://tinyurl.duapp.com/create.php"), {:url=>(URI.encode_www_form_component URI.decode(long_uri)), :type=>"t.cn"})
37
+ result = JSON.parse response.body
38
+ result['data']
39
+ end
40
+
41
+ def url_cn long_uri
42
+ response = Net::HTTP.get(URI("http://121.199.13.65/web/json.php?type=url.cn&xzurl=#{long_uri}"))
43
+ (/http\:\/\/\w+\.\w+\/\w+/.match response).to_s
44
+ end
45
+
46
+ def rdcnzz long_uri
47
+ response = Net::HTTP.get(URI("http://rdcnzz.com/v1/data/link_conv/rd_dispatcher.php?orig_link=#{long_uri}"))
48
+ end
49
+
50
+ def am long_uri
51
+ response = Net::HTTP.post_form(URI("http://126.am/api!shorten.action"), {:key=>"d1f04c518037412988e26e449469106f", :longUrl=>(URI.decode "http://"+long_uri)})
52
+ result = JSON.parse response.body
53
+ "http://"+result["url"]
54
+ end
55
+
56
+ end
57
+ # 126.am api key: d1f04c518037412988e26e449469106f
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uri_shortener
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Asakawa Ryu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-31 00:00:00.000000000 Z
11
+ date: 2013-08-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: 'Providing types: t.cn, url.cn, url7.me, 126.am'
14
14
  email: i@nyangawa.me