class SolidusClient::CLI

The class responsible of handling command line logic

Public Instance Methods

run() click to toggle source

Entry point to start the application

# File lib/solidus_client/cli.rb, line 12
def run
  client = Client.new(parse_options)
  command = ARGV.shift.to_sym
  args = convert_data_input

  result = client.send(command, *args)
  pp result
rescue OptionParser::InvalidOption => e
  abort("#{e.message}, see 'solidus --help'")
end

Private Instance Methods

commands_descriptions() click to toggle source
# File lib/solidus_client/cli.rb, line 57
def commands_descriptions
  Client.public_instance_methods(false).sort.map do |method|
    params = Client.instance_method(method).parameters
    "#{method} #{params_description(params)}"
  end
end
convert_data_input() click to toggle source
# File lib/solidus_client/cli.rb, line 68
def convert_data_input
  ARGV.map do |arg|
    begin
      JSON.parse(arg, symbolize_names: true)
    rescue JSON::ParserError
      arg
    end
  end
end
opts_banner(opts) click to toggle source
# File lib/solidus_client/cli.rb, line 37
def opts_banner(opts)
  opts.banner = 'Usage: solidus [options] command [target] [JSON data]'
  opts.separator('')
  opts.separator("Commands:\n - #{commands_descriptions.join("\n - ")}")
  opts.separator('')
  opts.separator('Options:')
end
opts_key(opts, options) click to toggle source
# File lib/solidus_client/cli.rb, line 51
def opts_key(opts, options)
  opts.on('-k', '--key [STRING]', 'Solidus API key') do |value|
    options[:api_key] = value
  end
end
opts_url(opts, options) click to toggle source
# File lib/solidus_client/cli.rb, line 45
def opts_url(opts, options)
  opts.on('-u', '--url [STRING]', 'Solidus URL') do |value|
    options[:url] = value
  end
end
params_description(params) click to toggle source
# File lib/solidus_client/cli.rb, line 64
def params_description(params)
  params.map { |param| "<#{param[1]}>" }.join(' ')
end
parse_options() click to toggle source
# File lib/solidus_client/cli.rb, line 25
def parse_options
  options = {}
  OptionParser.new do |opts|
    opts.version = SolidusClient::Version::STRING
    opts_banner(opts)
    opts_url(opts, options)
    opts_key(opts, options)
  end.parse!

  options
end