Completed
Push — gem ( 68d4ba...3cb05e )
by Joska
01:13
created

Server.out_config()   A

Complexity

Conditions 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 2
dl 0
loc 7
rs 9.4286
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
              end
51
        puts msg
52
      end
53
54
      # Output some config
55
      #
56
      # @param what [Symbol] What to show the config for.
57
      # @param cnf [Array<String>] The array for the config.
58
      def self.out_config(what, cnf)
59
        if cnf.has_key? what.to_s
60
          puts "#{what.to_s} set to #{cnf[what.to_s]}"
61
        else
62
          puts "Unknown setting."
63
        end
64
      end
65
66
      # Shows the help
67
      #
68
      # @param what [Symbol] What to show the help for.
69
      def self.help(what)
70
        case what
71
        when :list
72
          puts "midb v1.1.1 has several commands that you can use. For detailed information, see `midb help command`."
73
          puts " "
74
          puts "bootstrap\tCreate the basic files and directories that midb needs to be ran in a folder."
75
          puts "set\tModify this project's settings. See the detailed help for a list of options."
76
          puts "serve\tServes a JSON file - creates an API endpoint."
77
          puts "unserve\tStops serving a JSON file - the endpoint is no longer valid."
78
          puts "start\tStarts an API server. See detailed help for more."
79
        when :bootstrap
80
          puts "This command creates the `.midb.yaml` config file, and the `db` and `json` directories if they don't exist."
81
          puts "You must bootstrap before running any other commands."
82
        when :set
83
          puts "Sets config options. If no value is given, it shows the current value."
84
          puts "db:host\tHost name of the database (for MySQL)"
85
          puts "db:user\tUsername for the database (for MySQL)"
86
          puts "db:password\tPassword for the database (for MySQL)"
87
          puts "db:engine\t(sqlite3, mysql) Changes the database engine."
88
          puts "api:key\tChanges the private API key, used for authentication over HTTP."
89
        when :serve
90
          puts "This command will create an API endpoint pointing to a JSON file in the json/ directory."
91
          puts "It will support GET, POST, PUT and DELETE requests."
92
          puts "For detailed information on how to format your file, see the GitHub README and/or wiki."
93
        when :unserve
94
          puts "Stops serving a JSON file under the json/ directory."
95
        when :start
96
          puts "Starts the server. You must run the serve/unserve commands beforehand, so to set some endpoints."
97
          puts "Options:"
98
          puts "db:DATABASE\tSets DATABASE as the database where to get the data. Mandatory."
99
          puts "port:PORT\tSets PORT as the port where the server will listen to. Default: 8081."
100
        end
101
      end
102
    end
103
  end
104
end
105