Class: RubyRest::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyrest/engine.rb

Overview

The Ruby-on-Rest Engine is the glue between the custom application and the webserver implementation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Engine

Initializes the Ruby-on-Rest Engine, Application and Webserver. The Application must be installed as a rubygem so that the statement require @service works.



13
14
15
16
17
18
19
20
21
# File 'lib/rubyrest/engine.rb', line 13

def initialize( config )
  params = intern_config( config )
  require_library( params )
  @app = Class.by_name( params[:module].capitalize + "::" + params[:service].capitalize + "::Application"  ).new( params )
  if params[:daemon ] != nil
    @daemon = params[:daemon ] 
  else @daemon=true end
  @server = Class.by_name( "RubyRest::" + params[:webserver].to_s.capitalize + "::Server" ).new( @app )
end

Instance Attribute Details

#appObject (readonly)

The application



8
9
10
# File 'lib/rubyrest/engine.rb', line 8

def app
  @app
end

Instance Method Details

#intern_config(hash) ⇒ Object

Returns a new hash, with all the keys as symbols



29
30
31
32
33
# File 'lib/rubyrest/engine.rb', line 29

def intern_config( hash )
  interned = Hash.new
  hash.each{ |k,v| interned[ k.intern ] = v }
  return interned
end

#require_library(params) ⇒ Object

Requires the library params



24
25
26
# File 'lib/rubyrest/engine.rb', line 24

def require_library( params )
  require params[:gem] if params[:gem]
end

#startObject

Starts the server, embedded or as a detached daemon



37
38
39
40
41
42
43
44
# File 'lib/rubyrest/engine.rb', line 37

def start
  if @daemon
    @pid = fork do
      @server.up
    end
    puts "#{self.to_s}: started - pid=#{@pid}, port=#{@app.config[:http_port]}"
  else @server.up end
end

#stopObject

Shutdowns the current server. This is only useful when working in daemon mode.



48
49
50
51
52
# File 'lib/rubyrest/engine.rb', line 48

def stop
  return if @pid == nil
  Process.kill( 9, @pid )
  puts "#{self.to_s}: killed process with pid=#{@pid}"
end

#to_sObject

Returns the deployed application name



55
56
57
# File 'lib/rubyrest/engine.rb', line 55

def to_s 
  "rubyrest: #{@app}"
end