class NightcrawlerSwift::CLI::Runner

Attributes

argv[RW]
opt_parser[R]
options[R]

Public Class Methods

new(argv) click to toggle source
# File lib/nightcrawler_swift/cli/runner.rb, line 6
def initialize argv
  @argv = argv
  configure_logger
end

Public Instance Methods

log(string) click to toggle source
# File lib/nightcrawler_swift/cli/runner.rb, line 20
def log string
  NightcrawlerSwift.logger.info string
end
run() click to toggle source
# File lib/nightcrawler_swift/cli/runner.rb, line 11
def run
  configure_default_options
  configure_opt_parser
  parse_parameters
  @command_name = argv.shift
  validate_command_and_options
  execute_command if @command_name
end

Private Instance Methods

check_rcfile() click to toggle source
# File lib/nightcrawler_swift/cli/runner.rb, line 125
def check_rcfile
  sample = NightcrawlerSwift::CLI.sample_rcfile
  unless File.exist?(options.config_file)
    File.open(options.config_file, "w") { |f|
      f.write(sample)
    }
  end

  if sample == File.read(options.config_file)
    options.configured = false
  end
end
command_name_normalized() click to toggle source
# File lib/nightcrawler_swift/cli/runner.rb, line 76
def command_name_normalized
  @command_name.downcase.gsub(/-/, "_")
end
config_hash() click to toggle source
# File lib/nightcrawler_swift/cli/runner.rb, line 58
def config_hash
  @config_hash ||= JSON.parse(File.read(options.config_file), symbolize_names: true) rescue {}
end
configure_default_options() click to toggle source
# File lib/nightcrawler_swift/cli/runner.rb, line 30
def configure_default_options
  user_home_dir = Dir.home
  @options = OpenStruct.new
  @options.configured = true
  @options.default_config_file = true
  @options.config_file = File.expand_path(File.join(user_home_dir, CONFIG_FILE))
  @options.use_cache = true
  @options.cache_file = File.expand_path(File.join(user_home_dir, CACHE_FILE))
  @options.config_hash = {}
end
configure_logger() click to toggle source
# File lib/nightcrawler_swift/cli/runner.rb, line 25
def configure_logger
  STDOUT.sync = true
  NightcrawlerSwift.logger.formatter = lambda {|severity, datetime, progname, msg| "#{msg}\n"}
end
configure_opt_parser() click to toggle source
# File lib/nightcrawler_swift/cli/runner.rb, line 121
def configure_opt_parser
  @opt_parser = OptParser.new self
end
connect_and_execute(&block) click to toggle source
# File lib/nightcrawler_swift/cli/runner.rb, line 80
def connect_and_execute &block
  path = options.cache_file
  if File.exist?(path)
    @options.use_cache ? restore_connection(path) : File.delete(path)
  end

  begin
    block.call
  ensure
    return unless @options.use_cache
    File.open(path, "w") do |f|
      f.write(NightcrawlerSwift.connection.auth_response.to_h.to_json)
    end
  end
end
execute_command() click to toggle source
# File lib/nightcrawler_swift/cli/runner.rb, line 62
def execute_command
  NightcrawlerSwift.configure config_hash.merge(@options.config_hash)

  connect_and_execute do
    command_method = "command_#{command_name_normalized}"
    command_class = COMMANDS[@command_name][:command]
    Formatters::Basic.new(self).send(command_method, command_class)
  end

rescue NightcrawlerSwift::Exceptions::BaseError, Errno::ENOENT => e
  log "Error: #{e.message}"
  exit 1
end
parse_parameters() click to toggle source
# File lib/nightcrawler_swift/cli/runner.rb, line 112
def parse_parameters
  @opt_parser.parse!
  check_rcfile if options.default_config_file

rescue OptionParser::InvalidOption => e
  log e.message
  exit 1
end
restore_connection(cache_path) click to toggle source
# File lib/nightcrawler_swift/cli/runner.rb, line 96
def restore_connection cache_path
  begin
    hash = JSON.parse File.read(cache_path)
    NightcrawlerSwift.connection.auth_response = OpenStruct.new(hash)
    NightcrawlerSwift.connection.configure

    token_id = NightcrawlerSwift.connection.token_id
    NightcrawlerSwift.logger.debug "Cache found, restablishing connection with token_id: #{token_id}"

  rescue
    log "Failed to restore connection, removing cache"
    File.delete cache_path
    NightcrawlerSwift.connection.auth_response = nil
  end
end
validate_command_and_options() click to toggle source
# File lib/nightcrawler_swift/cli/runner.rb, line 41
def validate_command_and_options
  if @command_name.nil? or argv.nil?
    log @opt_parser.help
    exit
  end

  unless COMMANDS.include?(@command_name)
    log "Error: Unknown command '#{@command_name}'"
    exit 1
  end

  unless options.configured
    log "You must configure your swift credentials, take a look at:\n   #{options.config_file}"
    exit 1
  end
end