class MDQT::CLI::Base

Public Class Methods

absorb_piped_args(args) click to toggle source
# File lib/mdqt/cli/base.rb, line 50
def self.absorb_piped_args(args)
  piped = piped? ? parse_stdin : []
  args + piped
end
check_requirements(args, options) click to toggle source
# File lib/mdqt/cli/base.rb, line 20
def self.check_requirements(args, options)
  abort "Error: No MDQ service URL has been specified." unless options.service
  if options.save_to
    dir = options.save_to
    begin
      FileUtils.mkdir_p(dir) unless File.exist?(dir)
    rescue
      abort "Error: Directory #{dir} did not exist, and we can't create it"
    end
    abort "Error: '#{dir}' is not a writable directory!" if (File.directory?(dir) && ! File.writable?(dir))
    abort "Error: '#{dir}' is not a directory!" unless File.directory?(dir)
  end

end
introduce(args, options) click to toggle source
# File lib/mdqt/cli/base.rb, line 35
def self.introduce(args, options)
  if options.verbose
    STDERR.puts "MDQT version #{MDQT::VERSION}"
    STDERR.puts "Using #{options.service}" unless options.service == :not_required
    STDERR.puts "Caching is #{options.cache ? 'on' : 'off'}"
    STDERR.print "XML validation is #{MDQT::Client.verification_available? ? 'available' : 'not available'}"
    STDERR.puts  " #{options.validate ? "and active" : "but inactive"} for this request" if MDQT::Client.verification_available?
    STDERR.print "Signature verification is #{MDQT::Client.verification_available? ? 'available' : 'not available'}"
    STDERR.puts  " #{options.verify_with ? "and active" : "but inactive"} for this request" if MDQT::Client.verification_available?
    STDERR.puts "Output directory for saved files is: #{File.absolute_path(options.save_to)}" if options.save_to
    STDERR.puts("Warning! TLS certificate verification has been disabled!") if options.tls_risky
    STDERR.puts
  end
end
new(args, options) click to toggle source
# File lib/mdqt/cli/base.rb, line 63
def initialize(args, options)
  @args = args
  @options = options
end
parse_stdin() click to toggle source
# File lib/mdqt/cli/base.rb, line 59
def self.parse_stdin
  STDIN.gsub("\s", "\n").each_line.collect {|l| l.strip}
end
piped?() click to toggle source
# File lib/mdqt/cli/base.rb, line 55
def self.piped?
  !STDIN.tty? && !STDIN.closed?
end
run(args, options) click to toggle source
# File lib/mdqt/cli/base.rb, line 10
def self.run(args, options)

  #args = options.stdin ? absorb_piped_args(args) : args

  check_requirements(args, options)
  introduce(args, options)

  self.new(args, options).run
end

Public Instance Methods

advise_on_xml_signing_support() click to toggle source
# File lib/mdqt/cli/base.rb, line 110
def advise_on_xml_signing_support
  hey "XML signature verification and XML validation are not available. Install the 'xmldsig' gem if you can." unless MDQT::Client.verification_available?
end
args() click to toggle source
# File lib/mdqt/cli/base.rb, line 72
def args
  @args
end
args=(new_args) click to toggle source
# File lib/mdqt/cli/base.rb, line 68
def args=(new_args)
  @args = new_args
end
btw(comment) click to toggle source
# File lib/mdqt/cli/base.rb, line 138
def btw(comment)
  STDERR.puts(comment) if options.verbose
end
colour_shell?() click to toggle source
# File lib/mdqt/cli/base.rb, line 126
def colour_shell?
  TTY::Color.color?
end
explain(response) click to toggle source
# File lib/mdqt/cli/base.rb, line 96
def explain(response)
  unless response.explanation.empty?
    require 'terminal-table'
    misc_rows = [['URL', response.explanation[:url]], ["Method", response.explanation[:method]], ['Status', response.explanation[:status]]]
    req_header_rows  = response.explanation[:request_headers].map { |k, v| ['C', k, v] }
    resp_header_rows = response.explanation[:response_headers].map { |k, v| ['S', k, v] }

    btw Terminal::Table.new :title => "HTTP Misc", :rows => misc_rows
    btw Terminal::Table.new :title => "Client Request Headers", :headings => ['C/S', 'Header', 'Value'], :rows => req_header_rows
    btw Terminal::Table.new :title => "Server Response Headers", :headings => ['C/S', 'Header', 'Value'], :rows => resp_header_rows

  end
end
extract_certificate_paths(cert_paths = options.verify_with) click to toggle source
# File lib/mdqt/cli/base.rb, line 114
def extract_certificate_paths(cert_paths = options.verify_with)
  cert_paths.collect do |cert_path|
    begin
      halt! "Cannot read certificate at '#{cert_path}'!" unless File.readable?(cert_path)
      halt! "File at '#{cert_path}' does not seem to be a PEM format certificate" unless IO.binread(cert_path).include?("-----BEGIN CERTIFICATE-----")
      cert_path
    rescue
      halt! "Unable to validate the certificate at '#{cert_path}'"
    end
  end
end
halt!(comment) click to toggle source
# File lib/mdqt/cli/base.rb, line 146
def halt!(comment)
  abort pastel.red("Error: #{comment}")
end
hey(comment) click to toggle source
# File lib/mdqt/cli/base.rb, line 134
def hey(comment)
  STDERR.puts(comment)
end
options() click to toggle source
# File lib/mdqt/cli/base.rb, line 80
def options
  @options
end
options=(new_options) click to toggle source
# File lib/mdqt/cli/base.rb, line 76
def options=(new_options)
  @options = new_options
end
output(response) click to toggle source
# File lib/mdqt/cli/base.rb, line 84
def output(response)
  if response.ok?
    yay response.message
    hey explain(response) if options.explain
    trailer = response.data[-1] == "\n" ? "" : "\n"
    response.data + trailer
  else
    hey response.message
  end

end
pastel() click to toggle source
# File lib/mdqt/cli/base.rb, line 130
def pastel
  @pastel ||= Pastel.new
end
run() click to toggle source
# File lib/mdqt/cli/base.rb, line 149
def run
  halt! "No action has been defined for this command!"
end
yay(comment) click to toggle source
# File lib/mdqt/cli/base.rb, line 142
def yay(comment)
  btw pastel.green(comment)
end