class Downspout::Tmpfile

Public Class Methods

Downspout::Tmpfile.new( :name => 'desired-file-name.txt', :prefix → 'my-app' ) click to toggle source

accepts an options hash which can include either or both of :name and :prefix then creates a Tempfile with the optionally given name in a unique sub-folder of the configured directory, optionally named with the prefix string. The unique folder name includes the prefix, a sortable date, the PID of the download process, and a randomly generated sequence of characters.

=> "/tmp/downloads/my-app-20110203-59488-1run8k2-0/desired-file-name.txt"
Calls superclass method
# File lib/downspout/tmp_file.rb, line 19
def initialize( options = nil )
  # make sure the configured directory exists
  FileUtils.mkdir_p( Downspout::Config.tmp_dir )

  defaults = {:prefix => Downspout::Config.default_prefix, :name => "downloaded_file.tmp"}

  # overwrite defaults with given options
  defaults.merge!( options ) unless options.nil?

  # create a unique file path from the given options
  unique_path = File.join( Downspout::Config.tmp_dir, tmp_dir_name( defaults[:prefix] ), defaults[:name] )

  # make sure the unique directory exists
  $logger.debug("downspout | tmpfile | initialize | Creating unique directory : #{File.dirname(unique_path)}")
  FileUtils.mkdir_p( File.dirname( unique_path ) )
  raise "MakeDir Error" unless File.exist?( File.dirname( unique_path ) )
  
  super( unique_path, File::CREAT, 0644 )

end

Private Instance Methods

tmp_dir_name( prefix, n=rand(9) ) click to toggle source
# File lib/downspout/tmp_file.rb, line 87
def tmp_dir_name( prefix, n=rand(9) )
  t = Time.now.strftime("%Y%m%d")
  path = "#{prefix}-#{t}-#{$$}-#{rand(0x100000000).to_s(36)}-#{n}"
end