Class: AtRandom::App

Inherits:
Object
  • Object
show all
Defined in:
lib/at-random/app.rb

Instance Method Summary collapse

Constructor Details

#initialize(argv = []) ⇒ App

Returns a new instance of App.



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
# File 'lib/at-random/app.rb', line 6

def initialize(argv=[])
  @pick_time_opts = {}
  while argv[0] =~ /--(from|to|random-seed)/
    arg = argv.shift
    if arg =~ /--from/
      from_str = arg.length > 6 ? arg[7..-1] : argv.shift
      @pick_time_opts[:from] = from_str
    elsif arg =~ /--to/
      to_str = arg.length > 4 ? arg[5..-1] : argv.shift
      @pick_time_opts[:to] = to_str
    elsif arg =~ /--random-seed/
      @random_seed = (arg.length > 13 ? arg[14..-1] : argv.shift).to_i
    end
  end

  if @pick_time_opts[:from] && @pick_time_opts[:from].length == 2
    @pick_time_opts[:from] += ':00'
  end

  if @pick_time_opts[:to] && @pick_time_opts[:to].length == 2
    @pick_time_opts[:to] += ':59'
  end

  @at_args = argv
end

Instance Method Details

#runObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/at-random/app.rb', line 32

def run
  Kernel.srand(@random_seed) if @random_seed
  @picked_time = PickTime.new @pick_time_opts
  begin
    AtCmd.new(@picked_time.time_s, @at_args).exec
    0
  rescue Exception => e
    $stderr.puts e.message
    -1
  end
end