midb 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f6a12da2fe9657a3beacfe2a00aed774bede4101
4
- data.tar.gz: 7af6d67d1a748311d665af0b1330d7ac4ba69dcb
3
+ metadata.gz: 8a61365a18572ed93ed1115447612d6f1f1463f5
4
+ data.tar.gz: dbc56dd3cb04e326c2a12e23d887b28f9d389584
5
5
  SHA512:
6
- metadata.gz: 7eaee7eccb98dbf4001039eea0f2d11a012b305bff7fb822597137b631689cfbae555188f1f5384e2a035067ad836a85920864086c65e1ec03b7f98f35cb3dea
7
- data.tar.gz: 1deda596c389b6804abe4cd55202165f4a3897a6169899e5aa4e2f3f3af4cf7101f66a5a474ed5c91bceb8f0b42c09a8163be42de5ca5935d1168a51b4997cd7
6
+ metadata.gz: 88389b80d4ad740ea1bbf541dd2ee5fc54faf8e99de8266dc26d5350ae0bf1ca5a27bd4776eaef062e73c1040403ba63947899ebb368753f259c8d374b284514
7
+ data.tar.gz: abc6f7ae590fc3013402739769f6a5f9252cab0c63483c93897931a825abda899d993fd75e01037babe1082cd619ca47c518d06bb82c7bed0102883f8ef4043f
@@ -3,8 +3,25 @@ require 'sqlite3'
3
3
  require 'mysql2'
4
4
 
5
5
  module MIDB
6
+ # @author unrar
7
+ # This class handles engine-dependent database operations
6
8
  class DbengineModel
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
7
22
  attr_accessor :engine, :host, :uname, :pwd, :port, :db
23
+
24
+ # Constructor - initializes the attributes with the configuration from ServerController
8
25
  def initialize()
9
26
  @engine = MIDB::ServerController.config["dbengine"]
10
27
  @host = MIDB::ServerController.config["dbhost"]
@@ -13,20 +30,28 @@ module MIDB
13
30
  @pwd = MIDB::ServerController.config["dbpassword"]
14
31
  @db = MIDB::ServerController.db
15
32
  end
16
- # Method: connect
33
+
17
34
  # Connect to the specified database
35
+ #
36
+ # @return [SQLite3::Database, Mysql2::Client] A resource referencing to the database
18
37
  def connect()
38
+ # Connect to an SQLite3 database
19
39
  if @engine == :sqlite3
20
40
  sq = SQLite3::Database.open("./db/#{@db}.db")
21
41
  sq.results_as_hash = true
22
42
  return sq
43
+ # Connect to a MySQL database
23
44
  elsif @engine == :mysql
24
45
  return Mysql2::Client.new(:host => @host, :username => @uname, :password => @pwd, :database => @db)
25
46
  end
26
47
  end
27
48
 
28
- # Method: query
29
- # Perform a query, return a hash
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
30
55
  def query(res, query)
31
56
  if @engine == :sqlite3
32
57
  return res.execute(query)
@@ -35,8 +60,12 @@ module MIDB
35
60
  end
36
61
  end
37
62
 
38
- # Method: extract
39
- # Extract a field from a query
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
40
69
  def extract(result, field)
41
70
  if @engine == :sqlite3
42
71
  return result[0][field] || result[field]
@@ -46,5 +75,18 @@ module MIDB
46
75
  end
47
76
  end
48
77
  end
78
+
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
89
+ end
90
+ end
49
91
  end
50
92
  end
@@ -1,11 +1,12 @@
1
1
  require 'midb/server_controller'
2
2
 
3
- # This controller handles errors.
4
3
  module MIDB
4
+ # A view that outputs errors.
5
5
  class ErrorsView
6
- # Method: die
7
- # Handles arguments that cause program termination.
8
- # Errors: :noargs, :server_already_started
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.
9
10
  def self.die(err)
10
11
  errmsg = case err
11
12
  when :noargs then "No command supplied. See `midb help`."
@@ -25,7 +26,6 @@ module MIDB
25
26
  else "Unknown error: #{err.to_s}"
26
27
  end
27
28
  abort("Fatal error: #{errmsg}")
