Class: WAB::Impl::Shell
- Inherits:
-
Object
- Object
- WAB::Impl::Shell
- Extended by:
- Forwardable
- Includes:
- ShellLogger
- Defined in:
- lib/wab/impl/shell.rb
Overview
The shell for reference Ruby implementation.
Instance Attribute Summary collapse
-
#indent ⇒ Object
Returns the value of attribute indent.
-
#path_pos ⇒ Object
readonly
Returns the value of attribute path_pos.
-
#type_key ⇒ Object
readonly
Returns the path where a data type is located.
Attributes included from ShellLogger
Instance Method Summary collapse
-
#controller(data) ⇒ Object
Returns the controller associated with the type key found in the data.
-
#data(value = {}, repair = false) ⇒ Object
Create and return a new data instance with the provided initial value.
-
#initialize(config) ⇒ Shell
constructor
Sets up the shell with the supplied configuration data.
-
#path_controller(path) ⇒ Object
Returns the controller according to the type in the path.
-
#register_controller(type, controller) ⇒ Object
Register a controller for a named type.
-
#start ⇒ Object
Start listening.
Constructor Details
#initialize(config) ⇒ Shell
Sets up the shell with the supplied configuration data.
- config
-
Configuration object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/wab/impl/shell.rb', line 25 def initialize(config) @indent = config[:indent].to_i || 0 @pre_path = config[:path_prefix] || '/v1' @path_pos = @pre_path.split('/').length - 1 base = config[:base] || '.' @model = Model.new((config['store.dir'] || File.join(base, 'data')).gsub('$BASE', base), indent) @type_key = config[:type_key] || 'kind' @logger = config[:logger] @logger.level = config[:verbosity] unless @logger.nil? @http_dir = (config['http.dir'] || File.join(base, 'pages')).gsub('$BASE', base) @http_port = (config['http.port'] || 6363).to_i @export_proxy = config[:export_proxy] @export_proxy = true if @export_proxy.nil? # The default is true if not present. @controllers = {} requires = config[:require] case requires when Array requires.each { |r| require r.strip } when String requires.split(',').each { |r| require r.strip } end if config[:handler].is_a?(Array) config[:handler].each { |hh| register_controller(hh[:type], hh[:handler]) } end end |
Instance Attribute Details
#indent ⇒ Object
Returns the value of attribute indent.
17 18 19 |
# File 'lib/wab/impl/shell.rb', line 17 def indent @indent end |
#path_pos ⇒ Object (readonly)
Returns the value of attribute path_pos.
15 16 17 |
# File 'lib/wab/impl/shell.rb', line 15 def path_pos @path_pos end |
#type_key ⇒ Object (readonly)
Returns the path where a data type is located. The default is ‘kind’.
14 15 16 |
# File 'lib/wab/impl/shell.rb', line 14 def type_key @type_key end |
Instance Method Details
#controller(data) ⇒ Object
Returns the controller associated with the type key found in the data. If a controller has not be registered under that key the default controller is returned if there is one.
- data
-
data to extract the type from for lookup in the controllers
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/wab/impl/shell.rb', line 94 def controller(data) path = data.get(:path) path = path.native if path.is_a?(WAB::Data) return path_controller(path) unless path.nil? || (path.length <= @path_pos) content = data.get(:content) return @controllers[content.get(@type_key)] || default_controller unless content.nil? default_controller end |
#data(value = {}, repair = false) ⇒ Object
Create and return a new data instance with the provided initial value. The value must be a Hash or Array. The members of the Hash or Array must be nil, boolean, String, Integer, Float, BigDecimal, Array, Hash, Time, URI::HTTP, or WAB::UUID. Keys to Hashes must be Symbols.
If the repair flag is true then an attempt will be made to fix the value by replacing String keys with Symbols and calling to_h or to_s on unsupported Objects.
- value
-
initial value
- repair
-
flag indicating invalid value should be repaired if possible
123 124 125 |
# File 'lib/wab/impl/shell.rb', line 123 def data(value={}, repair=false) Data.new(value, repair) end |
#path_controller(path) ⇒ Object
Returns the controller according to the type in the path.
path: path Array such as from a URL
108 109 110 |
# File 'lib/wab/impl/shell.rb', line 108 def path_controller(path) @controllers[path[@path_pos]] || default_controller end |
#register_controller(type, controller) ⇒ Object
Register a controller for a named type.
If a request is received for an unregistered type the default controller will be used. The default controller is registered with a nil key.
- type
-
type name
- controller
-
Controller instance for handling requests for the identified
type. This can be a Controller, a Controller class, or a Controller class name.
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/wab/impl/shell.rb', line 78 def register_controller(type, controller) case controller when String controller = Object.const_get(controller).new(self) when Class controller = controller.new(self) end controller.shell = self @controllers[type] = controller end |
#start ⇒ Object
Start listening. This should be called after registering Controllers with the Shell.
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/wab/impl/shell.rb', line 55 def start() mime_types = WEBrick::HTTPUtils::DefaultMimeTypes mime_types['es6'] = 'application/javascript' server = WEBrick::HTTPServer.new(Port: @http_port, DocumentRoot: @http_dir, MimeTypes: mime_types) server.logger.level = 5 - @logger.level unless @logger.nil? server.mount(@pre_path, Handler, self) server.mount('/', ExportProxy, @http_dir) if @export_proxy trap 'INT' do server.shutdown end server.start end |