Module: URI

Defined in:
lib/fe_core_ext/core_ext/uri.rb

Defined Under Namespace

Classes: Generic

Instance Method Summary collapse

Instance Method Details

#download(file) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/fe_core_ext/core_ext/uri.rb', line 10

def download(file)
  binary = OpenURI.open_uri(self).read
  dirname = ::File.dirname(file)
  unless ::File.directory?(dirname)
    ::FileUtils.mkdir_p(dirname)
  end
  ::File.open(file, 'wb') {|f| f.write(binary)}
end

#exists?Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
# File 'lib/fe_core_ext/core_ext/uri.rb', line 19

def exists?
  req = ::Net::HTTP.new(host, port)
  req.use_ssl = true if scheme == 'https'
  res = req.request_head(path)
  return URI(res['location']).exists? if %w(301 302).include?(res.code)
  res.code == '200'
end