Class: Praxis::Application

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/praxis/application.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApplication

Returns a new instance of Application.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



14
15
16
# File 'lib/praxis/application.rb', line 14

def app
  @app
end

#bootloaderObject

Returns the value of attribute bootloader.



16
17
18
# File 'lib/praxis/application.rb', line 16

def bootloader
  @bootloader
end

#builderObject (readonly)

Returns the value of attribute builder.



14
15
16
# File 'lib/praxis/application.rb', line 14

def builder
  @builder
end

#controllersObject (readonly)

Returns the value of attribute controllers.



14
15
16
# File 'lib/praxis/application.rb', line 14

def controllers
  @controllers
end

#doc_browser_plugin_pathsObject

Returns the value of attribute doc_browser_plugin_paths.



16
17
18
# File 'lib/praxis/application.rb', line 16

def doc_browser_plugin_paths
  @doc_browser_plugin_paths
end

#endpoint_definitionsObject (readonly)

Returns the value of attribute endpoint_definitions.



14
15
16
# File 'lib/praxis/application.rb', line 14

def endpoint_definitions
  @endpoint_definitions
end

#error_handlerObject

Returns the value of attribute error_handler.



16
17
18
# File 'lib/praxis/application.rb', line 16

def error_handler
  @error_handler
end

#file_layoutObject

Returns the value of attribute file_layout.



16
17
18
# File 'lib/praxis/application.rb', line 16

def file_layout
  @file_layout
end

#handlersObject

Returns the value of attribute handlers.



16
17
18
# File 'lib/praxis/application.rb', line 16

def handlers
  @handlers
end

#loaded_filesObject

Returns the value of attribute loaded_files.



16
17
18
# File 'lib/praxis/application.rb', line 16

def loaded_files
  @loaded_files
end

#loggerObject

Returns the value of attribute logger.



16
17
18
# File 'lib/praxis/application.rb', line 16

def logger
  @logger
end

#pluginsObject

Returns the value of attribute plugins.



16
17
18
# File 'lib/praxis/application.rb', line 16

def plugins
  @plugins
end

#rootObject

Returns the value of attribute root.



16
17
18
# File 'lib/praxis/application.rb', line 16

def root
  @root
end

#routerObject (readonly)

Returns the value of attribute router.



14
15
16
# File 'lib/praxis/application.rb', line 14

def router
  @router
end

#validation_handlerObject

Returns the value of attribute validation_handler.



16
17
18
# File 'lib/praxis/application.rb', line 16

def validation_handler
  @validation_handler
end

#versioning_schemeObject

Returns the value of attribute versioning_scheme.



16
17
18
# File 'lib/praxis/application.rb', line 16

def versioning_scheme
  @versioning_scheme
end

Class Method Details

.configure {|instance| ... } ⇒ Object

Yields:

  • (instance)


18
19
20
# File 'lib/praxis/application.rb', line 18

def self.configure
  yield(instance)
end

Instance Method Details

#call(env) ⇒ Object



93
94
95
96
97
98
# 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) ⇒ Object



104
105
106
107
108
109
110
# 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) ⇒ Object



112
113
114
# File 'lib/praxis/application.rb', line 112

def config=(config)
  @config.set(config)
end

#handler(name, handler) ⇒ Object

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.

Parameters:

  • name (String)
  • a (Class)

    class that responds to .new, #parse and #generate

Raises:

  • (ArgumentError)


82
83
84
85
86
87
88
89
90
91
# 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

#inspectObject



45
46
47
# File 'lib/praxis/application.rb', line 45

def inspect
  "#<#{self.class}##{object_id} @root=#{@root}>"
end

#layout(&block) ⇒ Object



100
101
102
# File 'lib/praxis/application.rb', line 100

def layout(&block)
  self.file_layout = FileGroup.new(root, &block)
end

#middleware(middleware, *args, &block) ⇒ Object



71
72
73
# File 'lib/praxis/application.rb', line 71

def middleware(middleware, *args, &block)
  @builder.use(middleware, *args, &block)
end

#resource_definitionsObject

DEPRECATED
  • Warn of the change of method name for the transition



117
118
119
# 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: '.') ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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