class Praxis::Application

Attributes

app[R]
bootloader[RW]
builder[R]
controllers[R]
doc_browser_plugin_paths[RW]
error_handler[RW]
file_layout[RW]
handlers[RW]
loaded_files[RW]
logger[RW]
plugins[RW]
resource_definitions[R]
root[RW]
router[R]
validation_handler[RW]
versioning_scheme[RW]

Public Class Methods

configure() { |instance| ... } click to toggle source
# File lib/praxis/application.rb, line 29
def self.configure
  yield(self.instance)
end
new() click to toggle source
# File lib/praxis/application.rb, line 33
def initialize
  @controllers = Set.new
  @resource_definitions = Set.new

  @error_handler = ErrorHandler.new
  @validation_handler = ValidationHandler.new

  @router = Router.new(self)

  @builder = Rack::Builder.new
  @app = nil

  @bootloader = Bootloader.new(self)
  @file_layout = nil
  @plugins = Hash.new
  @doc_browser_plugin_paths = []
  @handlers = Hash.new
  @loaded_files = Set.new
  @config = Config.new
  @root = nil
  @logger = Logger.new(STDOUT)
end

Public Instance Methods

call(env) click to toggle source
# File lib/praxis/application.rb, line 112
def call(env)
  response = []
  Notifications.instrument 'rack.request.all'.freeze, response: response do
    response.push(*@app.call(env))
  end
end
config(key=nil, type=Attributor::Struct, **opts, &block) click to toggle source
# File lib/praxis/application.rb, line 123
def config(key=nil, type=Attributor::Struct, **opts, &block)
  if block_given? || (type==Attributor::Struct && !opts.empty? )
    @config.define(key, type, opts, &block)
  else
    @config.get
  end
end
config=(config) click to toggle source
# File lib/praxis/application.rb, line 131
def config=(config)
  @config.set(config)
end
handler(name, handler) click to toggle source

Register a media type handler used to transform medias' structured data to HTTP response entitites with a specific encoding (JSON, XML, etc) and to parse request bodies into structured data.

@param [String] name @param [Class] a class that responds to .new, parse and generate

# File lib/praxis/application.rb, line 99
def handler(name, handler)
  # Construct an instance, if the handler is a class and needs to be initialized.
  handler = handler.new

  # Make sure it quacks like a handler.
  unless handler.respond_to?(:generate) && handler.respond_to?(:parse)
    raise ArgumentError, "Media type handlers must respond to #generate and #parse"
  end

  # Register that thing!
  @handlers[name.to_s] = handler
end
layout(&block) click to toggle source
# File lib/praxis/application.rb, line 119
def layout(&block)
  self.file_layout = FileGroup.new(self.root, &block)
end
middleware(middleware, *args, &block) click to toggle source
# File lib/praxis/application.rb, line 88
def middleware(middleware, *args, &block)
  @builder.use(middleware, *args, &block)
end
setup(root: '.') click to toggle source
# File lib/praxis/application.rb, line 57
def setup(root: '.')
  return self unless @app.nil?

  @root = Pathname.new(root).expand_path

  builtin_handlers = {
    'plain' => Praxis::Handlers::Plain,
    'json' => Praxis::Handlers::JSON,
    'x-www-form-urlencoded' => Praxis::Handlers::WWWForm
  }
  # Register built-in handlers unless the app already provided its own
  builtin_handlers.each_pair do |name, handler|
    self.handler(name, handler) unless handlers.key?(name)
  end

  @bootloader.setup!

  @builder.run(@router)
  @app = @builder.to_app

  Notifications.subscribe 'rack.request.all'.freeze do |name, start, finish, _id, payload|
    duration = (finish - start) * 1000
    Stats.timing(name, duration)

    status, _, _ = payload[:response]
    Stats.increment "rack.request.#{status}"
  end

  self
end