2010-08-18 82 views
0

有没有什么办法来操纵Ruby中的URI来接受参数而不使用?&红宝石URI操纵

例如,我要做到以下几点:的

http://localhost:5000/service/get/ip/port

代替

http://localhost:5000/service?ip=192.168.1.1&port=1

要为指定的设备返回信息。这将利用完全基于REST的界面。

示例代码:

hello_proc = lambda do |req,res| 
    res['Content-Type'] = "text/html" 
    res.body = %{ 
    <html><body> 
     Hello. You are calling from a #{req['User-Agent']} 
     <p> 
     I see parameters: #{req.query.keys.join(', ')} 
    </body></html> 
    } 
end 

使用这个网址:http://localhost:5000/a/b 在上面,REQ对一个给定的URL输出将为:

GET /a/b HTTP/1.1

在 '请求',怎么可能一个去处理URI?

预先感谢您。

+0

你使用Rails,Rack或其他来处理http请求吗? – 2010-08-18 21:00:35

+0

是的,WEBrick处理所有传入的http请求。 – Yazdaan 2010-08-18 21:15:13

回答

1

路由就是你要找的。从文档routing at api.rubyonrails.org

路由可以生成漂亮的URL。例如:

map.connect 'articles/:year/:month/:day', 
      :controller => 'articles', 
      :action  => 'find_by_date', 
      :year  => /\d{4}/, 
      :month  => /\d{1,2}/, 
      :day  => /\d{1,2}/ 

更改此了一下你所有的设置了IPS和端口。

使用上面的路线,该URL “本地主机:3000 /用品/ 2005/11/06” 映射到

params = {:year => '2005', :month => '11', :day => '06'} 
+0

这看起来很不错。我会进一步研究。谢谢! – Yazdaan 2010-08-19 20:13:37

1

你应该尝试西纳特拉,这是一个非常轻量级的REST集中层上机架顶部,你可以这样做:

get '/service/get/:ip/:port' do |ip, port| 
    content_type "text/html" 

    "IP: #{ip} PORT: #{port}" 
end 
+0

这会很好,但不幸的是我正在使用一段时间在Rails上开发的服务。尽管如此,谢谢! – Yazdaan 2010-08-19 20:09:58