midb 1.0.5 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aee36621935f7db48f10ae5e199e1e04fa6af949
4
- data.tar.gz: 125adfc8de0773ea3dc2c87bafa110ca84f011b2
3
+ metadata.gz: c1800c9b95f115f1c96c363a245c6ba28fbc0541
4
+ data.tar.gz: 77177bd1d17209dd9cd304ae8a1259851cc93291
5
5
  SHA512:
6
- metadata.gz: cdc131cdc0c230b93d3f323fd712e4e9eb53fee916ec1385ad4caa9bd810e37b7039907ac6776e64923084908832006fe667854aef9fd1568e09824aca2b9ef1
7
- data.tar.gz: 496d34906597bca28076bdc7cdb67f425d0fe498dccb403f1a1c57a59c1aeaa43361eee11913922432f76e2f1619ebb27ec6f85dcc0272891d3ea4976eda8649
6
+ metadata.gz: 3faa266d28b6965fe4b97e3b4e74cbdcb8b9f538ea76c5381e7d4a28ca614cd0f1704ec20038d9d4272e8a512f2315902bf466991c4edf9c38c42ae6e43c395b
7
+ data.tar.gz: a418745028a7f5770d66ae4a18ce7a327d5160aa39a1542f0cb5c5072f9413372c4c96d3351c61e3a0243563d7196b835ae240b0299b6e5548900989301b9aa7
data/bin/midb CHANGED
@@ -4,11 +4,14 @@
4
4
  # 08/31/15, unrar
5
5
  require 'midb'
6
6
 
7
+ api = MIDB::API::Controller.new(ARGV)
8
+ api.init()
9
+ api.save()
7
10
  # Pass the arguments to the controler, we don't want the action here ;-)
8
- MIDB::ServerController.args = ARGV
11
+ #MIDB::ServerController.args = ARGV
9
12
 
10
13
  # And start the server
11
- MIDB::ServerController.init()
14
+ #MIDB::ServerController.init()
12
15
 
13
16
  # Save data in case we didn't actually start the server but change the configuration
14
- MIDB::ServerController.save()
17
+ #!!MIDB::ServerController.save()
@@ -1,91 +1,95 @@
1
- require 'midb/server_controller'
2
1
  require 'sqlite3'
3
2
  require 'mysql2'
4
3
 
5
4
  module MIDB
6
- # @author unrar
7
- # This class handles engine-dependent database operations
8
- class DbengineModel
5
+ module API
6
+ # @author unrar
7
+ # This class handles engine-dependent database operations
8
+ class Dbengine
9
9
 
10
- # @!attribute engine
11
- # @return [Symbol] The database engine being used
12
- # @!attribute host
13
- # @return [String] Host name where the database is located
14
- # @!attribute uname
15
- # @return [String] Username for the database
16
- # @!attribute pwd
17
- # @return [String] Password for the database.
18
- # @!attribute port
19
- # @return [Fixnum] Port for the database
20
- # @!attribute db
21
- # @return [String] Name of the database
22
- attr_accessor :engine, :host, :uname, :pwd, :port, :db
10
+ # @!attribute engine
11
+ # @return [Symbol] The database engine being used
12
+ # @!attribute host
13
+ # @return [String] Host name where the database is located
14
+ # @!attribute uname
15
+ # @return [String] Username for the database
16
+ # @!attribute pwd
17
+ # @return [String] Password for the database.
18
+ # @!attribute port
19
+ # @return [Fixnum] Port for the database
20
+ # @!attribute db
21
+ # @return [String] Name of the database
22
+ attr_accessor :engine, :host, :uname, :pwd, :port, :db
23
23
 
24
- # Constructor - initializes the attributes with the configuration from ServerController
25
- def initialize()
26
- @engine = MIDB::ServerController.config["dbengine"]
27
- @host = MIDB::ServerController.config["dbhost"]
28
- @port = MIDB::ServerController.config["dbport"]
29
- @uname = MIDB::ServerController.config["dbuser"]
30
- @pwd = MIDB::ServerController.config["dbpassword"]
31
- @db = MIDB::ServerController.db
32
- end
24
+ # Constructor - initializes the attributes with the configuration from ServerController
25
+ #
26
+ # @param cnf [Array<String>] The configuration array.
27
+ # @param db [String] Database name.
28
+ def initialize(cnf, db)
29
+ @engine = cnf["dbengine"]
30
+ @host = cnf["dbhost"]
31
+ @port = cnf["dbport"]
32
+ @uname = cnf["dbuser"]
33
+ @pwd = cnf["dbpassword"]
34
+ @db = db
35
+ end
33
36
 
