Class: PDK::Util::VendoredFile

Inherits:
Object
  • Object
show all
Defined in:
lib/pdk/util/vendored_file.rb

Defined Under Namespace

Classes: DownloadError

Constant Summary collapse

HTTP_ERRORS =
[
  EOFError,
  Errno::ECONNRESET,
  Errno::EINVAL,
  Errno::ECONNREFUSED,
  Net::HTTPBadResponse,
  Net::HTTPHeaderSyntaxError,
  Net::ProtocolError,
  Timeout::Error,
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name, url) ⇒ VendoredFile

Returns a new instance of VendoredFile.



25
26
27
28
# File 'lib/pdk/util/vendored_file.rb', line 25

def initialize(file_name, url)
  @file_name = file_name
  @url = url
end

Instance Attribute Details

#file_nameObject (readonly)

Returns the value of attribute file_name.



22
23
24
# File 'lib/pdk/util/vendored_file.rb', line 22

def file_name
  @file_name
end

#urlObject (readonly)

Returns the value of attribute url.



23
24
25
# File 'lib/pdk/util/vendored_file.rb', line 23

def url
  @url
end

Instance Method Details

#readObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pdk/util/vendored_file.rb', line 30

def read
  return File.read(package_vendored_path) if PDK::Util.package_install?
  return File.read(gem_vendored_path) if File.file?(gem_vendored_path)

  content = download_file

  # TODO: should only write if it's valid JSON
  # TODO: need a way to invalidate if out of date
  FileUtils.mkdir_p(File.dirname(gem_vendored_path))
  File.open(gem_vendored_path, 'w') do |fd|
    fd.write(content)
  end
  content
end