Class: SongkickQueue::CLI

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI

Parameters:

  • argv (Array<String>)

    of command line arguments


9
10
11
12
13
14
15
16
17
# File 'lib/songkick_queue/cli.rb', line 9

def initialize(argv)
  @options = OpenStruct.new(
    libraries: [],
    consumers: [],
    process_name: 'songkick_queue',
  )

  parse_options(argv)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options


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

def options
  @options
end

Instance Method Details

#parse_options(argv) ⇒ Object

Parse the command line arguments using OptionParser

Parameters:

  • argv (Array<String>)

    of command line arguments


22
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
# File 'lib/songkick_queue/cli.rb', line 22

def parse_options(argv)
  option_parser = OptionParser.new do |opts|
    opts.banner = 'Usage: songkick_consumer [options]'

    opts.on('-r', '--require LIBRARY',
            'Path to require LIBRARY. Usually this will be a file that ',
            'requires some consumers') do |lib|
      options.libraries << lib
    end

    opts.on('-c', '--consumer CLASS_NAME',
            'Register consumer with CLASS_NAME') do |class_name|
      options.consumers << class_name
    end

    opts.on('-n', '--name NAME',
            'Set the process name to NAME') do |name|
      options.process_name = name
    end

    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end
  end

  option_parser.parse!(argv)
end

#runObject

Instantiates and runs a new Worker for the parsed options. Calling this method blocks the main Thread. See Worker#run for more info


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/songkick_queue/cli.rb', line 54

def run
  options.libraries.each do |lib|
    require lib
  end

  if options.consumers.empty?
    puts 'No consumers provided, exiting. Run `songkick_queue --help` for more info.'
    exit 1
  end

  consumers = options.consumers.map do |class_name|
    Object.const_get(class_name)
  end

  Worker.new(options.process_name, consumers).run
end