class Praxis::Application

Attributes

app[R]
bootloader[RW]
builder[R]
controllers[R]
doc_browser_plugin_paths[RW]
endpoint_definitions[R]
error_handler[RW]
file_layout[RW]
handlers[RW]
loaded_files[RW]
logger[RW]
plugins[RW]
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 18
def self.configure
  yield(instance)
end
new() click to toggle source
# File lib/praxis/application.rb, line 22
def initialize
  @controllers = Set.new
  @endpoint_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 = {}
  @doc_browser_plugin_paths = []
  @handlers = {}
  @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 93
def call(env)
  response = []
  Notifications.instrument 'rack.request.all', 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 104
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 112
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 82
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.
  raise ArgumentError, 'Media type handlers must respond to #generate and #parse' unless handler.respond_to?(:generate) && handler.respond_to?(:parse)

  # Register that thing!
  @handlers[name.to_s] = handler
end
inspect() click to toggle source
# File lib/praxis/application.rb, line 45
def inspect
  "#<#{self.class}##{object_id} @root=#{@root}>"
end
layout(&block) click to toggle source
# File lib/praxis/application.rb, line 100
def layout(&block)
  self.file_layout = FileGroup.new(root, &block)
end
middleware(middleware, *args, &block) click to toggle source
# File lib/praxis/application.rb, line 71
def middleware(middleware, *args, &block)
  @builder.use(middleware, *args, &block)
end
resource_definitions() click to toggle source
DEPRECATED
  • Warn of the change of method name for the transition

# File lib/praxis/application.rb, line 117
def resource_definitions
  raise 'Praxis::Application.instance does not use `resource_definitions` any longer. Use `endpoint_definitions` instead.'
end
setup(root: '.') click to toggle source
# File lib/praxis/application.rb, line 49
def setup(root: '.')
  return self unless @app.nil?

  @root = Pathname.new(root).expand_path

  builtin_handlers = {
    'plain' => Praxis::Handlers::Plain,
    'json' => Praxis::Handlers::JSON
  }
  # 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

  self
end