class LocalResource::Instance

Public Class Methods

new(url, tmp_filename) click to toggle source
# File lib/local_resource/instance.rb, line 6
def initialize(url, tmp_filename)
  @url = url
  @tmp_file_name = File.basename(tmp_filename, ".*")
  @tmp_file_ext = File.extname(tmp_filename)
end
tmp_dir() click to toggle source
# File lib/local_resource/instance.rb, line 26
def self.tmp_dir
  path = Rails.root.join('tmp/local_resources')
  FileUtils::mkdir_p(path.to_s).first
end

Public Instance Methods

destroy() click to toggle source
# File lib/local_resource/instance.rb, line 20
def destroy
  return unless @file
  @file.delete
  @file = nil
end
file() click to toggle source
# File lib/local_resource/instance.rb, line 12
def file
  @file ||= load_tempfile
end
file_path() click to toggle source
# File lib/local_resource/instance.rb, line 16
def file_path
  file.path
end

Private Instance Methods

load_downloaded_data() click to toggle source
# File lib/local_resource/instance.rb, line 33
def load_downloaded_data
  open(@url, allow_redirections: :all) do |io|
    return io.read
  end
end
load_tempfile() click to toggle source
# File lib/local_resource/instance.rb, line 39
def load_tempfile
  filename = [@tmp_file_name, @tmp_file_ext]
  data = load_downloaded_data

  Tempfile.new(filename, self.class.tmp_dir, encoding: data.encoding).tap do |f|
    f.write(data)
    f.close
  end
end