Class: MIDB::ServerView
- Inherits:
-
Object
- Object
- MIDB::ServerView
- Defined in:
- lib/midb/server_view.rb
Class Method Summary collapse
-
.help(what) ⇒ Object
Method: help Shows the help.
-
.info(what, info = nil) ⇒ Object
Method: info Send some info.
-
.json_error(errno, msg) ⇒ Object
Method: json_error Return a JSON error response.
-
.out_config(what) ⇒ Object
Method: out_config Output some config.
-
.server_stopped ⇒ Object
Method: server_stopped Notice that the server has been stopped.
-
.show_serving ⇒ Object
Method: show_serving Shows the files being served.
- .success ⇒ Object
Class Method Details
.help(what) ⇒ Object
Method: help Shows the help
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/midb/server_view.rb', line 65 def self.help(what) case what when :list puts "midb has several commands that you can use. For detailed information, see `midb help command`." puts " " puts "bootstrap\tCreate the basic files and directories that midb needs to be ran in a folder." puts "set\tModify this project's settings. See the detailed help for a list of options." puts "serve\tServes a JSON file - creates an API endpoint." puts "unserve\tStops serving a JSON file - the endpoint is no longer valid." puts "start\tStarts an API server. See detailed help for more." when :bootstrap puts "This command creates the `.midb.yaml` config file, and the `db` and `json` directories if they don't exist." puts "You must bootstrap before running any other commands." when :set puts "Sets config options. If no value is given, it shows the current value." puts "db:host\tHost name of the database (for MySQL)" puts "db:user\tUsername for the database (for MySQL)" puts "db:password\tPassword for the database (for MySQL)" puts "db:engine\t(sqlite3, mysql) Changes the database engine." puts "api:key\tChanges the private API key, used for authentication over HTTP." when :serve puts "This command will create an API endpoint pointing to a JSON file in the json/ directory." puts "It will support GET, POST, PUT and DELETE requests." puts "For detailed information on how to format your file, see the GitHub README and/or wiki." when :unserve puts "Stops serving a JSON file under the json/ directory." when :start puts "Starts the server. You must run the serve/unserve commands beforehand, so to set some endpoints." puts "Options:" puts "db:DATABASE\tSets DATABASE as the database where to get the data. Mandatory." puts "port:PORT\tSets PORT as the port where the server will listen to. Default: 8081." end end |
.info(what, info = nil) ⇒ Object
Method: info Send some info
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/midb/server_view.rb', line 31 def self.info(what, info=nil) msg = case what when :start then "Server started on port #{info}. Listening for connections..." when :incoming_request then "> Incoming request from #{info}." when :request then ">> Request method: #{info[0]}\n>>> Endpoint: #{info[1]}" when :match_json then ">> The request matched a JSON file: #{info}.json\n>> Creating response..." when :response then ">> Sending JSON response (RAW):\n#{info}" when :success then "> Successfully managed this request!" when :not_found then "> Invalid endpoint - sending a 404 error." when :auth_required then ">> Authentication required. Checking for the HTTP header..." when :no_auth then ">> No authentication header - sending a 401 error." when :auth_success then ">> Successfully authenticated the request." when :bootstrap then "> Successfully bootstraped!" end puts msg end |
.json_error(errno, msg) ⇒ Object
Method: json_error Return a JSON error response
10 11 12 |
# File 'lib/midb/server_view.rb', line 10 def self.json_error(errno, msg) return {"error" => {"errno" => errno, "msg" => msg}} end |
.out_config(what) ⇒ Object
Method: out_config Output some config
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/midb/server_view.rb', line 50 def self.out_config(what) msg = case what when :dbengine then "Database engine: #{MIDB::ServerController.config['dbengine']}." when :dbhost then "Database server host: #{MIDB::ServerController.config['dbhost']}." when :dbport then "Database server port: #{MIDB::ServerController.config['dbport']}." when :dbuser then "Database server user: #{MIDB::ServerController.config['dbuser']}." when :dbpassword then "Database server password: #{MIDB::ServerController.config['dbpassword']}." when :apikey then "Private API key: #{MIDB::ServerController.config['apikey']}" else "Error??" end puts msg end |
.server_stopped ⇒ Object
Method: server_stopped Notice that the server has been stopped.
25 26 27 |
# File 'lib/midb/server_view.rb', line 25 def self.server_stopped() puts "The server has been successfully stopped!" end |
.show_serving ⇒ Object
Method: show_serving Shows the files being served
16 17 18 19 20 21 |
# File 'lib/midb/server_view.rb', line 16 def self.show_serving() puts "The follow JSON files are being served as APIs:" MIDB::ServerController.config["serves"].each do |serv| puts "- #{serv}" end end |
.success ⇒ Object
4 5 6 |
# File 'lib/midb/server_view.rb', line 4 def self.success() puts "Ayyy great" end |