Browse files

move open-uri redirections patch to a external gem

  • Loading branch information...
1 parent 0a4b51e commit 8a9e70c04833bf3a4de3e4c65294d7043be30910 @jaimeiniesta committed Dec 3, 2012
Showing with 2 additions and 51 deletions.
  1. +0 −1 lib/meta_inspector.rb
  2. +0 −50 lib/meta_inspector/open_uri.rb
  3. +1 −0 lib/meta_inspector/scraper.rb
  4. +1 −0 meta_inspector.gemspec
View
1 lib/meta_inspector.rb
@@ -1,7 +1,6 @@
# -*- encoding: utf-8 -*-
require File.expand_path(File.join(File.dirname(__FILE__), 'meta_inspector/scraper'))
-require File.expand_path(File.join(File.dirname(__FILE__), 'meta_inspector/open_uri'))
module MetaInspector
extend self
View
50 lib/meta_inspector/open_uri.rb
@@ -1,50 +0,0 @@
-# Patch to allow open-uri to follow safe (http to https) and unsafe redirects (https to http).
-# Original gist URL:
-# https://gist.github.com/1271420
-#
-# Relevant issue:
-# http://redmine.ruby-lang.org/issues/3719
-#
-# Source here:
-# https://github.com/ruby/ruby/blob/trunk/lib/open-uri.rb
-
-module OpenURI
- class <<self
- alias_method :open_uri_original, :open_uri
- alias_method :redirectable_cautious?, :redirectable?
-
- def redirectable_safe?(uri1, uri2)
- uri1.scheme.downcase == uri2.scheme.downcase || (uri1.scheme.downcase == "http" && uri2.scheme.downcase == "https")
- end
-
- def redirectable_unsafe?(uri1, uri2)
- !redirectable_safe?(uri1, uri2)
- end
- end
-
- # The original open_uri takes *args but then doesn't do anything with them.
- # Assume we can only handle a hash.
- def self.open_uri(name, options = {})
- redirectable_unsafe = options.delete :allow_unsafe_redirections
- redirectable_safe = options.delete :allow_safe_redirections
-
- if redirectable_unsafe
- class <<self
- remove_method :redirectable?
- alias_method :redirectable?, :redirectable_unsafe?
- end
- elsif redirectable_safe
- class <<self
- remove_method :redirectable?
- alias_method :redirectable?, :redirectable_safe?
- end
- else
- class <<self
- remove_method :redirectable?
- alias_method :redirectable?, :redirectable_cautious?
- end
- end
-
- self.open_uri_original name, options
- end
-end
View
1 lib/meta_inspector/scraper.rb
@@ -1,6 +1,7 @@
# -*- encoding: utf-8 -*-
require 'open-uri'
+require 'open_uri_redirections'
require 'nokogiri'
require 'hashie/rash'
require 'timeout'
View
1 meta_inspector.gemspec
@@ -16,6 +16,7 @@ Gem::Specification.new do |gem|
gem.add_dependency 'nokogiri', '~> 1.5'
gem.add_dependency 'rash', '0.3.2'
+ gem.add_dependency 'open_uri_redirections', '0.0.1'
gem.add_development_dependency 'rspec', '2.12.0'
gem.add_development_dependency 'fakeweb', '1.3.0'

0 comments on commit 8a9e70c

Please sign in to comment.