Class: Gemometer::CLI

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

Constant Summary collapse

NOTIFIERS =
Gemometer.notifiers
MANDATORY =
%w[notifier url]

Class Method Summary collapse

Class Method Details

.notifyObject



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/gemometer/cli.rb', line 63

def self.notify
  begin
    gems = Gemometer::Parser.new(Gemometer::System.bundle_outdated).parse

    Gemometer::Notifiers.const_get(@options.notifier.capitalize).new(
      gems: gems,
      url: @options.url
    ).notify
  rescue Gemometer::NotifyError => e
    abort(e.message)
  end
end

.parse_args(args) ⇒ Object



16
17
18
19
20
21
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
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/gemometer/cli.rb', line 16

def self.parse_args(args)
  options = OpenStruct.new

  opt_parser = OptionParser.new do |opts|
    opts.banner = 'Usage: gemometer [options]'

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

    opts.on('-n', '--notifier NOTIFIER', NOTIFIERS, {},
            'Specify the notifier app', "  (#{NOTIFIERS.join(', ')})") do |notifier|
      options.notifier = notifier
    end

    opts.on('-u', '--url URL',
            'Specify the app notification url') do |url|
      options.url = url
    end

    opts.separator ''
    opts.separator 'Common options:'

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

    opts.on_tail('--version', 'Show version') do
      puts Gemometer::VERSION
      exit
    end
  end

  begin
    opt_parser.parse!(args)
    missing = MANDATORY.select{ |param| options.send(param).nil? }
    if missing.any?
      abort("\nMissing options: #{missing.join(', ')}\n\n\n#{opt_parser}")
    end
  rescue OptionParser::InvalidArgument, OptionParser::MissingArgument, OptionParser::InvalidOption
    # Friendly output when parsing fails
     abort("\n#{$!}\n\n\n#{opt_parser}")
  end

  options
end

.start(args) ⇒ Object



11
12
13
14
# File 'lib/gemometer/cli.rb', line 11

def self.start(args)
  @options = parse_args(args)
  notify
end