标签: sinatra

删除 Sinatra 应用程序中的默认路由日志记录

我有一个带有 /health/api 端点的 Sintra 应用程序,如下所述,我的负载均衡器每秒调用多次。我想仅删除此路由的默认日志记录,或者相反,仅将我关心的端点打印到日志中。

get '/health/api' do
  # Health Check
  'I keep quiet'
end

get '/members' do
  # get members data
  'This request gets logged'
end
Run Code Online (Sandbox Code Playgroud)

ruby logging sinatra

3
推荐指数
1
解决办法
1591
查看次数

我可以用密码保护 Sinatra 应用程序不受路线影响吗?

我正在我的路线中安装 Sinatra 应用程序。我想为它提供 http 基本身份验证。我可以从路由文件中做到这一点吗?

mount My::App, :at => "/mysin"
Run Code Online (Sandbox Code Playgroud)

我知道我可以检查对 auth 标头的请求,但我不知道如何向用户提供 auth 对话框。通常,从控制器中,我会这样做authenticate_or_request_with_http_basic,但是由于我不在控制器的上下文中,因此这不起作用。任何解决方法?

routing ruby-on-rails sinatra

3
推荐指数
1
解决办法
280
查看次数

RSpec 规格未运行?

我在 Sinatra 应用程序上运行某些 rspec 规范时遇到问题。我只是在学习 Ruby,所以不幸的是,我不知道如何解决我用一本书(Paul Dix 着的《Ruby on Rails 中的服务导向设计》)中的代码遇到的问题。

问题是这些测试并没有真正运行(我认为)。在 app 目录中,我运行$ ruby spec/service_spec.rb,我没有得到测试通过或失败的迹象。我从文件中收到几条日志消息/service.rb,但这就是显示的所有输出。我输入了一些puts调用来显示 before 块和 it 块没有被执行(即使描述块是)。

如果您想了解有关代码的更多信息,请查看Github 上的存储库

/spec/service_spec.rb:

ENV['SINATRA_ENV'] = 'test'

require_relative '../service'
require 'rspec'
require 'rack/test'

RSpec.configure do |conf|
  conf.include Rack::Test::Methods
end

def app
  Sinatra::Application
end

