Class: Pantry::Commands::UploadFile

Inherits:
Pantry::Command show all
Defined in:
lib/pantry/commands/upload_file.rb

Overview

Base class for any command that needs to upload a single file where that file is small enough for its contents to be passed around in plain messages. For larger files that shouldn’t be pulled entirely into memory, please use #send_file and #receive_file instead.

This class is not used directly. Subclass to define the CLI command pattern and the directory where the uploaded file will end up.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Pantry::Command

command, #finished, #finished?, message_type, #receive_client_response, #receive_response, #send_request, #send_request!, #server_or_client, #server_or_client=, #to_message, #wait_for_finish

Constructor Details

#initialize(file_to_upload = nil) ⇒ UploadFile

Returns a new instance of UploadFile.



15
16
17
# File 'lib/pantry/commands/upload_file.rb', line 15

def initialize(file_to_upload = nil)
  @file_to_upload = file_to_upload
end

Instance Attribute Details

#file_to_uploadObject (readonly)

Returns the value of attribute file_to_upload.



13
14
15
# File 'lib/pantry/commands/upload_file.rb', line 13

def file_to_upload
  @file_to_upload
end

Instance Method Details

#perform(message) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/pantry/commands/upload_file.rb', line 46

def perform(message)
  cmd_options = message.body[0]
  file_name   = message.body[1]
  file_body   = message.body[2]

  upload_dir = upload_directory(cmd_options)

  FileUtils.mkdir_p(upload_dir)
  File.open(upload_dir.join(file_name), "w+") do |file|
    file.write file_body
  end

  true
end

#prepare_message(options) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pantry/commands/upload_file.rb', line 32

def prepare_message(options)
  required_options.each do |required|
    unless options[required]
      raise Pantry::MissingOption, "Required option #{required} is missing"
    end
  end

  super.tap do |message|
    message << options
    message << File.basename(@file_to_upload)
    message << File.read(@file_to_upload)
  end
end

#receive_server_response(response) ⇒ Object



61
62
63
# File 'lib/pantry/commands/upload_file.rb', line 61

def receive_server_response(response)
  # Say nothing. Finishing is enough
end

#required_optionsObject

Specify any required options for this Command by long-name For example, to require the base APPLICATION option, return %i(application) Does not matter if the list is of strings or symbols.



28
29
30
# File 'lib/pantry/commands/upload_file.rb', line 28

def required_options
  []
end

#upload_directory(application) ⇒ Object

Specify the directory this file should be written to When applicable, application is the application that should know about this file



21
22
23
# File 'lib/pantry/commands/upload_file.rb', line 21

def upload_directory(application)
  raise "Must implement #upload_directory in subclass"
end