Class: Synapse::App

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

Overview

See the synopsis section of README.txt for usage.

See the RackRouter project for the kinds of routes you can setup.

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 that the matching Rack::Router route set.

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)


28
29
30
# File 'lib/synapse/base.rb', line 28

def initialize(opts = {})
  @logger = opts[:logger] || Logger.new(STDOUT)
end

Instance Attribute Details

#envObject

Returns the value of attribute env.



22
23
24
# File 'lib/synapse/base.rb', line 22

def env
  @env
end

#loggerObject

Returns the value of attribute logger.



22
23
24
# File 'lib/synapse/base.rb', line 22

def logger
  @logger
end

#paramsObject

Returns the value of attribute params.



22
23
24
# File 'lib/synapse/base.rb', line 22

def params
  @params
end

#responseObject

Returns the value of attribute response.



22
23
24
# File 'lib/synapse/base.rb', line 22

def response
  @response
end

Instance Method Details

#actionObject

The name of the action method, determined by the Route that Rack::Router bound to the incoming request



51
52
53
# File 'lib/synapse/base.rb', line 51

def action
  self.params[:action]
end

#as_rack_appObject

Alias for #router



39
# File 'lib/synapse/base.rb', line 39

def as_rack_app; self.router; end

#call(env) ⇒ Object

The rack #call method



42
43
44
# File 'lib/synapse/base.rb', line 42

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

#routerObject

the router for this Synapse::App. Subclasses are required to override this. (see README.txt for example usage)



34
35
36
# File 'lib/synapse/base.rb', line 34

def router
  raise "#{self.class} must implement a 'router' method that returns a Rack::Router"
end

#theres_no_actionObject

Overridable method that handles missing action that was defined by a route



56
57
58
59
# File 'lib/synapse/base.rb', line 56

def theres_no_action
  self.response[:body] = "Action '#{self.action}' not found in '#{self.class}'"
  self.response[:status_code] = 500
end