describe "service" do
  puts 'describe 1'
  before(:each) do
    puts 'before 1'
    User.delete_all
  end

  describe "GET on /api/v1/users/:id" do
    puts 'describe 2'
    before(:each) do
      puts 'before 2'
      User.create(
        :name => "paul", …
Run Code Online (Sandbox Code Playgroud)

ruby rspec sinatra

3
推荐指数
1
解决办法
7483
查看次数

如何每 x 秒调用一次方法?

是否有一个简单的库可以让我每隔 x 秒指定一个非常简单的任务来安排一个非常简单的任务?

我正在寻找以下方面的东西:

Something.after(5) { call_method }
Run Code Online (Sandbox Code Playgroud)

很简单。我是否必须安装像 resque 这样的库,或者是否有可以处理此问题的库?我不是在寻找完整的后台队列处理,只是在 x 秒后调用一个简单的方法。我知道 resque 和 sidekiq ......

我需要从 Rails 或 Sinatra 运行它。

ruby ruby-on-rails sinatra

3
推荐指数
1
解决办法
2441
查看次数

Ruby Hash:无法将String转换为Integer TypeError

目前收到Ruby Hash: can't convert String into Integer错误.代码在edit_id行上失败.

我已经尝试过许多不同的解决方案,这些解决方案来自SE上已发布的类似问题,但不幸的是,它们都没有奏效

哈希:

{"downloadID"=>115, "PageID"=>nil, "title"=>"hi", "dlLink"=>"http://www.a.com", "imgSrc"=>"http://www.a.com", "caption"=>"aaaa", "dlLive"=>nil, "createdAt"=>nil, "user_id"=>7}
Run Code Online (Sandbox Code Playgroud)

码:

#edit download
put '/view1/downloadedit' do
  data = JSON.parse(request.body.read)
  puts data
  edit_id = data["downloadID"]
  puts edit_id
  @download = Download.get(:download_id => edit_id)
  puts data
  if @download.update(data)
    status 201
    puts 'edit saved okay'
  else
    status 201
    puts 'edit failed to SAVE'
  end
end
Run Code Online (Sandbox Code Playgroud)

ruby hash sinatra ruby-datamapper

3
推荐指数
1
解决办法
5547
查看次数

数据库配置未指定适配器(Sinatra + Heroku + Activerecord)

我收到一个错误(在Sinatra + ActiveRecord Heroku上) Database configuration does not specify adapter.

从一些研究来看,这看起来是因为Heroku期望在机架期间不会使用环境变量.

我从列出数据库URL的db环境文件中删除了env变量,但我仍然遇到相同的错误.

编辑:这也只是前一天使用db文件不变,所以我不知道什么是错的.

2015-09-01T02:44:40.980448+00:00 app[web.1]: I, [2015-09-01T02:44:40.980313 #3]  INFO -- : Refreshing Gem list
2015-09-01T02:44:41.459544+00:00 app[web.1]: /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/connection_specification.rb:171:in `spec': database configuration does not specify adapter (ActiveRecord::AdapterNotSpecified)
2015-09-01T02:44:41.459548+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.4/lib/active_record/connection_handling.rb:50:in `establish_connection'
2015-09-01T02:44:41.459550+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.0.0/gems/sinatra-activerecord-2.0.8/lib/sinatra/activerecord.rb:43:in `database='
2015-09-01T02:44:41.459552+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.0.0/gems/sinatra-1.4.6/lib/sinatra/base.rb:1216:in `set'
2015-09-01T02:44:41.459554+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.0.0/gems/sinatra-activerecord-2.0.8/lib/sinatra/activerecord.rb:35:in `database_file='
2015-09-01T02:44:41.459555+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.0.0/gems/sinatra-1.4.6/lib/sinatra/base.rb:1216:in `set'
2015-09-01T02:44:41.459556+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.0.0/gems/sinatra-activerecord-2.0.8/lib/sinatra/activerecord.rb:19:in `registered'
2015-09-01T02:44:41.459560+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.0.0/gems/sinatra-1.4.6/lib/sinatra/base.rb:1399:in `block in register'
2015-09-01T02:44:41.459562+00:00 app[web.1]:    from /app/vendor/bundle/ruby/2.0.0/gems/sinatra-1.4.6/lib/sinatra/base.rb:1397:in `each'
2015-09-01T02:44:41.459563+00:00 app[web.1]: …
Run Code Online (Sandbox Code Playgroud)

activerecord heroku sinatra

3
推荐指数
1
解决办法
1356
查看次数

Rack::Reloader 未获取更改

考虑这个config.ru文件:

require 'sinatra'

use Rack::Reloader, 0

get '/' do
  'hi'
end

run Sinatra::Application
Run Code Online (Sandbox Code Playgroud)

我从命令行启动它thin starthi当我点击 时,应用程序会运行并显示localhost:3000。如果我现在更改hihello,保存文件并重新加载页面,则不会出现更改:页面仍然显示hi

为什么Rack::Reloader在这种情况下不起作用?我可以更改任何内容以使其正常工作吗?

rack sinatra

3
推荐指数
1
解决办法
890
查看次数

如何使用 Sinatra gem 和 Ruby 隐藏 API 密钥

我是一名初级程序员,目前正在练习 Sinatra gem。

我的项目之一是一个简单的天气应用程序,它可以告诉用户不同城市的天气。现在我想隐藏我一直在使用的 API 密钥,因为我想将此网站上传到 Heroku。我该如何去做呢?我知道我需要一个 config.yml 文件。这要去哪里?是否需要进入某个文件夹?

就像将其粘贴到 config.yml 文件中一样简单吗?

    weather_api_key: *api key here*
Run Code Online (Sandbox Code Playgroud)

我需要在 server.rb 文件中添加任何其他命令吗?有人可以在这里给我任何指示吗?

ruby api rubygems environment-variables sinatra

3
推荐指数
1
解决办法
891
查看次数

Docker 构建无法在 Sinatra 项目中安装 pg gem

我已经查看了 SO 上所有安装 Postgres gem 时出现类似错误的帖子pg

我的问题很独特:我可以通过 Gemfile 成功地将 Postgres 添加到我的 Sinatra Ruby 项目中,bundle install但是在构建 Docker 映像时,它安装失败pg,并出现以下错误:

ERROR:  Error installing pg:
ERROR: Failed to build gem native extension.

current directory: /usr/local/bundle/gems/pg-0.19.0/ext
/usr/local/bin/ruby -r ./siteconf20170113-7-ottv1k.rb extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
  --with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Run Code Online (Sandbox Code Playgroud)

我已经尝试了类似答案中建议的所有内容:

  • Brew 安装/卸载,
  • gem install / uninstall 指定两个文件的路径:pg_config和 …

postgresql sinatra bundler pg docker

3
推荐指数
1
解决办法
3575
查看次数

如何使用 rack::test 发送自定义标头?

我花了大约两天时间尝试使用 Rack::Test 发送自定义标头,但没有成功。我只是无法将任何标头发送到我的应用程序中。我在网上找到了很多类似代码的例子——方法(地址、正文、标题),但对我来说它们根本不起作用。

ruby 2.5.1p57 (2018-03-29 修订版 63029) [x86_64-linux]

# grep rack  Gemfile.lock
    rack (2.0.4)
    rack-protection (2.0.1)
      rack
    rack-test (1.0.0)
      rack (>= 1.0, < 3)
      rack-protection (>= 1.5.0)
      rack (~> 2.0)
      rack-protection (= 2.0.1)
  rack-test
Run Code Online (Sandbox Code Playgroud)

应用程序中的代码(sinatra):

$log = Logger.new STDERR
class MyApi < Sinatra::Application
  before do
    $log.debug{ "Headers: #{ headers.keys.inspect }" }
  end
  get '/offers' do
    #... some code
  end
  post '/offers' do
    # .. some another code
  end
end
Run Code Online (Sandbox Code Playgroud)

规格/api_spec.rb

RSpec.describe MyApi, '/offers' do
  include Rack::Test::Methods
  def app
    MyApi …
Run Code Online (Sandbox Code Playgroud)

ruby rspec sinatra rack-test

3
推荐指数
1
解决办法
1695
查看次数