Tag: sinatra

Sinatra请求对象

我可能在这里遗漏了一些非常明显的东西,但我似乎无法找到答案,或者自己解决这个问题。 在Sinatra中,他们有一个self.get方法,它捕获块,当一个块被调用时,你可以在里面使用request变量,这怎么可能? 西纳特拉 module Sinatra class Base class Request < Rack::Request end attr_accessor :request def call!(env) @request = Request.new(env) end class << self def get(path, opts = {}, &block) … end end end end 应用 class App < Sinatra::Base get '/' do puts request end end

DataMapper – 单表inheritance

有人可以向我解释这里发生了什么吗? 这是一个我放在一起展示你们最新情况的例子: class Person include DataMapper::Resource property :id, Serial property :type, Discriminator property :name, String property :age, Integer end class Male < Person end class Father < Male property :job, String end class Son < Male end class Female < Person end class Mother < Female property :favorite_song, String end class Daughter < Female end […]

Sinatra:三个日志

我正在使用一个非常简单的Sinatra应用程序,效果很好。 但是,每条日志消息重复三次。 我可以通过禁用Sinatra日志记录将其降低到2 disable :logging 但我还有两个。 消息略有不同,所以我收集他们来自Rack和堆栈中的其他地方。 如何完全禁用成功Web请求的记录?

Heroku和Datamapper问题

我可以在Heroku上启动一个基本应用程序,显示带有’/’的消息…工作得很好。 但是,每当我尝试使用datamapper添加sqlite时,事情就会崩溃。 要查看我的应用程序结构,请查看github上的项目。 我保持代码相当简陋。 在heroku的日志中我得到: 2011-06-26T21:28:36+00:00 app[web.1]: /app/.bundle/gems/ruby/1.9.1/gems/dm-core-1.1.0/lib/dm-core/adapters.rb:163:in `require’: no such file to load — dm-postgres-adapter (LoadError) 关于这一点的是我没有使用postgres,所以我很困惑为什么这么说。

基于ruby的服务器的自适应映像

我想根据用户屏幕的大小向客户端发送图像,如果我有高分辨率图像,并且用户想要在移动设备上查看它我不想发送完整的高清图像,而是我想将缩小的图像发送到用户的设备尺寸,这将增加低带宽设备的加载速度 这里有一个解决方案 自适应图像可检测访问者的屏幕大小,并自动创建,缓存和提供设备适当的重新缩放版本的网页嵌入式HTML图像。它旨在与响应式设计一起使用,并与流体图像技术结合使用。 (来自给定的链接) 我看到这种基于php和apache的服务器的解决方案,是否有基于ruby服务器的Web应用程序的解决方案? gem可能是…谢谢

如何让Twitter Bootstrap的Less文件与Sinatra AssetPack一起使用?

我试图让Bootstrap的Less文件与Sinatra AssetPack一起使用,但我得到的解析器错误更少。 这些错误让我相信通过bootstrap.less导入的文件越少,彼此之间就无法识别。 我有一个app.rb文件: require ‘sinatra/base’ require ‘sinatra/assetpack’ class App < Sinatra::Base set :root, File.dirname(__FILE__) register Sinatra::AssetPack assets do css :bootstrap, [ '/css/bootstrap.css' ] end get '/' do erb :index end # start the server if ruby file executed directly run! if app_file == $0 end 我已将所有Bootstrap less文件复制到/app/css目录并修改了bootstrap.less,以便每个@import语句以.css而不是.less结尾(但实际的文件扩展名没有更改)。 我还把一切都放在了Github上: https : //github.com/elevine/sinatra-assetpack-bootstrap 这是我得到的一个错误的堆栈跟踪的前半部分: Less::ParseError – […]

如何知道救援的例外情况?

在Ruby中使用特定的代码库时,我经常发现自己不知道要解救什么exception。 例如,我经常使用HTTParty来处理我的rails / sinatra app会发出的任何HTTP请求。 我挖掘了HTTParty的代码,发现了一个包含已定义的exception的文件。 大! 我会在提出要求时拯救他们。 为了测试它,我为请求输入了一个虚假的域名,但我没有得到HTTParty :: ResponseErrorexception,而是得到了一个SocketErrorexception。 处理这个问题的最佳方法是什么? 我知道HTTParty是Ruby实现的包装器,这可能是抛出SocketErrorexception的原因。 但我怎么知道呢? 我可以通过拯救“例外”来解决这个问题,但这是非常糟糕的做法。 我宁愿清楚我可能造成的exception并处理这些exception。 编辑:我应该澄清一下,真正促使我创建这个问题的是我不知道如何能够找出在调用特定函数时可能引发的exception…也就是说,无需查看每个函数调用在堆栈中。

在Sinatra中运行后台进程

我有Sinatra / Rails应用程序和一个开始一些漫长过程的动作。 普通的我为后台工作排队。 但是这种情况太简单了,后台进程很少开始,所以队列是开销。 那么如何在没有队列的情况下运行后台进程? get “/build_logs/:project” do LogBuilder.new(params[:project]).generate “done” end 我试图将它作为一个新的Thread或Process fork,但它没有帮助。

在RSpec中记录RestClient响应

我有以下规格…… describe “successful POST on /user/create” do it “should redirect to dashboard” do post ‘/user/create’, { :name => “dave”, :email => “dave@dave.com”, :password => “another_pass” } last_response.should be_redirect follow_redirect! last_request.url.should == ‘http://example.org/dave/dashboard’ end end Sinatra应用程序上的post方法使用rest-client调用外部服务。 我需要以某种方式存根其余的客户端调用以发回预设的响应,因此我不必调用实际的HTTP调用。 我的申请代码是…… post ‘/user/create’ do user_name = params[:name] response = RestClient.post(‘http://localhost:1885/api/users/’, params.to_json, :content_type => :json, :accept => :json) if response.code […]

我如何告诉Sinatra它是什么环境(开发,测试,生产)?

(免责声明:在Heroku上部署Sinatra的新function。) 我见过http://www.sinatrarb.com/configuration.html它告诉我set :environment, :production 。 我的问题是,如何指定它:“在Heroku中,将环境设置为生产,否则保持测试/开发。” 此外,即使在设置了行set :environment, :production ,我认为它不起作用,因为当我尝试在本地设置应用程序时,它仍在运行(当我知道(或者我想我知道)它应该’因为我没有在我的电脑上安装postgres)。 的Gemfile group :production do gem ‘dm-postgres-adapter’ end group :development, :test do gem ‘dm-sqlite-adapter’, “~> 1.2.0” end