28
-
29
29
  end
30
30
  end
31
31
  end
@@ -2,29 +2,40 @@ require 'hmac-sha1'
2
2
  require 'base64'
3
3
  require 'cgi'
4
4
 
5
- # midb security controller - handles API authentication
6
- # this will probably become another different project soon!
7
5
  module MIDB
6
+ # Controller that handles API HMAC authentication.
7
+ #
8
+ # @note This will probably become a separate project soon.
8
9
  class SecurityController
9
10
 
10
- # Method: is_auth?
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.
12
16
  def self.is_auth?(header)
13
17
  return header.split(":")[0].downcase == "authentication"
14
18
  end
15
19
 
16
- # Method: parse_auth
17
- # Parses an authentication header
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.
18
25
  def self.parse_auth(header)
19
26
  return header.split(" ")[1]
20
27
  end
21
28
 
22
- # Method: check?
23
- # Checks if an HMAC digest is properly authenticated
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.
24
36
  def self.check?(header, params, key)
25
- signature = params
26
37
  hmac = HMAC::SHA1.new(key)
27
- hmac.update(signature)
38
+ hmac.update(params)
28
39
  return self.parse_auth(header) == CGI.escape(Base64.encode64("#{hmac.digest}"))
29
40
  end
30
41
  end
@@ -12,23 +12,33 @@ require 'sqlite3'
12
12
  module MIDB
13
13
  # This controller controls the behavior of the midb server.
14
14
  class ServerController
15
- # Variable declaration
15
+
16
+ # Attribute declaration here
16
17
  class << self
17
- # args[] => passed by the binary
18
- # config => configuration array saved and loaded from .midb.yaml
19
- # db => the database we're using
20
- # http_status => the HTTP status, sent by the model
18
+ # @!attribute args
19
+ # @return [Array<String>] Arguments passed to the binary.
20
+ # @!attribute config
21
+ # @return [Hash] Contains the project's configuration, saved in .midb.yaml
22
+ # @!attribute db
23
+ # @return [String] Database name (if SQLite is the engine, file name without extension)
24
+ # @!attribute http_status
25
+ # @return [String] HTTP status code and string representation for the header
26
+ # @!attribute port
27
+ # @return [Fixnum] Port where the server will listen.
21
28
  attr_accessor :args, :config, :db, :http_status, :port
22
29
  end
23
- # status => server status
24
- # serves[] => JSON files served by the API
30
+ # Default values
31
+ #
32
+ # @see #http_status
33
+ # @see #args
34
+ # @see #config
35
+ # @see #port
25
36
  @http_status = "200 OK"
26
37
  @args = []
27
38
  @config = Hash.new()
28
39
  @port = 8081
29
40
 
30
- # Method: init
31
- # Decide what to do according to the supplied command!
41
+ # Decide the server's behavior depending on the arguments.
32
42
  def self.init()
33
43
  # We should have at least one argument, which can be `run` or `serve`
34
44
  MIDB::ErrorsView.die(:noargs) if @args.length < 1
@@ -244,7 +244,7 @@ module MIDB
244
244
  else
245
245
  query = dbe.query(dblink, "SELECT #{field} from #{table} WHERE id=#{row['id']};")
246
246
  end
247
- jso[row["id"]][name] = query.length > 0 ? dbe.extract(query,field) : "unknown"
247
+ jso[row["id"]][name] = dbe.length(query) > 0 ? dbe.extract(query,field) : "unknown"
248
248
  end
249
249
  end
250
250
  MIDB::ServerController.http_status = "200 OK"
@@ -284,7 +284,7 @@ module MIDB
284
284
  else
285
285
  query = dbe.query(dblink, "SELECT #{field} from #{table} WHERE id=#{row['id']};")
286
286
  end
287
- jso[row["id"]][name] = query.length > 0 ? dbe.extract(query,field) : "unknown"
287
+ jso[row["id"]][name] = dbe.length(query) > 0 ? dbe.extract(query,field) : "unknown"
288
288
  end
289
289
  end
290
290
  return jso
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: midb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - unrar
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-31 00:00:00.000000000 Z
11
+ date: 2015-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mysql2