class HammerCLIForeman::DownloadCommand

Public Class Methods

command_name(name = nil) click to toggle source
Calls superclass method
# File lib/hammer_cli_foreman/commands.rb, line 682
def self.command_name(name = nil)
  super(name) || "download"
end

Public Instance Methods

default_filename() click to toggle source
# File lib/hammer_cli_foreman/commands.rb, line 704
def default_filename
  "Downloaded-#{Time.new.strftime("%Y-%m-%d")}.txt"
end
execute() click to toggle source
# File lib/hammer_cli_foreman/commands.rb, line 693
def execute
  response = send_request
  if option_path
    filepath = store_response(response)
    print_message(_('The response has been saved to %{path}s.'), {:path => filepath})
  else
    puts response.body
  end
  return HammerCLI::EX_OK
end
request_options() click to toggle source
# File lib/hammer_cli_foreman/commands.rb, line 686
def request_options
  { :response => :raw }
end

Private Instance Methods

store_response(response) click to toggle source
# File lib/hammer_cli_foreman/commands.rb, line 710
def store_response(response)
  if response.headers.key?(:content_disposition)
    suggested_filename = response.headers[:content_disposition].match(/filename="(.*)"/)
  end
  filename = suggested_filename ? suggested_filename[1] : default_filename
  path = option_path.dup
  path << '/' unless path.end_with? '/'
  raise _("Cannot save file: %s does not exist") % path unless File.directory?(path)
  filepath = path + filename
  File.write(filepath, response.body)
  filepath
end