homebrew-cask-versions/lib/cask/underscore_supporting_uri.rb
Paul Hinze f6f507b289 some refactoring around the new download strategy
- move the interface from top-level methods to hash arguments of URL to
   keep the cask DSL as skinny as possible
 - promote the Cask::Headers object up to a Cask::URL object that
   encapsulates all infornation about the URL
 - pull all knowledge about curl arguments into the DownloadStrategy,
   leaving URL to act as a value object to be queried for details
 - test at the DownloadStrategy level; setting up expected curl args
   and example casks
2013-12-15 13:18:47 -06:00

27 lines
691 B
Ruby

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