百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
N

nyny

> 编程语言
开源

一个 (可笑地) 小而强大的 Web 框架。

317 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

一个 (可笑地) 小而强大的 Web 框架。

New York, New York.

(ridiculously) small and powerful micro web framework. Install the gem:

bash
gem install nyny

Create the myapp.rb file:

ruby
# myapp.rb

require 'nyny'
class App  /some/folder/
puts NYNY.root.join("foo") #=> /some/folder/foo

To get NYNY's environment, use NYNY.env

ruby
#env.rb
require 'nyny'
puts NYNY.env
puts NYNY.env.production?
bash
$ ruby env.rb
development
false

$ ruby env.rb RACK_ENV=production
production
true

Configuration

You can configure your app by attaching arbitrary properties to config object:

ruby
class App  'bar'

Or, you can use the configure block:

ruby
class App  {:content_type => 'text/html'} do
    'html'
  end
end

What 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:

ruby
class App  'text/html' do
    get '/' do
      'html'
    end

    get '/foo' do
      'foo html'
    end
  end
end

Besides the constraints, you can specify defaults:

ruby
class App  {:format => 'html'} do
    'html'
  end
end

Each block that is passed to a route definition is evaluated in the context of a request scope. See below what methods are available there.

Request scope

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)

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:

ruby
require 'sprockets'
require 'nyny'

class AppWithAssets  'bar' #access it as foo in template
  end
end

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

ruby
class App  ['public']
  use SinatraApp
end

I recommend looking at the list of Rack middlewares

Helpers

NYNY supports helpers as Sinatra does:

ruby
class App < NYNY::App
  helpers MyHelperModule
  helpers do
    def using_a_block_to_define_helper
      true
    end
  end
end

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:

…

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.

F. A. Q.

TBD.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言