Module: CLIHelpers

Includes:
MockServer
Included in:
MockServerCLI
Defined in:
lib/cli.rb

Overview

CLI for this gem

Author:

Constant Summary collapse

LOGGER =
LoggingFactory::DEFAULT_FACTORY.log('MockServerClient')

Constants included from MockServer

MockServer::CLEAR_ENDPOINT, MockServer::DUMP_LOG_ENDPOINT, MockServer::EXPECTATION_ENDPOINT, MockServer::HTTP_FORWARD, MockServer::HTTP_REQUEST, MockServer::HTTP_RESPONSE, MockServer::HTTP_TIMES, MockServer::RESET_ENDPOINT, MockServer::RETRIEVE_ENDPOINT, MockServer::VERSION

Instance Method Summary collapse

Instance Method Details

#error(message) ⇒ Object

Prints an error message

Parameters:

  • message (String)

    an error message



65
66
67
# File 'lib/cli.rb', line 65

def error(message)
  puts message.red
end

#execute_command(mockserver = false, data_required = false, error_msg = '--data option must be provided') {|a, a| ... } ⇒ Object

Process a block using options extracted into a struct

Parameters:

  • mockserver (Boolean) (defaults to: false)

    true to use mockserver, false to use proxy

Yield Parameters:

  • a (AbstractClient)

    mockserver or a proxy client

  • a (Struct)

    struct created from options hash



52
53
54
55
56
57
58
59
60
61
# File 'lib/cli.rb', line 52

def execute_command(mockserver = false, data_required = false, error_msg = '--data option must be provided', &_)
  print_parameters(options)
  struct_options = to_struct({ data: nil }.merge(options))
  if data_required && !options['data']
    error(error_msg)
  else
    client = mockserver ? mockserver_client(struct_options) : proxy_client(struct_options)
    yield client, struct_options if block_given?
  end
end

#mockserver_client(options) ⇒ MockServerClient

Create a mockserver client

Parameters:

  • options (Struct)

    with host and port set

Returns:



25
26
27
28
29
# File 'lib/cli.rb', line 25

def mockserver_client(options)
  client        = MockServerClient.new(options.host, options.port)
  client.logger = LOGGER
  client
end

Prints out the parameters passed to it

Parameters:

  • options (Hash)

    a hash of parameters



16
17
18
19
20
# File 'lib/cli.rb', line 16

def print_parameters(options)
  puts "\nRunning with parameters:".bold
  options.each { |k, v| puts "\t#{k}: #{v}".yellow }
  puts ''
end

#proxy_client(options) ⇒ ProxyClient

Create a proxy client

Parameters:

  • options (Struct)

    with host and port set

Returns:

  • (ProxyClient)

    the proxy client with the host and port



34
35
36
37
38
# File 'lib/cli.rb', line 34

def proxy_client(options)
  client        = ProxyClient.new(options.host, options.port)
  client.logger = LOGGER
  client
end

#read_file(file) ⇒ Object

Read a file

Parameters:

  • file (String)

    a file to read



71
72
73
# File 'lib/cli.rb', line 71

def read_file(file)
  YAML.load_file(file)
end

#to_struct(hash) ⇒ Struct

Convert a hash to a struct

Parameters:

  • hash (Hash)

    a hash

Returns:

  • (Struct)

    a struct constructed from the hash



43
44
45
46
# File 'lib/cli.rb', line 43

def to_struct(hash)
  hash = symbolize_keys(hash)
  Struct.new(*hash.keys).new(*hash.values)
end