homebrew-cask-versions/lib/hbc/underscore_supporting_uri.rb
Roland Walker 202d6019f8 Move all code under an Hbc:: namespace
* convert existing Cask:: namespace to Hbc::
* move Homebrew-fork code under Hbc::
* move freestanding classes such as Tty and TopologicalHash under Hbc::
* recast HOMEBREW_CASK_ constants as HBC_
* modify our Homebrew Formula for backward compatibility
* devscripts and dev docs
2015-01-02 07:27:03 -05:00

31 lines
777 B
Ruby

require 'uri'
module Hbc::UnderscoreSupportingURI
def self.parse(maybe_uri)
return nil if maybe_uri.nil?
URI.parse(maybe_uri)
rescue URI::InvalidURIError => e
scheme, host, path = simple_parse(maybe_uri)
if path and host =~ /\_/
URI.parse(without_host_underscores(scheme, host, path)).tap { |uri|
uri.instance_variable_set('@host', host)
}
else
raise e
end
end
def self.simple_parse(maybe_uri)
begin
scheme, host_and_path = maybe_uri.split('://')
host, path = host_and_path.split('/', 2)
[scheme, host, path]
rescue StandardError => e
return nil
end
end
def self.without_host_underscores(scheme, host, path)
["#{scheme}:/", host.gsub(/\_/, '-'), path].join('/')
end
end