Class: FilePostMonster::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/post_file/cli.rb

Class Method Summary collapse

Class Method Details

.execute(stdout, arguments = []) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/post_file/cli.rb', line 6

def self.execute(stdout, arguments=[])
  options = {
    :file_param => 'file'
  }
  mandatory_options = %w( file address )

  parser = OptionParser.new do |opts|
    opts.banner = "Usage: post_file -f file_name_to_post -a address_to_post_to [options]"
    opts.separator ""
    opts.on("-f", "--file=PATH", String, "File to post (e.g. myfile.txt)") { |arg| options[:file] = arg }
    opts.on("-a", "--address=ADDRESS", String, "Address to post to (e.g. http://myfileservice.com/recieve)") do |arg|
      options[:address] = arg
    end
    opts.on("-p", "--param=PARAM_NAME", String, "Name of the parameter used for the file in the post request (e.g. file_param), DEFAULT => file") do |arg|
      options[:file_param] = arg
    end
    opts.on("-l", "--local_param=PARAM_NAME", String, "Name of a parameter used to post the exact local path with (e.g. local_file_path)") do |arg|
      options[:path_param] = arg
    end
    opts.on("-n", "--nesting=PARAM_NEST", String, "Parameter name you would like other parameters to be nested in") do |arg|
      options[:nesting] = arg
    end
    opts.on("-h", "--help",
            "Show this help message.") { stdout.puts opts; exit }
    opts.parse!(arguments)

    if mandatory_options && mandatory_options.find { |option| options[option.to_sym].nil? }
      stdout.puts opts; exit
    end
  end
  
  post(options)
end

.post(options) ⇒ Object



41
42
43
44
45
46
# File 'lib/post_file/cli.rb', line 41

def self.post(options)
  params = {options[:file_param] => File.new(options[:file])}
  params[options[:path_param]] = File.expand_path(options[:file]) if options[:path_param]
  params = {options[:nesting] => params} if options[:nesting]
  RestClient.post options[:address], params
end