Class: Resty::Cli

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

Instance Method Summary collapse

Instance Method Details

#basic_auth_invalid?(options) ⇒ Boolean



37
38
39
40
41
# File 'lib/resty/cli.rb', line 37

def basic_auth_invalid?(options)
  username = options[:username]
  password = options[:password]
  (username && password.nil?) || (username.nil? && password)
end

#missing_host_or_alias?(options) ⇒ Boolean



33
34
35
# File 'lib/resty/cli.rb', line 33

def missing_host_or_alias?(options)
  (options[:alias] && options[:host]) || (options[:alias].nil? && options[:host].nil?)
end

#runObject



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
31
# File 'lib/resty/cli.rb', line 6

def run
  options = Trollop::options do
    version "Version: #{Resty::VERSION}\n"
    opt :alias, "The per-host entry to use in ~/.ruby_resty.yml", type: :string, short: "-a"
    opt :headers, "The headers sent with each request. Ex: X-NYAN-CAT-SECRET-KEY:nyan_nyan",
      type: :strings, short: "-H"
    opt :host, "The hostname of the REST service. Ex: http://nyan.cat", type: :string, short: "-h"
    opt :list_aliases, "List aliases in ~/.ruby_resty.yml", short: "-l"
    opt :username, "HTTP basic authentication username", type: :string, short: "-u"
    opt :password, "HTTP basic authentication password", type: :string, short: "-p"
    opt :verbose, "Verbose mode", short: "-v"
  end

  if options[:list_aliases]
    system("cat ~/.ruby_resty.yml")
  elsif missing_host_or_alias?(options)
    puts "Please specify an alias OR a host. Use --help for more info."
  elsif basic_auth_invalid?(options)
    puts "Please specify a username and password. Use --help for more info."
  else
    Resty::Repl.start(options)
  end

rescue ConfigFileError => e
  puts e.message
end