Class: CSV2API::CLI

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/csv2api/cli.rb

Overview

A Simple class for the executable version of the gem

Constant Summary collapse

"Usage: csv2api [OPTION] [ARGS]\nDescription:\nCSV2API, Auto-creates API endpoints from csv files\n\nOptions:\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

csv_files, file_names, generate_json, included, load_csv, sanitize_column_headers

Constructor Details

#initialize(args) ⇒ CLI

Passes arguments from ARGV

Parameters:

  • args (Array<String>)

    The command-line arguments



23
24
25
26
# File 'lib/csv2api/cli.rb', line 23

def initialize(args)
  @args = args
  @options = Options.new
end

Instance Attribute Details

#optionsString

Returns options for api settings.

Returns:

  • (String)

    options for api settings



19
20
21
# File 'lib/csv2api/cli.rb', line 19

def options
  @options
end

Instance Method Details

#default_options(opts) ⇒ Object

Configures the arguments for the command

Parameters:

  • opts (OptionParser)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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

#directoryFile

Returns actual directory when no argument is passed

Returns:

  • (File)

    directory path



53
54
55
# File 'lib/csv2api/cli.rb', line 53

def directory
  options.directory ? options.directory : '.'
end

#directory?TrueClass, FalseClass

Checks if argument passed is a directory

Returns:

  • (TrueClass, FalseClass)


59
60
61
# File 'lib/csv2api/cli.rb', line 59

def directory?
  FileTest.directory?(options.directory)
end

#file?TrueClass, FalseClass

Checks if argument passed is a file

Returns:

  • (TrueClass, FalseClass)


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

def file?
  FileTest.file?(options.file) unless options.file.nil?
end

#load_pathObject

Configures load path for CSV sources



76
77
78
# File 'lib/csv2api/cli.rb', line 76

def load_path
  ENV['CSV2API_ROOT'] = directory
end

#load_serverObject

Loads API Server



94
95
96
97
98
# File 'lib/csv2api/cli.rb', line 94

def load_server
  load_path
  server_loading_info
  CSV2API::Server.run!
end

#parseObject

Parses options sent from command-line



101
102
103
104
105
# File 'lib/csv2api/cli.rb', line 101

def parse
  opts = OptionParser.new(&method(:default_options))
  opts.parse!(@args)
  load_server
end

#read_sourceFile

Returns a file or a directory

Returns:

  • (File)

    source containing csv data



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

def read_source
  file? ? File.read(options.file) : Dir.entries(directory)
end

#server_loading_infoObject

Returns text when server is loading



81
82
83
84
85
86
87
88
89
90
91
# 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