Also, NYNY provides a simple api for hooking into the app's initialization:

classApp<NYNY::Appbefore_initializedo|app|#this will be executed just before the Rack app is compiled#'app' is the a App instanceendafter_initializedo|app,rack_app|#this will be executed after the Rack app is compiled#'app' is the a App instance#'rack_app' is the main block which will be called on any requestendend

Running

by requiring it in a config.ru file, and then passing it as argument to the
Rack's run function:

# config.rurequire'app'runApp.new

by using the run! method directly on the app class:

# app.rb# ...app class definition...App.run!

run! takes the port number as optional argument (the default port is 9292).
Also the run! method will include 2 default middlewares to make the
development easier: Rack::CommonLogger and BetterErrors::Middleware (only in dev).
This will show all requests in the log, and will provide useful details
in the case a error occurs during a request.

Defining routes

NYNY uses Journey for routing, that means that NYNY has all the
awesomeness the Rails' router has. NYNY supports the following verbs for defining a route: delete, get, head,
options, patch, post, put and trace.

Maps

NYNY uses a Rack::Builder underneath to generate a proper Rack app when it is
being run. Because of that, NYNY exposes the Builder's map method to allow
fine grained low-level url mapping. The most common use case for that is to
integrate sprockets into a NYNY app:

Please note that none of the NYNY's routing helpers are being exposed in a
map block, the block gets executed in the context of a Rack::Builder
(stuff you're used to see in a config.ru file)

Namespaces

You can define namespaces for routes in NYNY. Each namespace is an isolated
app, which means that you can use the same api that you use in your top app there:

classPing<NYNY::Appget'/'do#this will be accessbile at '/ping''pong'endendclassApp<NYNY::Appget'/'do'Hello'end# You can pass a Rack app to the namespacenamespace'/ping',Ping# Or you can pass a block, which will create the app for younamespace'/nested'douseSomeMiddlewarehelpersSomeHelpersget'/'do# this will be accessible at '/nested''Hello from namespace!'endendend

Templates

NYNY can render templates, all you need is to call the render function:

classApp<NYNY::Appget'/'dorender'index.erb'endend

There are 2 ways to pass data to the template:

Via a instance variable:

classApp<NYNY::Appget'/'do@foo='bar'#access it as @foo in templaterender'index.erb'endend

Or via a local variable:

classApp<NYNY::Appget'/'dorender'index.erb',:foo=>'bar'#access it as foo in templateendend

To render a template with a layout, you need to render both files. It's best
to create a helper for that:

NYNY uses Tilt for templating, so the list of supported engines is pretty complete.

Filters

Unlike Sinatra, NYNY supports only "generic" before and after filters.
This means that you can't declare a filter to execute depending on a URL pattern.
However, you can obtain the same effect by calling next in a before block
if the request.path matches a pattern.

Helpers

Using a helper implies that the helper module is included in the RequestScope,
and that all the methods in that module will be available inside a route
definition block.

Extensions

Since version 2.0.0, NYNY added support for extensions.
This makes possible to include helpers, middlewares and custom app class
methods inside a single module:

moduleMyKewlExtensionclassMiddlewaredefinitializeapp@app=appenddefcallenvenv['KEWL']=true@app.call(env)if@appendendmoduleHelpersdefthe_ultimate_answer42endenddefget_or_postroute,&blockgetroute,&blockpostroute,&blockenddefself.registeredappapp.useMiddlewareapp.helpersHelpersapp.get_or_post'/'do"After many years of hard computation, the answer is #{the_ultimate_answer}"endendendclassApp<NYNY::AppregisterMyKewlExtensionendApp.run!

By default, the App class will extend the provided extension module.
Optionally, an extension can add a registered method, which will be invoked
once the extension is registered. That method will be called with the app class
as a parameter.