Class: Praxis::Application
- Inherits:
-
Object
- Object
- Praxis::Application
- Includes:
- Singleton
- Defined in:
- lib/praxis/application.rb
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#bootloader ⇒ Object
Returns the value of attribute bootloader.
-
#builder ⇒ Object
readonly
Returns the value of attribute builder.
-
#controllers ⇒ Object
readonly
Returns the value of attribute controllers.
-
#doc_browser_plugin_paths ⇒ Object
Returns the value of attribute doc_browser_plugin_paths.
-
#error_handler ⇒ Object
Returns the value of attribute error_handler.
-
#file_layout ⇒ Object
Returns the value of attribute file_layout.
-
#handlers ⇒ Object
Returns the value of attribute handlers.
-
#loaded_files ⇒ Object
Returns the value of attribute loaded_files.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#plugins ⇒ Object
Returns the value of attribute plugins.
-
#resource_definitions ⇒ Object
readonly
Returns the value of attribute resource_definitions.
-
#root ⇒ Object
Returns the value of attribute root.
-
#router ⇒ Object
readonly
Returns the value of attribute router.
-
#validation_handler ⇒ Object
Returns the value of attribute validation_handler.
-
#versioning_scheme ⇒ Object
Returns the value of attribute versioning_scheme.
Class Method Summary collapse
Instance Method Summary collapse
- #call(env) ⇒ Object
- #config(key = nil, type = Attributor::Struct, **opts, &block) ⇒ Object
- #config=(config) ⇒ Object
-
#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.
-
#initialize ⇒ Application
constructor
A new instance of Application.
- #layout(&block) ⇒ Object
- #middleware(middleware, *args, &block) ⇒ Object
- #setup(root: '.') ⇒ Object
Constructor Details
#initialize ⇒ Application
Returns a new instance of Application.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# 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 |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
12 13 14 |
# File 'lib/praxis/application.rb', line 12 def app @app end |
#bootloader ⇒ Object
Returns the value of attribute bootloader.
15 16 17 |
# File 'lib/praxis/application.rb', line 15 def bootloader @bootloader end |
#builder ⇒ Object (readonly)
Returns the value of attribute builder.
13 14 15 |
# File 'lib/praxis/application.rb', line 13 def builder @builder end |
#controllers ⇒ Object (readonly)
Returns the value of attribute controllers.
10 11 12 |
# File 'lib/praxis/application.rb', line 10 def controllers @controllers end |
#doc_browser_plugin_paths ⇒ Object
Returns the value of attribute doc_browser_plugin_paths.
20 21 22 |
# File 'lib/praxis/application.rb', line 20 def doc_browser_plugin_paths @doc_browser_plugin_paths end |
#error_handler ⇒ Object
Returns the value of attribute error_handler.
23 24 25 |
# File 'lib/praxis/application.rb', line 23 def error_handler @error_handler end |
#file_layout ⇒ Object
Returns the value of attribute file_layout.
16 17 18 |
# File 'lib/praxis/application.rb', line 16 def file_layout @file_layout end |
#handlers ⇒ Object
Returns the value of attribute handlers.
21 22 23 |
# File 'lib/praxis/application.rb', line 21 def handlers @handlers end |
#loaded_files ⇒ Object
Returns the value of attribute loaded_files.
17 18 19 |
# File 'lib/praxis/application.rb', line 17 def loaded_files @loaded_files end |
#logger ⇒ Object
Returns the value of attribute logger.
18 19 20 |
# File 'lib/praxis/application.rb', line 18 def logger @logger end |
#plugins ⇒ Object
Returns the value of attribute plugins.
19 20 21 |
# File 'lib/praxis/application.rb', line 19 def plugins @plugins end |
#resource_definitions ⇒ Object (readonly)
Returns the value of attribute resource_definitions.
11 12 13 |
# File 'lib/praxis/application.rb', line 11 def resource_definitions @resource_definitions end |
#root ⇒ Object
Returns the value of attribute root.
22 23 24 |
# File 'lib/praxis/application.rb', line 22 def root @root end |
#router ⇒ Object (readonly)
Returns the value of attribute router.
9 10 11 |
# File 'lib/praxis/application.rb', line 9 def router @router end |
#validation_handler ⇒ Object
Returns the value of attribute validation_handler.
24 25 26 |
# File 'lib/praxis/application.rb', line 24 def validation_handler @validation_handler end |
#versioning_scheme ⇒ Object
Returns the value of attribute versioning_scheme.
26 27 28 |
# File 'lib/praxis/application.rb', line 26 def versioning_scheme @versioning_scheme end |
Class Method Details
.configure {|self.instance| ... } ⇒ Object
29 30 31 |
# File 'lib/praxis/application.rb', line 29 def self.configure yield(self.instance) end |
Instance Method Details
#call(env) ⇒ Object
112 113 114 115 116 117 |
# 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) ⇒ Object
123 124 125 126 127 128 129 |
# 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) ⇒ Object
131 132 133 |
# File 'lib/praxis/application.rb', line 131 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.
99 100 101 102 103 104 105 106 107 108 109 110 |
# 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) ⇒ Object
119 120 121 |
# File 'lib/praxis/application.rb', line 119 def layout(&block) self.file_layout = FileGroup.new(self.root, &block) end |
#middleware(middleware, *args, &block) ⇒ Object
88 89 90 |
# File 'lib/praxis/application.rb', line 88 def middleware(middleware, *args, &block) @builder.use(middleware, *args, &block) end |
#setup(root: '.') ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/praxis/application.rb', line 57 def setup(root: '.') return self unless @app.nil? @root = Pathname.new(root). 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 |