Class: AutoREST::CLI
- Inherits:
-
Thor
- Object
- Thor
- AutoREST::CLI
- Defined in:
- lib/autorest.rb
Class Method Summary collapse
-
.exit_on_failure? ⇒ Boolean
Determines if the program should exit on failure.
Instance Method Summary collapse
-
#boot(dsn) ⇒ Object
Starts the AutoREST API server using a DSN (Data Source Name) Parses the DSN and starts the server using the corresponding database.
-
#new ⇒ Object
Creates a new AutoREST API server project Prompts the user for database details and creates a configuration file.
-
#server(file) ⇒ Object
Starts the AutoREST API server using a configuration file Loads the configuration file and starts the server with the specified database settings.
-
#version ⇒ Object
Prints the version of AutoREST.
Class Method Details
.exit_on_failure? ⇒ Boolean
Determines if the program should exit on failure
29 30 31 |
# File 'lib/autorest.rb', line 29 def self.exit_on_failure? true end |
Instance Method Details
#boot(dsn) ⇒ Object
Starts the AutoREST API server using a DSN (Data Source Name) Parses the DSN and starts the server using the corresponding database.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/autorest.rb', line 118 def boot(dsn) uri = URI.parse(dsn) if uri.scheme == "sqlite" require_relative "autorest/db/sqlite" db = AutoREST::SQLiteDB.new(uri.host) table, *_ = uri.path.sub(/^\//, '').split('/') else database, table = uri.path.sub(/^\//, '').split('/') passwd = URI.decode_www_form_component(uri.password) case uri.scheme when "mysql" require_relative "autorest/db/mysql" db = AutoREST::MySQLDB.new(uri.host, uri.port, uri.user, passwd, database) when "pg" require_relative "autorest/db/postgres" db = AutoREST::PostgresDB.new(uri.host, uri.port, uri.user, passwd, database) when "orcl" require_relative "autorest/db/oracle" db = AutoREST::OracleDB.new(uri.host, uri.port, uri.user, passwd, database) end end db.prepare db.set_access_tables([table]) start_server(db) end |
#new ⇒ Object
Creates a new AutoREST API server project Prompts the user for database details and creates a configuration file.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/autorest.rb', line 44 def new prompt = TTY::Prompt.new opts = {db: {}, server: { host: "localhost", port: 7914 } } puts "Welcome to AutoREST API Server Generator" project_name = prompt.ask("Enter the project's name:") opts[:db][:kind] = prompt.select("Select your database:", {"SQLite" => :sqlite, "MySQL" => :mysql, "PostgreSQL" => :pg, "Oracle" => :orcl}, default: "SQLite") if opts[:db][:kind] == :sqlite require_relative "autorest/db/sqlite" opts[:db][:name] = prompt.ask("Enter location of DB file:") db = AutoREST::SQLiteDB.new(opts[:db][:name]) else def_port = {mysql: 3306, pg: 5432, orcl: 1521} def_usr = {mysql: "root", pg: "postgres", orcl: "SYS"} opts[:db][:host] = prompt.ask("Enter hostname of DB:", default: "localhost") opts[:db][:port] = prompt.ask("Enter port of DB:", default: def_port[opts[:db][:kind]]) opts[:db][:user] = prompt.ask("Enter username:", default: def_usr[opts[:db][:kind]]) opts[:db][:passwd] = prompt.ask("Enter password:", echo: false) opts[:db][:name] = prompt.ask("Enter database #{opts[:db][:kind] == :orcl ? "SID" : "name"}:") case opts[:db][:kind] when :mysql require_relative "autorest/db/mysql" db = AutoREST::MySQLDB.new(opts[:db][:host], opts[:db][:port], opts[:db][:user], opts[:db][:passwd], opts[:db][:name]) when :pg require_relative "autorest/db/postgres" db = AutoREST::PostgresDB.new(opts[:db][:host], opts[:db][:port], opts[:db][:user], opts[:db][:passwd], opts[:db][:name]) when :orcl require_relative "autorest/db/oracle" db = AutoREST::OracleDB.new(opts[:db][:host], opts[:db][:port], opts[:db][:user], opts[:db][:passwd], opts[:db][:name]) end end opts[:db][:tables] = prompt.multi_select("Select tables from database:", db.tables) db.set_access_tables(opts[:db][:tables]) puts "Creating configuration file..." File.open("#{project_name}.yml", "w") do |f| f.write(opts.to_yaml) end puts "Successfully completed!" start_server(db) end |
#server(file) ⇒ Object
Starts the AutoREST API server using a configuration file Loads the configuration file and starts the server with the specified database settings.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/autorest.rb', line 92 def server(file) opts = YAML.load_file(file) case opts[:db][:kind] when :sqlite require_relative "autorest/db/sqlite" db = AutoREST::SQLiteDB.new(opts[:db][:name]) when :mysql require_relative "autorest/db/mysql" db = AutoREST::MySQLDB.new(opts[:db][:host], opts[:db][:port], opts[:db][:user], opts[:db][:passwd], opts[:db][:name]) when :pg require_relative "autorest/db/postgres" db = AutoREST::PostgresDB.new(opts[:db][:host], opts[:db][:port], opts[:db][:user], opts[:db][:passwd], opts[:db][:name]) when :orcl require_relative "autorest/db/oracle" db = AutoREST::OracleDB.new(opts[:db][:host], opts[:db][:port], opts[:db][:user], opts[:db][:passwd], opts[:db][:name]) end db.prepare db.set_access_tables(opts[:db][:tables]) servinfo = opts.fetch(:server, { host: "localhost", port: 7914}) start_server(db, servinfo[:host], servinfo[:port]) end |