Class: CSV2API::CLI
Overview
A Simple class for the executable version of the gem
Constant Summary collapse
- BANNER =
Displays example use when running the command-line application
"Usage: csv2api [OPTION] [ARGS]\nDescription:\nCSV2API, Auto-creates API endpoints from csv files\n\nOptions:\n"
Instance Attribute Summary collapse
-
#options ⇒ String
Options for api settings.
Instance Method Summary collapse
-
#default_options(opts) ⇒ Object
Configures the arguments for the command.
-
#directory ⇒ File
Returns actual directory when no argument is passed.
-
#directory? ⇒ TrueClass, FalseClass
Checks if argument passed is a directory.
-
#file? ⇒ TrueClass, FalseClass
Checks if argument passed is a file.
-
#initialize(args) ⇒ CLI
constructor
Passes arguments from ARGV.
-
#load_path ⇒ Object
Configures load path for CSV sources.
-
#load_server ⇒ Object
Loads API Server.
-
#parse ⇒ Object
Parses options sent from command-line.
-
#read_source ⇒ File
Returns a file or a directory.
-
#server_loading_info ⇒ Object
Returns text when server is loading.
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
23 24 25 26 |
# File 'lib/csv2api/cli.rb', line 23 def initialize(args) @args = args = Options.new end |
Instance Attribute Details
#options ⇒ String
Returns options for api settings.
19 20 21 |
# File 'lib/csv2api/cli.rb', line 19 def end |
Instance Method Details
#default_options(opts) ⇒ Object
Configures the arguments for the command
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 (opts) opts.version, opts. = CSV2API::VERSION, BANNER opts.set_program_name 'CSV2API' opts.on_head('-d', '--directory [DIRECTORY]', String, 'load csv files from directory (default)') do |dir| .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 ⇒ File
Returns actual directory when no argument is passed
53 54 55 |
# File 'lib/csv2api/cli.rb', line 53 def directory .directory ? .directory : '.' end |
#directory? ⇒ TrueClass, FalseClass
Checks if argument passed is a directory
59 60 61 |
# File 'lib/csv2api/cli.rb', line 59 def directory? FileTest.directory?(.directory) end |
#file? ⇒ TrueClass, FalseClass
Checks if argument passed is a file
65 66 67 |
# File 'lib/csv2api/cli.rb', line 65 def file? FileTest.file?(.file) unless .file.nil? end |
#load_path ⇒ Object
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_server ⇒ Object
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 |
#parse ⇒ Object
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_source ⇒ File
Returns a file or a directory
71 72 73 |
# File 'lib/csv2api/cli.rb', line 71 def read_source file? ? File.read(.file) : Dir.entries(directory) end |
#server_loading_info ⇒ Object
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 |