Sha256: cecd543e3f94143d75f9afa09c70ff3a13e17a7ee580cda1c3f27feab3fb8c40

Contents?: true

Size: 824 Bytes

Versions: 2

Compression:

Stored size: 824 Bytes

Contents

# frozen_string_literal: true

require "uri"
require "fileutils"
require "open-uri"
require "net/http"

module FeCoreExt::CoreExt
end

module URI
  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

  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
end

class URI::Generic
  def basename
    ::File.basename(path)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fe_core_ext-0.30.0 lib/fe_core_ext/core_ext/uri.rb
fe_core_ext-0.29.1 lib/fe_core_ext/core_ext/uri.rb