Class: PDK::Util::VendoredFile

Inherits:
Object
  • Object
show all
Includes:
Filesystem
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

Methods included from Filesystem

directory?, exist?, expand_path, file?, fnmatch, glob, mkdir_p, read_file, readable?, write_file

Constructor Details

#initialize(file_name, url) ⇒ VendoredFile

Returns a new instance of VendoredFile.



28
29
30
31
# File 'lib/pdk/util/vendored_file.rb', line 28

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.



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

def file_name
  @file_name
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

#readObject



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

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))
  write_file(gem_vendored_path, content)
  content
end