34
- # Connect to the specified database
35
- #
36
- # @return [SQLite3::Database, Mysql2::Client] A resource referencing to the database
37
- def connect()
38
- # Connect to an SQLite3 database
39
- if @engine == :sqlite3
40
- sq = SQLite3::Database.open("./db/#{@db}.db")
41
- sq.results_as_hash = true
42
- return sq
43
- # Connect to a MySQL database
44
- elsif @engine == :mysql
45
- return Mysql2::Client.new(:host => @host, :username => @uname, :password => @pwd, :database => @db)
37
+ # Connect to the specified database
38
+ #
39
+ # @return [SQLite3::Database, Mysql2::Client] A resource referencing to the database
40
+ def connect()
41
+ # Connect to an SQLite3 database
42
+ if @engine == :sqlite3
43
+ sq = SQLite3::Database.open("./db/#{@db}.db")
44
+ sq.results_as_hash = true
45
+ return sq
46
+ # Connect to a MySQL database
47
+ elsif @engine == :mysql
48
+ return Mysql2::Client.new(:host => @host, :username => @uname, :password => @pwd, :database => @db)
49
+ end
46
50
  end
47
- end
48
51
 
49
- # Perform a query to the database.
50
- #
51
- # @param res [SQLite3::Database, Mysql2::Client] An existing database resource.
52
- # @param query [String] The SQL query to be ran.
53
- #
54
- # @return [Array, Hash] Returns an array of hashes for SQLite3 or a hash for MySQL
55
- def query(res, query)
56
- if @engine == :sqlite3
57
- return res.execute(query)
58
- elsif @engine == :mysql
59
- return res.query(query)
52
+ # Perform a query to the database.
53
+ #
54
+ # @param res [SQLite3::Database, Mysql2::Client] An existing database resource.
55
+ # @param query [String] The SQL query to be ran.
56
+ #
57
+ # @return [Array, Hash] Returns an array of hashes for SQLite3 or a hash for MySQL
58
+ def query(res, query)
59
+ if @engine == :sqlite3
60
+ return res.execute(query)
61
+ elsif @engine == :mysql
62
+ return res.query(query)
63
+ end
60
64
  end
61
- end
62
65
 
63
- # Extract a field from a query, because different engines return different types (see #query)
64
- #
65
- # @param result [Array, Hash] The result of a query obtained via #query
66
- # @param field [String] The name of the field to be extracted.
67
- #
68
- # @return [String, Fixnum] The field extracted from a query
69
- def extract(result, field)
70
- if @engine == :sqlite3
71
- return result[0][field] || result[field]
72
- elsif @engine == :mysql
73
- result.each do |row|
74
- return row[field]
66
+ # Extract a field from a query, because different engines return different types (see #query)
67
+ #
68
+ # @param result [Array, Hash] The result of a query obtained via #query
69
+ # @param field [String] The name of the field to be extracted.
70
+ #
71
+ # @return [String, Fixnum] The field extracted from a query
72
+ def extract(result, field)
73
+ if @engine == :sqlite3
74
+ return result[0][field] || result[field]
75
+ elsif @engine == :mysql
76
+ result.each do |row|
77
+ return row[field]
78
+ end
75
79
  end
76
80
  end
77
- end
78
81
 
79
- # Returns the length of a result, because different engines return diferent types (see #query)
80
- #
81
- # @param result [Array, Hash] The result of a query obtained via #query
82
- #
83
- # @return [Fixnum] Length of the result.
84
- def length(result)
85
- if @engine == :sqlite3
86
- return result.length
87
- elsif @engine == :mysql
88
- return result.count
82
+ # Returns the length of a result, because different engines return diferent types (see #query)
83
+ #
84
+ # @param result [Array, Hash] The result of a query obtained via #query
85
+ #
86
+ # @return [Fixnum] Length of the result.
87
+ def length(result)
88
+ if @engine == :sqlite3
89
+ return result.length
90
+ elsif @engine == :mysql
91
+ return result.count
92
+ end
89
93
  end
90
94
  end
91
95
  end
@@ -1,31 +1,30 @@
1
- require 'midb/server_controller'
2
-
3
1
  module MIDB
