a (ridiculously) small and powerful web framework.
(ridiculously) small and powerful micro web framework. Install the gem:
gem install nynyCreate the myapp.rb file:
# myapp.rb
require 'nyny'
class App /some/folder/
puts NYNY.root.join("foo") #=> /some/folder/fooTo get NYNY's environment, use NYNY.env
#env.rb
require 'nyny'
puts NYNY.env
puts NYNY.env.production?$ ruby env.rb
development
false
$ ruby env.rb RACK_ENV=production
production
trueYou can configure your app by attaching arbitrary properties to config object:
class App 'bar'Or, you can use the configure block:
class App {:content_type => 'text/html'} do
'html'
end
endWhat are constraints? Constraints are assertions on the request object.
That means that the route from above will match only if
request.content_type is 'text/html'.
To group multiple routes for a single constraint, use the constraints block:
class App 'text/html' do
get '/' do
'html'
end
get '/foo' do
'foo html'
end
end
endBesides the constraints, you can specify defaults:
class App {:format => 'html'} do
'html'
end
endEach block that is passed to a route definition is evaluated in the context of a request scope. See below what methods are available there.
As was said above, when you pass a block to a route definition, that block is evaluated in the context of a RequestScope. This means that several methods/objects available inside that block:
request - A Rack::Request object which encapsulates the request
to that route. (see Rack::Request documentation for more info)response - A Rack::Response object which encapsulates the response.params - a hash which contains both POST body params and GET querystring params.headers - a hash with the response headers
(ex: headers['Content-Type'] = 'text/html')status - allows you to set the status of the response (ex: status 403)redirect_to - sets the response to redirect
(ex: redirect_to 'http://google.com')cookies - a hash which allows you to access/modify/remove cookies
(ex: cookies[:foo] = 'bar' or cookies.delete[:foo])session - a hash which allows you to access/modify/remove session variables
(ex: session[:foo] = 'bar')halt - allows you to instantly return a response, interrupting current
handler execution (see halt)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:
require 'sprockets'
require 'nyny'
class AppWithAssets 'bar' #access it as foo in template
end
endTo render a template with a layout, you need to render both files. It's best to create a helper for that:
class App ['public']
use SinatraApp
endI recommend looking at the list of Rack middlewares
NYNY supports helpers as Sinatra does:
class App < NYNY::App
helpers MyHelperModule
helpers do
def using_a_block_to_define_helper
true
end
end
endUsing 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.
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:
…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.
TBD.
git checkout -b my-new-feature)git commit -am 'Add some feature')git push origin my-new-feature)No open issues yet, or sync has not completed.