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)
  warn 'URI#downloadの引数にはPathnameを使いましょう!' if file.is_a?(String)
  binary = OpenURI.open_uri(self).read
  file = file + self.basename if file.to_s[-1] == '/'
  dirname = ::File.dirname(file)
  ::FileUtils.mkdir_p(dirname) unless ::File.directory?(dirname)
  ::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