4
- # A view that outputs errors.
5
- class ErrorsView
6
-
7
- # Handles fatal errors that will cause the application to abrort.
8
- #
9
- # @param err [Symbol] The ID of the error that's to be reported.
10
- def self.die(err)
11
- errmsg = case err
12
- when :noargs then "No command supplied. See `midb help`."
13
- when :server_already_started then "The server has already been started and is running."
14
- when :server_not_running then "The server isn't running."
15
- when :server_error then "Error while starting server."
16
- when :no_serves then "No files are being served. Try running `midb serve file.json`"
17
- when :syntax then "Syntax error. See `midb help`"
18
- when :file_404 then "File not found."
19
- when :not_json then "Specified file isn't JSON!"
20
- when :json_exists then "Specified file is already being served."
21
- when :json_not_exists then "The JSON file isn't being served."
22
- when :unsupported_engine then "The specified database engine isn't supported by midb."
23
- when :already_project then "This directory already contains a midb project."
24
- when :bootstrap then "midb hasn't been bootstraped in this folder. Run `midb bootstrap`."
25
- when :no_help then "No help available for this command. See a list of commands with `midb help`."
26
- else "Unknown error: #{err.to_s}"
27
- end
28
- abort("Fatal error: #{errmsg}")
2
+ module Interface
3
+ # A view that outputs errors.
4
+ class Errors
5
+ # Handles fatal errors that will cause the application to abrort.
6
+ #
7
+ # @param err [Symbol] The ID of the error that's to be reported.
8
+ def self.die(err)
9
+ errmsg = case err
10
+ when :noargs then "No command supplied. See `midb help`."
11
+ when :server_already_started then "The server has already been started and is running."
12
+ when :server_not_running then "The server isn't running."
13
+ when :server_error then "Error while starting server."
14
+ when :no_serves then "No files are being served. Try running `midb serve file.json`"
15
+ when :syntax then "Syntax error. See `midb help`"
16
+ when :file_404 then "File not found."
17
+ when :not_json then "Specified file isn't JSON!"
18
+ when :json_exists then "Specified file is already being served."
19
+ when :json_not_exists then "The JSON file isn't being served."
20
+ when :unsupported_engine then "The specified database engine isn't supported by midb."
21
+ when :already_project then "This directory already contains a midb project."
22
+ when :bootstrap then "midb hasn't been bootstraped in this folder. Run `midb bootstrap`."
23
+ when :no_help then "No help available for this command. See a list of commands with `midb help`."
24
+ else "Unknown error: #{err.to_s}"
25
+ end
26
+ abort("Fatal error: #{errmsg}")
27
+ end
29
28
  end
30
29
  end
31
30
  end
data/lib/midb/hooks.rb ADDED
@@ -0,0 +1,13 @@
1
+ # ADDED in midb-2.0.0a by unrar #
2
+ #
3
+ # The "hooks" part of the MIDB::API module allows programmers to customize their API.
4
+ # Hooks are methods that are run in the API; overriding them allows you to run a custom API.
5
+ #
6
+ module MIDB
7
+ module API
8
+ class Hooks
9
+ def self.after_get_all_entries()
10
+ end
11
+ end
12
+ end
13
+ end
@@ -3,40 +3,42 @@ require 'base64'
3
3
  require 'cgi'
4
4
 
5
5
  module MIDB
6
- # Controller that handles API HMAC authentication.
7
- #
8
- # @note This will probably become a separate project soon.
9
- class SecurityController
6
+ module API
7
+ # Controller that handles API HMAC authentication.
8
+ #
9
+ # @note This will probably become a separate project soon.
10
+ class Security
10
11
 
11
- # Checks if an HTTP header is the authorization one
12
- #
13
- # @deprecated It's no longer used but kept for historical reasons.
14
- # @param header [String] A line of an HTTP header.
15
- # @return [Boolean] Whether it's an auth header or not.
16
- def self.is_auth?(header)
17
- return header.split(":")[0].downcase == "authentication"
18
- end
12
+ # Checks if an HTTP header is the authorization one
13
+ #
14
+ # @deprecated It's no longer used but kept for historical reasons.
15
+ # @param header [String] A line of an HTTP header.
16
+ # @return [Boolean] Whether it's an auth header or not.
17
+ def self.is_auth?(header)
18
+ return header.split(":")[0].downcase == "authentication"
19
+ end
19
20
 
20
- # Parses an authentication header so to get the HMAC digest.
21
- #
22
- # @param header [String] A line of an HTTP header (should have been checked
23
- # to be an auth header)
24
- # @return [String] The HMAC digest as a string.
25
- def self.parse_auth(header)
26
- return header.split(" ")[1]
27
- end
21
+ # Parses an authentication header so to get the HMAC digest.
22
+ #
23
+ # @param header [String] A line of an HTTP header (should have been checked
24
+ # to be an auth header)
25
+ # @return [String] The HMAC digest as a string.
26
+ def self.parse_auth(header)
27
+ return header.split(" ")[1]
28
+ end
28
29
 
29
- # Checks if an HMAC digest is properly authenticated.
30
- #
31
- # @param header [String] A line of an HTTP header (see #parse_auth)
32
- # @param params [String] The data passed via the HTTP request.
33
- # @param key [String] The private API key.
34
- #
35
- # @return [Boolean] Whether the given digest matches the correct one or not.
36
- def self.check?(header, params, key)
37
- hmac = HMAC::SHA1.new(key)
38
- hmac.update(params)
39
- return self.parse_auth(header) == CGI.escape(Base64.encode64("#{hmac.digest}"))
30
+ # Checks if an HMAC digest is properly authenticated.
31
+ #
32
+ # @param header [String] A line of an HTTP header (see #parse_auth)
33
+ # @param params [String] The data passed via the HTTP request.
34
+ # @param key [String] The private API key.
35
+ #
36
+ # @return [Boolean] Whether the given digest matches the correct one or not.
37
+ def self.check?(header, params, key)
38
+ hmac = HMAC::SHA1.new(key)
39
+ hmac.update(params)
40
+ return self.parse_auth(header) == CGI.escape(Base64.encode64("#{hmac.digest}"))
41
+ end
40
42
  end
41
43
  end
42
44
  end