Class: WAB::Impl::Shell

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
ShellLogger
Defined in:
lib/wab/impl/shell.rb

Overview

The shell for reference Ruby implementation.

Instance Attribute Summary collapse

Attributes included from ShellLogger

#logger

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Shell

Sets up the shell with the supplied configuration data.

config

Configuration object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/wab/impl/shell.rb', line 30

def initialize(config)
  @indent       = config[:indent].to_i || 0
  @pre_path     = config[:path_prefix] || '/v1'
  @path_pos     = @pre_path.split('/').length - 1
  @tql_path     = config[:tql_path] || '/tql'
  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  = {}
  @mounts       = config[:handler] || []
	@server       = config['http.server'].to_s.downcase
  requires      = config[:require]
  case requires
  when Array
    requires.each { |r| require r.strip }
  when String
    requires.split(',').each { |r| require r.strip }
  end
end

Instance Attribute Details

#export_proxyObject (readonly)

Returns the value of attribute export_proxy.



20
21
22
# File 'lib/wab/impl/shell.rb', line 20

def export_proxy
  @export_proxy
end

#http_dirObject (readonly)

Returns the value of attribute http_dir.



17
18
19
# File 'lib/wab/impl/shell.rb', line 17

def http_dir
  @http_dir
end

#http_portObject (readonly)

Returns the value of attribute http_port.



18
19
20
# File 'lib/wab/impl/shell.rb', line 18

def http_port
  @http_port
end

#indentObject

Returns the value of attribute indent.



22
23
24
# File 'lib/wab/impl/shell.rb', line 22

def indent
  @indent
end

#mountsObject (readonly)

Returns the value of attribute mounts.



16
17
18
# File 'lib/wab/impl/shell.rb', line 16

def mounts
  @mounts
end

#path_posObject (readonly)

Returns the value of attribute path_pos.



14
15
16
# File 'lib/wab/impl/shell.rb', line 14

def path_pos
  @path_pos
end

#pre_pathObject (readonly)

Returns the value of attribute pre_path.



15
16
17
# File 'lib/wab/impl/shell.rb', line 15

def pre_path
  @pre_path
end

#tql_pathObject (readonly)

Returns the value of attribute tql_path.



19
20
21
# File 'lib/wab/impl/shell.rb', line 19

def tql_path
  @tql_path
end

#type_keyObject (readonly)

Returns the path where a data type is located. The default is ‘kind’.



13
14
15
# File 'lib/wab/impl/shell.rb', line 13

def type_key
  @type_key
end

Instance Method Details

#create_controller(controller) ⇒ Object

Helper function that creates a controller instance given either a class name, a class, or an already created object.



102
103
104
105
106
107
108
109
110
111
# File 'lib/wab/impl/shell.rb', line 102

def create_controller(controller)
  case controller
  when String
    controller = Object.const_get(controller).new(self)
  when Class
    controller = controller.new(self)
  end
  controller.shell = self
  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



96
97
98
# File 'lib/wab/impl/shell.rb', line 96

def data(value={}, repair=false)
  Data.new(value, repair)
end

#log_call(controller, op, path, query, body = nil) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/wab/impl/shell.rb', line 113

def log_call(controller, op, path, query, body=nil)
	if @logger.debug?
	  if body.nil?
	    @logger.debug("#{controller.class}.#{op}(#{path_to_s(path)}#{query})")
	  else
	    body = body.json(@indent) unless body.is_a?(String)
	    @logger.debug("#{controller.class}.#{op}(#{path_to_s(path)}#{query})\n#{body}")
	  end
	elsif @logger.info?
	  @logger.info("#{controller.class}.#{op}(#{path_to_s(path)}#{query})") if @logger.info?
	end
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.



81
82
83
# File 'lib/wab/impl/shell.rb', line 81

def register_controller(type, controller)
	@mount << { type: type, handler: controller }
end

#startObject

Start listening. This should be called after registering Controllers with the Shell.



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/wab/impl/shell.rb', line 58

def start
	case @server
	when 'agoo'
	  require 'wab/impl/agoo'
	  WAB::Impl::Agoo::Server::start(self)
	when 'sinatra'
	  require 'wab/impl/sinatra'
	   WAB::Impl::Sinatra::Server::start(self)
	else
	  require 'wab/impl/webrick'
	  WAB::Impl::WEBrick::Server::start(self)
	end
end