class Evostream::Runner

Execute CLI with this gem

Attributes

options[R]

Public Class Methods

new() click to toggle source
# File lib/evostream/cli/runner.rb, line 29
def initialize
  CLI::Config.instance
  @options = CLI::Options.new
end

Public Instance Methods

run(args = ARGV) click to toggle source

rubocop:disable Metrics/MethodLength rubocop:disable Naming/RescuedExceptionsVariableName

# File lib/evostream/cli/runner.rb, line 36
def run(args = ARGV)
  @options.parse

  access_evostream?
  execute_runner(two_last_arg(args)) if args.count >= 1
rescue CodeError::Evostream::ConnectionFailed
  201
rescue CodeError::Evostream::NoResult
  200
rescue CodeError::Syntax::CommandInvalid
  101
rescue CodeError::Syntax::OptionInvalid
  100
rescue Evostream::Commands::Errors::MissingMandatory => error
  $stdout.puts error.message.red
  50
rescue CodeError::Finished
  0
end

Private Instance Methods

access_evostream?() click to toggle source

rubocop:enable Metrics/MethodLength rubocop:enable Naming/RescuedExceptionsVariableName

# File lib/evostream/cli/runner.rb, line 60
def access_evostream?
  Timeout.timeout(1) do
    test_server_started
  end
rescue Timeout::Error
  raise CodeError::Evostream::ConnectionFailed
end
cmd_exist?(cmd) click to toggle source
# File lib/evostream/cli/runner.rb, line 102
def cmd_exist?(cmd)
  Evostream::Commands::Command.descendants.none? do |command|
    command.to_s.split('::').last.casecmp(cmd).zero?
  end
end
create_an_hash(arguments) click to toggle source
# File lib/evostream/cli/runner.rb, line 108
def create_an_hash(arguments)
  super_hash = {}
  arguments.split(': ').each_with_index do |value, index|
    if (index % 2).zero?
      super_hash[value] = nil
    else
      super_hash[super_hash.keys[index - 1]] = value
    end
  end
  super_hash
end
execute_runner(cmd) click to toggle source
# File lib/evostream/cli/runner.rb, line 76
def execute_runner(cmd)
  test_command(cmd) do
    payload = cmd.last == cmd.first ? {} : create_an_hash(cmd.last)
    act = Evostream::Action.new(payload)
    interpret_response(act.execute_action(cmd.first)[:data])
  end
end
interpret_response(result) click to toggle source
# File lib/evostream/cli/runner.rb, line 84
def interpret_response(result)
  if CLI::Argument::Search.instance.search.nil?
    $stdout.puts result.to_yaml
  else
    CLI::Search.new.search_node(result)
  end
  raise CodeError::Finished
end
test_command(cmd) { || ... } click to toggle source
# File lib/evostream/cli/runner.rb, line 93
def test_command(cmd)
  cmd.each_with_index do |one_arg, index|
    raise CodeError::Syntax::CommandInvalid \
      if one_arg.start_with?('-', '--') ||
         (cmd_exist?(one_arg) if index.zero?)
  end
  yield
end
test_server_started() click to toggle source
# File lib/evostream/cli/runner.rb, line 68
def test_server_started
  uri = URI.parse(Evostream::Service.uri_in.to_s)
  socket = TCPSocket.new(uri.host, uri.port)
  socket.close
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
  raise CodeError::Evostream::ConnectionFailed
end
two_last_arg(args) click to toggle source
# File lib/evostream/cli/runner.rb, line 120
def two_last_arg(args)
  if CLI::Argument::Search.instance.search.nil?
    [args.first, args.last]
  else
    [args.last, args.last]
  end
end