Server.help()   C
last analyzed

Complexity

Conditions 7

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 7
dl 0
loc 33
rs 5.5
1
module MIDB
2
  module Interface
3
    # View that handles everything from the server
4
    class Server
5
6
      def self.success()
7
        puts "Success!"
8
      end
9
10
      # Return a JSON error response
11
      #
12
      # @param errno [Fixnum] Error number.
13
      # @param msg [String] Error message.
14
      def self.json_error(errno, msg)
15
        return {"error" => {"errno" => errno, "msg" => msg}}
16
      end
17
18
      # Shows the files being served
19
      #
20
      # @param cnf [Array<String>] The configuration from the server.
21
      def self.show_serving(cnf)
22
        puts "The follow JSON files are being served as APIs:"
23
        cnf["serves"].each do |serv|
24
          puts "- #{serv}"
25
        end
26
      end
27
28
      # Notice that the server has been stopped.
29
      def self.server_stopped()
30
        puts "The server has been successfully stopped!"
31
      end
32
33
      # Send some info
34
      # 
35
      # @param what [Symbol] What to show the information for.
36
      # @param info [Array<String>] Extra information needed for the message.
37
      def self.info(what, info=nil)
38
        msg = case what
39
              when :start then "Server started on port #{info}. Listening for connections..."
40
              when :incoming_request then "> Incoming request from #{info}."
41
              when :request then ">> Request method: #{info[0]}\n>>> Endpoint: #{info[1]}"
42
              when :match_json then ">> The request matched a JSON file: #{info}.json\n>> Creating response..."
43
              when :response then ">> Sending JSON response (RAW):\n#{info}"
44
              when :success then "> Successfully managed this request!"
45
              when :not_found then "> Invalid endpoint - sending a 404 error."
46
              when :auth_required then ">> Authentication required. Checking for the HTTP header..."
47
              when :no_auth then ">> No authentication header - sending a 401 error."
48
              when :auth_success then ">> Successfully authenticated the request."
49
              when :bootstrap then "> Successfully bootstraped!"
50
              when :fetch then ">> Fetching response... [#{info}]"
51
              end
52
        puts msg
53
      end
54
55
      # Output some config
56
      #
57
      # @param what [Symbol] What to show the config for.
58
      # @param cnf [Array<String>] The array for the config.
59
      def self.out_config(what, cnf)
60
        if cnf.has_key? what.to_s
61
          puts "#{what.to_s} set to #{cnf[what.to_s]}"
62
        else
63
          puts "Unknown setting."
64
        end
65
      end
66
67
      # Shows the help
68
      #
69
      # @param what [Symbol] What to show the help for.
70
      def self.help(what)
71
        case what
72
        when :list
73
          puts "midb v1.1.1 has several commands that you can use. For detailed information, see `midb help command`."
74
          puts " "
75
          puts "bootstrap\tCreate the basic files and directories that midb needs to be ran in a folder."
76
          puts "set\tModify this project's settings. See the detailed help for a list of options."
77
          puts "serve\tServes a JSON file - creates an API endpoint."
78
          puts "unserve\tStops serving a JSON file - the endpoint is no longer valid."
79
          puts "start\tStarts an API server. See detailed help for more."
80
        when :bootstrap
81
          puts "This command creates the `.midb.yaml` config file, and the `db` and `json` directories if they don't exist."
82
          puts "You must bootstrap before running any other commands."
83
        when :set
84
          puts "Sets config options. If no value is given, it shows the current value."
85
          puts "db:host\tHost name of the database (for MySQL)"
86
          puts "db:user\tUsername for the database (for MySQL)"
87
          puts "db:password\tPassword for the database (for MySQL)"
88
          puts "db:engine\t(sqlite3, mysql) Changes the database engine."
89
          puts "api:key\tChanges the private API key, used for authentication over HTTP."
90
        when :serve
91
          puts "This command will create an API endpoint pointing to a JSON file in the json/ directory."
92
          puts "It will support GET, POST, PUT and DELETE requests."
93
          puts "For detailed information on how to format your file, see the GitHub README and/or wiki."
94
        when :unserve
95
          puts "Stops serving a JSON file under the json/ directory."
96
        when :start
97
          puts "Starts the server. You must run the serve/unserve commands beforehand, so to set some endpoints."
98
          puts "Options:"
99
          puts "db:DATABASE\tSets DATABASE as the database where to get the data. Mandatory."
100
          puts "port:PORT\tSets PORT as the port where the server will listen to. Default: 8081."
101
        end
102
      end
103
    end
104
  end
105
end
106