Class: Tap::Server

Inherits:
Object
  • Object
show all
Includes:
Configurable, Rack::Utils
Defined in:
lib/tap/server.rb,
lib/tap/server/data.rb,
lib/tap/server/server_error.rb

Overview

::configurable

Defined Under Namespace

Classes: Data, ServerError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}, app = Tap::App.instance, &block) ⇒ Server

Returns a new instance of Server.



32
33
34
35
36
37
38
# File 'lib/tap/server.rb', line 32

def initialize(config={}, app=Tap::App.instance, &block)
  @handler = nil
  @controller = block
  
  @app = app
  initialize_config(config)
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



29
30
31
# File 'lib/tap/server.rb', line 29

def app
  @app
end

#controllerObject (readonly)

Returns the value of attribute controller.



30
31
32
# File 'lib/tap/server.rb', line 30

def controller
  @controller
end

Instance Method Details

#admin?(input) ⇒ Boolean

Returns true if input is equal to the secret, if a secret is set. Used to test if a particular request has rights to a remote administrative action.

Returns:

  • (Boolean)


60
61
62
# File 'lib/tap/server.rb', line 60

def admin?(input)
  secret != nil && input == secret
end

#bind(controller) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/tap/server.rb', line 44

def bind(controller)
  if controller.kind_of?(String)
    controller = app.resolve(controller)
  end
  
  unless controller.respond_to?(:call)
    raise "invalid controller: #{controller.inspect}"
  end
  
  @controller = controller
  self
end

#call(rack_env) ⇒ Object

The Rack interface method.



73
74
75
76
77
78
79
80
81
# File 'lib/tap/server.rb', line 73

def call(rack_env)
  # handle the request
  rack_env['tap.server'] = self
  controller.call(rack_env)
rescue ServerError
  $!.response
rescue Exception
  ServerError.response($!)
end

#envObject



40
41
42
# File 'lib/tap/server.rb', line 40

def env
  app.env
end

#module_path(path, klass) ⇒ Object



68
69
70
# File 'lib/tap/server.rb', line 68

def module_path(path, klass)
  app.env.module_path(:views, klass.ancestors, path) {|file| File.file?(file) }
end

#run!(handler = rack_handler) ⇒ Object

Runs self as configured, on the specified server, host, and port. Use an INT signal to interrupt.



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/tap/server.rb', line 85

def run!(handler=rack_handler)
  return self if @handler

  handler.run(self, :Host => host, :Port => port) do |handler|
    @handler = handler
    trap(:INT) { stop! }
    yield if block_given?
  end

  self
end

#stop!Object

Stops the server if running (ie a handler is set).



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/tap/server.rb', line 98

def stop!
  if @handler
    # Use thins' hard #stop! if available, otherwise just #stop
    @handler.respond_to?(:stop!) ? @handler.stop! : @handler.stop
    @handler = nil
  
    yield if block_given?
  end

  self
end

#template_path(path) ⇒ Object



64
65
66
# File 'lib/tap/server.rb', line 64

def template_path(path)
  app.env.path(:views, path) {|file| File.file?(file) }
end