Class: Synfeld::App

Inherits:
Object
  • Object
show all
Defined in:
lib/synfeld/base.rb

Overview

See the synopsis section of README.rdoc for usage.

See the README.rdoc for an overview of an Synfeld::App, and see the Rack::Mount project for more information on Rack::Mount style routing.

Variables of note:

@response
   a hash with keys :body, :headers, :status_code, the 3 items all rack handlers are expected to set.
   Body is a string, status code is an http status code integer, and headers is a hash that
   should conform to rack's contract.

@env
   The rack env passed into this apps #call method

@params
   The params as determined by the matching Rack::Mount route.

@root_dir
   This dir is prepended to relative paths to locate files.

@logger
   Either you pass in the @logger that synfeld uses, or it sets one up on STDOUT.

Constant Summary collapse

@@__regex_colon =

ROUTING

(RUBY_VERSION =~ /^1.8/)? ':' : ''

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ App

Options:

:logger => where to log to.
           Note this is not the same thing as the rack access log (although you
           can pass that logger in if you want). Default: Logger.new(STDOUT)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/synfeld/base.rb', line 35

def initialize(opts = {})

  @logger = opts[:logger]
  if self.logger.nil?
    @logger = Logger.new(STDOUT)
    puts "WARNING: Synfeld not configured with a logger, using STDOUT. Won't have much to say if running as a daemon."
  end

  @root_dir = opts[:root_dir] 
  if self.root_dir.nil?
    raise "You have to pass in the location of the 'root_dir', where all the files in your synfeld app are located" 
  end

  Kernel.at_exit {self.whine("Alright, I'm outta here.")}
end

Instance Attribute Details

#envObject

Returns the value of attribute env.



29
30
31
# File 'lib/synfeld/base.rb', line 29

def env
  @env
end

#loggerObject

Returns the value of attribute logger.



29
30
31
# File 'lib/synfeld/base.rb', line 29

def logger
  @logger
end

#paramsObject

Returns the value of attribute params.



29
30
31
# File 'lib/synfeld/base.rb', line 29

def params
  @params
end

#responseObject

Returns the value of attribute response.



29
30
31
# File 'lib/synfeld/base.rb', line 29

def response
  @response
end

#root_dirObject

Returns the value of attribute root_dir.



29
30
31
# File 'lib/synfeld/base.rb', line 29

def root_dir
  @root_dir
end

Instance Method Details

#actionObject

The name of the action method bound to the route that mathed the incoming request.



106
107
108
# File 'lib/synfeld/base.rb', line 106

def action
  self.params[:action]
end

#add_route(string_or_regex, opts = {}) ⇒ Object

See the README for a full explanation of how to use this method.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/synfeld/base.rb', line 81

def add_route(string_or_regex, opts = {})
  raise "You have to provide an :action method to call" unless opts[:action]
  method = (opts.delete(:method) || 'GET').to_s.upcase
  # Adapt string_or_regex into a rack-mount regex route. If it is a string, convert it to a
  # rack-mount compatable regex. In paths that look like /some/:var/in/path, convert the ':var'
  # bits to rack-mount variables.
  if string_or_regex.is_a?(String)
    regex_string = "^" + string_or_regex.gsub(/:(([^\/]+))/){|s| "(?#{@@__regex_colon}<#{$1}>.*)" } + "$"
    regex = %r{#{regex_string}}
    #puts regex_string # dbg
  else
    regex = string_or_regex
  end

  # Add the route to rack-mount
  @set.add_route(self,
                {:path_info => regex, :request_method => method.upcase},
                opts)
end

#as_rack_appObject

Return self as a rackup-able rack application.



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/synfeld/base.rb', line 56

def as_rack_app
  routes = Rack::Mount::RouteSet.new_without_optimizations do |set|
    @set = set
    self.add_routes
    add_route %r{^.*$},  :action => "render_static" 
  end
  app = Rack::Builder.new {
    #use Rack::CommonLogger, $stderr
    #use Rack::Reloader, 0
    run routes
  }.to_app
end

#call(env) ⇒ Object

The rack #call method



70
71
72
# File 'lib/synfeld/base.rb', line 70

def call(env)
  dup._call(env) # be thread-safe
end