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 params[:service]
  @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



25
26
27
28
29
# File 'lib/rubyrest/engine.rb', line 25

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

#startObject

Starts the server, embedded or as a detached daemon



33
34
35
36
37
38
39
40
# File 'lib/rubyrest/engine.rb', line 33

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.



44
45
46
47
48
# File 'lib/rubyrest/engine.rb', line 44

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



51
52
53
# File 'lib/rubyrest/engine.rb', line 51

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