unshorten 0.1.3 → 0.2.0

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.
Files changed (3) hide show
  1. data/lib/unshorten.rb +6 -2
  2. data/test/test_unshorten.rb +15 -0
  3. metadata +3 -3
data/lib/unshorten.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'net/http'
2
+ require 'net/https'
2
3
  require 'uri'
3
4
 
4
5
  # Get original URLs from shortened ones.
@@ -28,7 +29,8 @@ class Unshorten
28
29
  # @option options [Integer] :timeout Timeout in seconds, for every request
29
30
  # @option options [Regexp] :short_hosts Hosts that provides short url
30
31
  # services, only send requests if
31
- # host matches this regexp
32
+ # host matches this regexp. Set to false
33
+ # to follow all redirects.
32
34
  # @option options [Boolean] :use_cache Use cached result if available
33
35
  # @option options [Boolean] :add_missing_http add 'http://' if missing
34
36
  # @see DEFAULT_OPTIONS
@@ -70,10 +72,12 @@ class Unshorten
70
72
 
71
73
  uri = URI.parse(url) rescue nil
72
74
 
73
- return url if uri.nil? or not uri.host =~ options[:short_hosts]
75
+ return url if uri.nil?
76
+ return url if options[:short_hosts] != false and not uri.host =~ options[:short_hosts]
74
77
 
75
78
  http = Net::HTTP.new(uri.host, uri.port)
76
79
  http.read_timeout = options[:timeout]
80
+ http.use_ssl = true if uri.scheme == "https"
77
81
 
78
82
  response = http.request_head(uri.path.empty? ? '/' : uri.path) rescue nil
79
83
 
@@ -24,4 +24,19 @@ class UnshortenTest < Test::Unit::TestCase
24
24
  assert_equal SHORTENED_URL, Unshorten.unshorten(SHORTENED_URL, :short_hosts => /jmp/, :use_cache => false)
25
25
  end
26
26
 
27
+ HTTPS_LINK_1_FULL = 'https://github.com/aaronpk/unshorten'
28
+ HTTPS_LINK_1_SHORT = 'https://t.co/5204FqAr'
29
+
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
+
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
37
+
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
+
27
42
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unshorten
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-10 00:00:00.000000000 Z
12
+ date: 2013-01-03 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Get original URLs from shortened ones
15
15
  email: quark@lihdd.net
@@ -41,7 +41,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
41
41
  version: '0'
42
42
  requirements: []
43
43
  rubyforge_project:
44
- rubygems_version: 1.8.11
44
+ rubygems_version: 1.8.23
45
45
  signing_key:
46
46
  specification_version: 3
47
47
  summary: Unshorten URLs