Class: MIDB::Interface::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/midb/server_view.rb

Overview

View that handles everything from the server

Class Method Summary collapse

Class Method Details

.help(what) ⇒ Object

Shows the help

Parameters:

  • what (Symbol)

    What to show the help for.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/midb/server_view.rb', line 74

def self.help(what)
  case what
  when :list
    puts "midb v1.1.1 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

Send some info

Parameters:

  • what (Symbol)

    What to show the information for.

  • info (Array<String>) (defaults to: nil)

    Extra information needed for the message.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/midb/server_view.rb', line 37

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

Return a JSON error response

Parameters:

  • errno (Fixnum)

    Error number.

  • msg (String)

    Error message.



14
15
16
# File 'lib/midb/server_view.rb', line 14

def self.json_error(errno, msg)
  return {"error" => {"errno" => errno, "msg" => msg}}
end

.out_config(what, cnf) ⇒ Object

Output some config

Parameters:

  • what (Symbol)

    What to show the config for.

  • cnf (Array<String>)

    The array for the config.



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/midb/server_view.rb', line 58

def self.out_config(what, cnf)
  msg = case what
        when :dbengine then "Database engine: #{cnf['dbengine']}."
        when :dbhost then "Database server host: #{cnf['dbhost']}."
        when :dbport then "Database server port: #{cnf['dbport']}."
        when :dbuser then "Database server user: #{cnf['dbuser']}."
        when :dbpassword then "Database server password: #{cnf['dbpassword']}."
        when :apikey then "Private API key: #{cnf['apikey']}"
        else "Error??"
        end
  puts msg
end

.server_stoppedObject

Notice that the server has been stopped.



29
30
31
# File 'lib/midb/server_view.rb', line 29

def self.server_stopped()
  puts "The server has been successfully stopped!"
end

.show_serving(cnf) ⇒ Object

Shows the files being served

Parameters:

  • cnf (Array<String>)

    The configuration from the server.



21
22
23
24
25
26
# File 'lib/midb/server_view.rb', line 21

def self.show_serving(cnf)
  puts "The follow JSON files are being served as APIs:"
  cnf["serves"].each do |serv|
    puts "- #{serv}"
  end
end

.successObject



6
7
8
# File 'lib/midb/server_view.rb', line 6

def self.success()
  puts "Success!"
end