unshorten 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +6 -14
  2. data/lib/unshorten.rb +13 -5
  3. data/test/test_unshorten.rb +39 -28
  4. metadata +36 -9
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- ZTA5ZWQ3MDQ1YWM5YjVkMWE4ZWM0MWRlNmIxNTAyM2QyYmE1Njg2Mw==
5
- data.tar.gz: !binary |-
6
- MTExOThkNzU4YzhlM2VmYWYzMmVlZjg4NGQyZTIyOTkxZWYzZDI2NQ==
7
- !binary "U0hBNTEy":
8
- metadata.gz: !binary |-
9
- ZjFkZDBiYmNjOGY0MDI3MDFlM2VlYmU1MDE5OTNjODk2YzUxNDBmMzJjMmZj
10
- MWEzMDBhZmRjNzExM2ZkOGE2MTU2MTU0ZDUzYjJjNjgwYTkyM2Q0MjI3ZDVh
11
- NGFiYzgzNGE3ZDAwNWYzZTQ5NDU1MzQyODk0YzAxNjYyM2EyNjU=
12
- data.tar.gz: !binary |-
13
- Y2FkZGY5NDU2NGI2ZDI4ZGMyOTVhYTJhNzIwNjU2MGM3MzFiODY3M2E3OWQ4
14
- ODQ5YmVjZjNkMjM3MjMxNGFlZGQwZDk5Y2I3ZDdkNWU0MWZmM2QyZWVkODRk
15
- MDNkYTIwNGE2OTdiNjY5Y2Q4OTNkYzQxNDkyMGI3NjJhY2Y4ODY=
2
+ SHA1:
3
+ metadata.gz: e9b5c2d553a05779d8a45cec5d288152a2ee9840
4
+ data.tar.gz: 4a0c1dfe4bbfe38e0c276023d1f1d1a64a0e9465
5
+ SHA512:
6
+ metadata.gz: 40481e8187e84d1d7921ae79b52836e9033813d0cebf6b27c6b1b7e331c28eb8ad3677e8b1ae03cee2402e180c28cc2c6d58cd16a315afbba08933280d908c04
7
+ data.tar.gz: e4f1bd8db26b1801c8ffc57f503a0882197633faf9b95733fd2ecd32b1bfaf6c38b05f338b0622fd6669b360b3cedc50728be96ebda1473e4c41da5539f8bb3f
@@ -12,7 +12,8 @@ class Unshorten
12
12
  DEFAULT_OPTIONS = {
13
13
  :max_level => 10,
14
14
  :timeout => 2,
15
- :short_hosts => Regexp.union('url', /^[^.]{1,6}\.[^.]{1,2}$/i),
15
+ :short_hosts => false,
16
+ :short_urls => /^(?:https?:)?\/*[^\/]*\/*[^\/]*$/,
16
17
  :use_cache => true,
17
18
  :add_missing_http => true
18
19
  }
@@ -29,8 +30,12 @@ class Unshorten
29
30
  # @option options [Integer] :timeout Timeout in seconds, for every request
30
31
  # @option options [Regexp] :short_hosts Hosts that provides short url
31
32
  # services, only send requests if
32
- # host matches this regexp. Set to false
33
- # to follow all redirects.
33
+ # host matches this regexp. Set to
34
+ # nil to follow all redirects.
35
+ # @option options [Regexp] :short_urls URLs that looks like a short one.
36
+ # Only send requests when the URL
37
+ # match this regexp. Set to nil to
38
+ # follow all redirects.
34
39
  # @option options [Boolean] :use_cache Use cached result if available
35
40
  # @option options [Boolean] :add_missing_http add 'http://' if missing
36
41
  # @see DEFAULT_OPTIONS
@@ -69,11 +74,12 @@ class Unshorten
69
74
 
70
75
  return @@cache[url] if options[:use_cache] and @@cache[url]
71
76
  return url if level >= options[:max_level]
77
+ return url if options[:short_urls] && ! (url =~ options[:short_urls])
72
78
 
73
79
  uri = URI.parse(url) rescue nil
74
80
 
75
81
  return url if uri.nil?
76
- return url if options[:short_hosts] != false and not uri.host =~ options[:short_hosts]
82
+ return url if options[:short_hosts] && ! (uri.host =~ options[:short_hosts])
77
83
 
78
84
  http = Net::HTTP.new(uri.host, uri.port)
79
85
  http.open_timeout = options[:timeout]
@@ -84,7 +90,9 @@ class Unshorten
84
90
 
85
91
  if response.is_a? Net::HTTPRedirection and response['location'] then
86
92
  expire_cache if @@cache.size > CACHE_SIZE_LIMIT
87
- @@cache[url] = follow(response['location'], options, level + 1)
93
+ location = response['location']
94
+ location = (uri + location).to_s if location
95
+ @@cache[url] = follow(location, options, level + 1)
88
96
  else
89
97
  url
90
98
  end
@@ -1,42 +1,53 @@
1
1
  # coding: utf-8
2
2
 
3
- require 'test/unit'
3
+ require 'minitest/autorun'
4
4
  require 'unshorten'
5
5
 
6
- class UnshortenTest < Test::Unit::TestCase
7
- ORIGINAL_URL = 'http://dir.yahoo.com/Reference/Libraries/Library_and_Information_Science/Metadata/URIs___Universal_Resource_Identifiers/URLs___Uniform_Resource_Locators/URL_Shortening/'
8
- SHORTENED_URL = 'http://tinyurl.com/j'
6
+ class UnshortenTest < MiniTest::Test
7
+ ORIGINAL_URL = 'http://dir.yahoo.com/Reference/Libraries/Library_and_Information_Science/Metadata/URIs___Universal_Resource_Identifiers/URLs___Uniform_Resource_Locators/URL_Shortening/'
8
+ SHORTENED_URL = 'http://tinyurl.com/j'
9
9
 
10
- def test_unshorten_alias
11
- assert_equal ORIGINAL_URL, Unshorten[SHORTENED_URL, :use_cache => false]
12
- end
10
+ def test_unshorten_alias
11
+ assert_equal ORIGINAL_URL, Unshorten[SHORTENED_URL, :use_cache => false]
12
+ end
13
13
 
14
- def test_illegal_urls
15
- illegal_url = 'http://a汉字bc.那么/是非法的?url'
16
- assert_equal illegal_url, Unshorten.unshorten(illegal_url, :use_cache => false)
17
- end
14
+ def test_illegal_urls
15
+ illegal_url = 'http://a汉字bc.那么/是非法的?url'
16
+ assert_equal illegal_url, Unshorten.unshorten(illegal_url, :use_cache => false)
17
+ end
18
18
 
19
- def test_option_max_level
20
- assert_equal SHORTENED_URL, Unshorten.unshorten(SHORTENED_URL, :max_level => 0, :use_cache => false)
21
- end
19
+ def test_option_max_level
20
+ assert_equal SHORTENED_URL, Unshorten.unshorten(SHORTENED_URL, :max_level => 0, :use_cache => false)
21
+ end
22
22
 
23
- def test_option_short_hosts
24
- assert_equal SHORTENED_URL, Unshorten.unshorten(SHORTENED_URL, :short_hosts => /jmp/, :use_cache => false)
25
- end
23
+ def test_option_short_hosts
24
+ assert_equal SHORTENED_URL, Unshorten.unshorten(SHORTENED_URL, :short_hosts => /jmp/, :use_cache => false)
25
+ end
26
26
 
27
- HTTPS_LINK_1_FULL = 'https://github.com/aaronpk/unshorten'
28
- HTTPS_LINK_1_SHORT = 'https://t.co/5204FqAr'
27
+ def test_option_short_urls
28
+ assert_equal SHORTENED_URL, Unshorten.unshorten(SHORTENED_URL, :short_urls => /DONT_MATCH/, :use_cache => false)
29
+ dummy_url = 'http://a.com/b/c'
30
+ assert_equal dummy_url, Unshorten.unshorten(dummy_url, :use_cache => false)
31
+ end
29
32
 
30
- def test_https_link
31
- assert_equal HTTPS_LINK_1_FULL, Unshorten.unshorten(HTTPS_LINK_1_SHORT, :short_hosts => /./, :use_cache => false)
32
- end
33
+ HTTPS_LINK_1_FULL = 'https://github.com/aaronpk/unshorten'
34
+ HTTPS_LINK_1_SHORT = 'https://t.co/5204FqAr'
33
35
 
34
- def test_follow_all_redirects
35
- assert_equal HTTPS_LINK_1_FULL, Unshorten.unshorten(HTTPS_LINK_1_SHORT, :short_hosts => false, :use_cache => false)
36
- end
36
+ def test_https_link
37
+ assert_equal HTTPS_LINK_1_FULL, Unshorten.unshorten(HTTPS_LINK_1_SHORT, :short_hosts => /./, :use_cache => false)
38
+ end
39
+
40
+ def test_follow_all_redirects
41
+ assert_equal HTTPS_LINK_1_FULL, Unshorten.unshorten(HTTPS_LINK_1_SHORT, :short_hosts => false, :use_cache => false)
42
+ end
43
+
44
+ def test_only_tco_links
45
+ assert_equal "http://aaron.pk/649", Unshorten.unshorten(HTTPS_LINK_1_SHORT, :short_hosts => /t\.co/, :use_cache => false)
46
+ end
47
+
48
+ def test_relative_url
49
+ assert_equal 'http://waterpigs.co.uk/articles/getting-started-with-microformats2/', Unshorten.unshorten('http://bit.ly/1l1Dz6K', :short_urls => nil, :use_cache => false)
50
+ end
37
51
 
38
- def test_only_tco_links
39
- assert_equal "http://aaron.pk/649", Unshorten.unshorten(HTTPS_LINK_1_SHORT, :short_hosts => /t\.co/, :use_cache => false)
40
- end
41
52
 
42
53
  end
metadata CHANGED
@@ -1,15 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unshorten
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
- - Wu Jun
7
+ - Jun Wu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-04-05 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2014-03-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
13
41
  description: Get original URLs from shortened ones
14
42
  email: quark@lihdd.net
15
43
  executables:
@@ -17,9 +45,9 @@ executables:
17
45
  extensions: []
18
46
  extra_rdoc_files: []
19
47
  files:
48
+ - bin/unshorten
20
49
  - lib/unshorten.rb
21
50
  - test/test_unshorten.rb
22
- - bin/unshorten
23
51
  homepage: https://github.com/quark-zju/unshorten
24
52
  licenses: []
25
53
  metadata: {}
@@ -29,20 +57,19 @@ require_paths:
29
57
  - lib
30
58
  required_ruby_version: !ruby/object:Gem::Requirement
31
59
  requirements:
32
- - - ! '>='
60
+ - - ">="
33
61
  - !ruby/object:Gem::Version
34
62
  version: '0'
35
63
  required_rubygems_version: !ruby/object:Gem::Requirement
36
64
  requirements:
37
- - - ! '>='
65
+ - - ">="
38
66
  - !ruby/object:Gem::Version
39
67
  version: '0'
40
68
  requirements: []
41
69
  rubyforge_project:
42
- rubygems_version: 2.0.3
70
+ rubygems_version: 2.2.0
43
71
  signing_key:
44
72
  specification_version: 4
45
73
  summary: Unshorten URLs
46
74
  test_files:
47
75
  - test/test_unshorten.rb
48
- has_rdoc: