midb 1.1.0 → 1.1.1

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: c1800c9b95f115f1c96c363a245c6ba28fbc0541
4
- data.tar.gz: 77177bd1d17209dd9cd304ae8a1259851cc93291
3
+ metadata.gz: d5ef9483dfada39d3e9f8bccc1c44e1e418a49b1
4
+ data.tar.gz: 954831b1336e3d3402279f2d5b1cd49f99850d50
5
5
  SHA512:
6
- metadata.gz: 3faa266d28b6965fe4b97e3b4e74cbdcb8b9f538ea76c5381e7d4a28ca614cd0f1704ec20038d9d4272e8a512f2315902bf466991c4edf9c38c42ae6e43c395b
7
- data.tar.gz: a418745028a7f5770d66ae4a18ce7a327d5160aa39a1542f0cb5c5072f9413372c4c96d3351c61e3a0243563d7196b835ae240b0299b6e5548900989301b9aa7
6
+ metadata.gz: 50e11179acc6827bb9fc7ece5f5ecaf4b7938dac3838d82601d2c496a62fd5f6f4fa00fbab474c927a1c925e7aec4ede32603542256627c806e020c7d8010bbb
7
+ data.tar.gz: e148bf8571b0f54652a79945f92df92ebde4ab67e3898acc4541f1c34342b93fb6fa9d20f8d006e3eb9f44a4f0a9145293e1706e227f993dcceed68d1d50aee5
@@ -3,10 +3,43 @@
3
3
  # The "hooks" part of the MIDB::API module allows programmers to customize their API.
4
4
  # Hooks are methods that are run in the API; overriding them allows you to run a custom API.
5
5
  #
6
+ # This hooking technology has been developed in its-hookable.
7
+ #
6
8
  module MIDB
7
9
  module API
8
10
  class Hooks
9
- def self.after_get_all_entries()
11
+ attr_accessor :hooks
12
+ def initialize()
13
+ @hooks = Hash.new
14
+ @hooks["after_get_all_entries"] = []
15
+ @hooks["format_field"] = []
16
+ end
17
+
18
+ # This method adds a method _reference_ (:whatever) to the hash defined above.
19
+ def register(hook, method)
20
+ @hooks[hook].push method
21
+ end
22
+
23
+ # These are the methods that are ran when the hook is called from the main class.
24
+ # The code can be slightly modified depending on the arguments that each hook takes,
25
+ # but that's up to the original developer - not the one who does the customization.
26
+
27
+
28
+ def after_get_all_entries(n_entries)
29
+ @hooks["after_get_all_entries"].each do |f|
30
+ # Just run :f whenever this method is called, no arguments.
31
+ Object.send(f, n_entries)
32
+ end
33
+ end
34
+
35
+ def format_field(field, what)
36
+ if @hooks["format_field"] == []
37
+ return what
38
+ else
39
+ @hooks["format_field"].each do |f|
40
+ return Object.send(f, field, what)
41
+ end
42
+ end
10
43
  end
11
44
  end
12
45
  end
@@ -3,6 +3,7 @@ require 'midb/server_view'
3
3
  require 'midb/errors_view'
4
4
  require 'midb/security_controller'
5
5
  require 'midb/serverengine_controller'
6
+ require 'midb/hooks'
6
7
 
7
8
  require 'yaml'
8
9
  require 'socket'
@@ -26,12 +27,14 @@ module MIDB
26
27
  # @return [String] HTTP status code and string representation for the header
27
28
  # @!attribute port
28
29
  # @return [Fixnum] Port where the server will listen.
29
- attr_accessor :args, :config, :db, :http_status, :port
30
+ # @!attribute h
31
+ # @return [Object] MIDB::API::Hooks instance
32
+ attr_accessor :args, :config, :db, :http_status, :port, :hooks
30
33
 
31
34
  # Constructor for this controller.
32
35
  #
33
36
  # @param args [Array<String>] Arguments passed to the binary.
34
- def initialize(args)
37
+ def initialize(args, hooks=nil)
35
38
  # Default values
36
39
  #
37
40
  # @see #http_status
@@ -42,6 +45,12 @@ module MIDB
42
45
  @args = args
43
46
  @config = Hash.new()
44
47
  @port = 8081
48
+ if hooks == nil
49
+ # At some point someone should register this.
50
+ @hooks = MIDB::API::Hooks.new
51
+ else
52
+ @hooks = hooks
53
+ end
45
54
  end
46
55
 
47
56
  #####################
@@ -172,8 +181,8 @@ module MIDB
172
181
  end
173
182
  end
174
183
 
175
- # Call the server engine
176
- eng = MIDB::API::Engine.new(@db, @http_status, @config)
184
+ # Call the server engine and supply the (non)existing hooks
185
+ eng = MIDB::API::Engine.new(@db, @http_status, @config, @hooks)
177
186
  if eng.start(@port)
178
187
  @config["status"] = :running
179
188
  MIDB::Interface::Server.success()
@@ -10,17 +10,20 @@ module MIDB
10
10
  module API
11
11
  class Model
12
12
 
13
- attr_accessor :jsf, :db, :engine
13
+ attr_accessor :jsf, :db, :engine, :hooks
14
14
 
15
15
  # Constructor
16
16
  #
17
17
  # @param jsf [String] JSON file with the schema
18
18
  # @param db [String] Database to operate on.
19
19
  # @param engine [Object] Reference to the API engine.
20
+ #
21
+ # @notice that @hooks (the hooks) are taken from the engine.
20
22
  def initialize(jsf, db, engine)
21
23
  @jsf = jsf
22
24
  @db = db
23
25
  @engine = engine
26
+ @hooks = engine.hooks
24
27
  end
25
28
 
26
29
  # Safely get the structure
@@ -261,6 +264,7 @@ module MIDB
261
264
  query = dbe.query(dblink, "SELECT #{field} from #{table} WHERE id=#{row['id']};")
262
265
  end
263
266
  jso[row["id"]][name] = dbe.length(query) > 0 ? dbe.extract(query,field) : "unknown"
267
+ jso[row["id"]][name] = @hooks.format_field(name, jso[row["id"]][name])
264
268
  end
265
269
  end
266
270
  @engine.http_status = "200 OK"
@@ -299,9 +303,10 @@ module MIDB
299
303
  query = dbe.query(dblink, "SELECT #{field} from #{table} WHERE id=#{row['id']};")
300
304
  end
301
305
  jso[row["id"]][name] = dbe.length(query) > 0 ? dbe.extract(query,field) : "unknown"
306
+ jso[row["id"]][name] = @hooks.format_field(name, jso[row["id"]][name])
302
307
  end
303
308
  end
304
- MIDB::API::Hooks.after_get_all_entries()
309
+ @hooks.after_get_all_entries(rows.length)
305
310
  return jso
306
311
  end
307
312
  end
@@ -74,7 +74,7 @@ module MIDB
74
74
  def self.help(what)
75
75
  case what
76
76
  when :list
77
- puts "midb has several commands that you can use. For detailed information, see `midb help command`."
77
+ puts "midb v1.1.1 has several commands that you can use. For detailed information, see `midb help command`."
78
78
  puts " "
79
79
  puts "bootstrap\tCreate the basic files and directories that midb needs to be ran in a folder."
80
80
  puts "set\tModify this project's settings. See the detailed help for a list of options."
@@ -3,6 +3,7 @@ require 'midb/server_model'
3
3
  require 'midb/server_view'
4
4
  require 'midb/errors_view'
5
5
  require 'midb/security_controller'
6
+ require 'midb/hooks'
6
7
 
7
8
  require 'yaml'
8
9
  require 'socket'
@@ -22,17 +23,25 @@ module MIDB
22
23
  # @return [String] Database name (if SQLite is the engine, file name without extension)
23
24
  # @!attribute http_status
24
25
  # @return [String] HTTP status code and string representation for the header
25
- attr_accessor :config, :db, :http_status
26
+ # @!attribute h
27
+ # @return [Object] MIDB::API::Hooks instance
28
+ attr_accessor :config, :db, :http_status, :hooks
29
+
26
30
 
27
31
  # Constructor
28
32
  #
29
33
  # @param db [String] Database to which the server will bind.
30
34
  # @param stat [Fixnum] HTTP Status
31
35
  # @param cnf [Hash] Config from the server controller.
32
- def initialize(db, stat, cnf)
36
+ def initialize(db, stat, cnf, hooks=nil)
33
37
  @config = cnf
34
38
  @db = db
35
39
  @http_status = stat
40
+ if hooks == nil
41
+ @hooks = MIDB::API::Hooks.new
42
+ else
43
+ @hooks = hooks
44
+ end
36
45
  end
37
46
 
38
47
  # Starts the server on a given port (default: 8081)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: midb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - unrar