Tag: sinatra

如何使用Rack接收JSON对象

我有一个非常简单的Ruby Rack服务器,如: app = Proc.new do |env| req = Rack::Request.new(env).params p req.inspect [200, { ‘Content-Type’ => ‘text/plain’ }, [‘Some body’]] end Rack::Handler::Thin.run(app, :Port => 4001, :threaded => true) 每当我使用JSON对象向服务器发送POST HTTP请求时: { “session”: { “accountId”: String, “callId”: String, “from”: Object, “headers”: Object, “id”: String, “initialText”: String, “parameters”: Object, “timestamp”: String, “to”: Object, “userType”: String } } […]

小胡子和哈姆尔

我有这个haml / mustache模板: {{#data}} ok {{#items}} {{#item}} %b ID: {{id}} {{/item}} {{/items}} {{/data}} 而且我有Illegal nesting: nesting within plain text is illegal错误。 我在Sinatra渲染它 Mustache.render(haml(:index), hash)

Sinatra具有持久变量

我的sinatra应用程序必须解析一个~60MB的XML文件。 这个文件几乎没有变化:在夜间的cron工作中,它被另一个人覆盖。 是否存在将解析后的文件作为变量保存在内存中的技巧或方法,以便我可以在传入请求中读取它,但不必为每个传入请求一遍又一遍地解析它? 一些Pseudocode来说明我的问题。 get ‘/projects/:id’ return @nokigiri_object.search(“//projects/project[@id=#{params[:id]}]/name/text()”) end post ‘/projects/update’ if params[:token] == “s3cr3t” @nokogiri_object = reparse_the_xml_file end end 我需要知道的是,如何创建这样一个@nokogiri_object,以便在Sinatra运行时它仍然存在。 这有可能吗? 或者我需要一些存储空间?

Heroku应用程序无法启动 – “require”:没有要加载的文件 – sinatratestapp(LoadError)

我正在尝试使用bamboo-mri-1.9.2堆栈运行我的Heroku应用程序。 当然它在Ruby 1.9.2上运行良好。 但是在生产时它会在启动时在执行config.ru时崩溃,如下所示: require ‘sinatratestapp’ run Sinatra::Application 我的.gems文件: sinatra –version ‘>= 1.0’ 而应用程序本身就是sinatratestapp.rb : require ‘rubygems’ require ‘sinatra’ get ‘/’ do “Hello from Sinatra on Heroku!” end 这就是我在项目中的所有内容,并尝试在Heroku结果上运行: :29:in `require’: no such file to load — sinatratestapp (LoadError) from :29:in `require’ from config.ru:1:in `block (3 levels) in ‘ … —–> Your application is requiring […]

使用Bundler从Github安装gem

我正在尝试使用这里的说明来安装带有bundler的预发布版本的gem。 “bundle install”输出将gem列为已安装,但“gem list”无法找到它。 我的Gemfile: source :gemcutter gem ‘sinatra’, ‘1.1.0’, :git => ‘http://github.com/sinatra/sinatra.git’ gem ‘RedCloth’, ‘4.2.3’ 这是我的其余示例代码的要点 。 有没有人让这个场景奏效? 注意:我也在使用RVM(在OS X上)。 bundle show会将gem(和依赖项)列为现有的,但我无法正确解析它们。 谢谢。

简单的Ruby输入validation库

我一直在寻找一个简单的Ruby输入validation库。 一切似乎都指向ActiveRecord(或类似的)。 我没有使用Rails,我在没有ORM的情况下使用Sinatra。 validation用户输入的最佳方法是什么(不直接与模型层绑定)? 简单的事情,如“字符串长度”,“是数字”等。最好有一个很好的机制来声明错误消息。

使用异步sinatra流多个身体

我想从javascript开始一个很长的轮询请求,这很好,我希望我的ruby编程将多个主体部分流式传输到javascript。 为什么以下(伪)代码不起作用? require ‘rubygems’ require ‘sinatra/async’ require ‘eventmachine’ require ‘thin’ require ‘json’ class Test [ “this is part #{c}” ] }.to_json end end end run! end 也许我误解了长期轮询和异步应该做什么,但我的期望是我将多个机构送回客户端? 我需要使用eventmachine吗? 谢谢

通过HTTP流控制台输出(使用Ruby)

我试图远程运行一些命令,并且SSH不能进入机器。 我要做的是设置一个运行一些特定命令的Sinatra应用程序,并通过HTTP流输出输出。 示例操作如下所示: get “/log” do `tail -f some.log` end 1据我所知,我需要使用Unicorn(或Mongrel),因为Thin不支持流数据2我认为我需要通过某种IO ruby​​对象来管道输出命令 我几乎知道怎么做(1)但不知道如何实现(2)。

Ruby Sinatra Webservice在localhost上运行:4567但不在IP上运行

我在Windows 7 32位操作系统上有一个ruby(使用sinatra)webservice。 它在端口4567上运行。当我使用localhost时工作正常:4567但是当我用我的机器的本地ip替换localhost时, 192.168.103.99 :4567它不起作用,并且无法连接。 我已经禁用了防火墙,旁路代理并将端口4567添加到exception,仍然没有运气。 可能是什么问题?

使用rackup时未找到Sinatra静态资产

我有一个简单的Sinatra应用程序,使用模块化样式进行配置。 当我按照自述文件中的建议使用rackup -p 4567启动应用程序时,不会提供公用文件夹中的静态资源。 但是当我使用shotgun ./config.ru -p 4567开始它时,它们就会被送达。 为什么会这样? 这可能发生在生产中吗? 这是我的代码: # config.ru require ‘rubygems’ require ‘bundler’ require ‘sinatra’ require ‘jammit’ Bundler.require Jammit.package! require File.expand_path(‘./stick.rb’) run Stick 这是应用程序ruby文件 require ‘sinatra/base’ class Stick < Sinatra::Base get '/' do haml :index end end