class CSV2API::CLI

@author Jonah Ruiz <jonah@pixelhipsters.com> A Simple class for the executable version of the gem

Constants

Displays example use when running the command-line application

Attributes

options[RW]

@return [String] options for api settings

Public Class Methods

new(args) click to toggle source

Passes arguments from ARGV @param args [Array<String>] The command-line arguments

# File lib/csv2api/cli.rb, line 23
def initialize(args)
  @args = args
  @options = Options.new
end

Public Instance Methods

default_options(opts) click to toggle source

Configures the arguments for the command @param opts [OptionParser]

# File lib/csv2api/cli.rb, line 30
def default_options(opts)
  opts.version, opts.banner = CSV2API::VERSION, BANNER
  opts.set_program_name 'CSV2API'

  opts.on_head('-d', '--directory [DIRECTORY]', String,
               'load csv files from directory (default)') do |dir|
    options.directory = dir
  end

  opts.on_tail('-v', '--version',
               'display the version of CSV2API and exit') do
    puts opts.ver
    exit
  end

  opts.on_tail('-h', '--help', 'print this help') do
    puts opts.help
    exit
  end
end
directory() click to toggle source

Returns actual directory when no argument is passed @return [File] directory path

# File lib/csv2api/cli.rb, line 53
def directory
  options.directory ? options.directory : '.'
end
directory?() click to toggle source

Checks if argument passed is a directory @return [TrueClass, FalseClass]

# File lib/csv2api/cli.rb, line 59
def directory?
  FileTest.directory?(options.directory)
end
file?() click to toggle source

Checks if argument passed is a file @return [TrueClass, FalseClass]

# File lib/csv2api/cli.rb, line 65
def file?
  FileTest.file?(options.file) unless options.file.nil?
end
load_path() click to toggle source

Configures load path for CSV sources

# File lib/csv2api/cli.rb, line 76
def load_path
  ENV['CSV2API_ROOT'] = directory
end
load_server() click to toggle source

Loads API Server

# File lib/csv2api/cli.rb, line 94
def load_server
  load_path
  server_loading_info
  CSV2API::Server.run!
end
parse() click to toggle source

Parses options sent from command-line

# File lib/csv2api/cli.rb, line 101
def parse
  opts = OptionParser.new(&method(:default_options))
  opts.parse!(@args)
  load_server
end
read_source() click to toggle source

Returns a file or a directory @return [File] source containing csv data

# File lib/csv2api/cli.rb, line 71
def read_source
  file? ? File.read(options.file) : Dir.entries(directory)
end
server_loading_info() click to toggle source

Returns text when server is loading

# File lib/csv2api/cli.rb, line 81
def server_loading_info
  files = file_names(directory)
  text = files.map do |csv|
    "#{csv}.csv - http://localhost:4567/#{csv}"
  end

  text << "\n"
  text.unshift("#{files.count} files detected. Creating API endpoints...")
  text.unshift('Starting CSV2API...')
  puts text
end