Method: Fusion::Basic#get_remote_file

Defined in:
lib/basic.rb

#get_remote_file(url) ⇒ Object

Raises:

  • (Exception)


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/basic.rb', line 100

def get_remote_file(url)
  filename = CGI.escape(url)
  local_directory = File.join(@bundle_options[:project_path], ".remote")
  local_file_path = File.join(local_directory, filename)
  
  return local_file_path if File.exists?(local_file_path)
  
  @log.debug "Fetching remote file (#{url})"

  m = Mechanize.new
  response = m.get(url)

  raise Exception.new("Error downloading file (#{url}) -- returned code #{repsonse.code}") unless response.code == "200"
  
  @log.debug "Got file (#{url})"

  unless Dir.exists?(local_directory)
    Dir.mkdir(local_directory)
  end
              
  File.open(local_file_path,"w") {|f| f << response.body}
  
  local_file_path
end