Class: TenderSummary::Cli

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Cli

Returns a new instance of Cli.



6
7
8
# File 'lib/tender_summary/cli.rb', line 6

def initialize(argv)
  @argv = argv.dup
end

Instance Attribute Details

#fromObject

Returns the value of attribute from.



4
5
6
# File 'lib/tender_summary/cli.rb', line 4

def from
  @from
end

#outputObject

Returns the value of attribute output.



4
5
6
# File 'lib/tender_summary/cli.rb', line 4

def output
  @output
end

#toObject

Returns the value of attribute to.



4
5
6
# File 'lib/tender_summary/cli.rb', line 4

def to
  @to
end

Instance Method Details

#parseObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/tender_summary/cli.rb', line 23

def parse
  opts = OptionParser.new do |opts|
    opts.banner = "Usage: #{File.basename($0)} [options]"

    opts.separator ' '
    opts.separator 'Specific options:'

    opts.on("-s", "--subdomain SUBDOMAIN", "Tender subdomain") do |n|
      TenderSummary::TenderApi.subdomain = n
    end

    opts.on("-u", "--username USERNAME", "Tender username") do |n|
      TenderSummary::TenderApi.username = n
    end

    opts.on("-p", "--password PASSWORD", "Tender password") do |n|
      TenderSummary::TenderApi.password = n
    end

    opts.on("-t", "--to a,b,c", Array, "Users to email") do |n|
      self.to = n
    end

    opts.on("-f", "--from FROM", "Email address to use for From") do |n|
      self.from = n
    end

    opts.on("-o", "--output FILE", "Output HTML to file instead of sending") do |n|
      self.output = n
    end

    opts.separator " "
    opts.separator "Common options:"

    # No argument, shows at tail.  This will print an options summary.
    # Try it and see!
    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end
  end

  opts.parse!(@argv)

  TenderSummary::TenderApi.authenticate
end

#runObject



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/tender_summary/cli.rb', line 10

def run
  self.parse

  if output.present?
    mail = TenderSummary::Mailer.create_pending(self.to, self.from)
    open(output, 'w') do |io|
      io << mail.parts.first.body
    end
  else
    TenderSummary::Mailer.deliver_pending(self.to, self.from)
  end
end