Class: Speedly::CLI

Inherits:
Object
  • Object
show all
Includes:
Phantomjs
Defined in:
lib/speedly/cli.rb

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/speedly/cli.rb', line 10

def initialize
  opts = Slop.parse({ help: true, strict: true }) do
    banner 'Usage: speedly [--url[=<url>,=<url>]] [--advanced] ' \
           '[--version]'

    on :u=, :url=, 'Your URL(s)', as: Array, delimiter: ',', argument: true
    on :a, :advanced, 'Use advanced mode.'
    on :v, :version, 'Display the version.' do
      puts "Speedly version #{Speedly::VERSION}"
    end
  end

  if opts.url?
    opts[:url].each_with_index do |url, i|
      run(url, opts.advanced? ? 'advanced' : 'basic')
      puts "\n" unless i + 1 == opts[:url].length
    end
  elsif !opts.version? && !opts.help?
    puts opts.help
  end
end

Instance Method Details

#run(url, mode) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/speedly/cli.rb', line 32

def run(url, mode)
  begin
    json = mode == 'basic' ? Speedly.basic(url) : Speedly.advanced(url)
    format_output(json, mode)
  rescue Speedly::PhantomjsLoadError
    puts "[#{'FAILED'.red.reset}]      " \
         'PhantomJS not found. Please install PhantomJS.'
    exit
  rescue URI::InvalidURIError
    puts "[#{'FAILED'.red.reset}]      " \
         "'#{url.to_s.bold.reset}' is not a valid URL.\n"
  end
end