ruby - 如何在 Sinatra 项目中添加额外的路由?
问题描述
我怎样才能prepend
在另一个 gem 的 Sinatra 类中增加一条额外的路线?
我怎么样了
module StealthWebhook
module Route
post '/api/v1/webhook' do
response_status, response_body = Webhook.send_request(request)
status response_status
body response_body
end
end
end
module Stealth
class Server < Sinatra::Base
prepend StealthWebhook::Route
end
end
我需要在这个类中添加这条路线
但我收到此错误:
bundler: failed to load command: puma (/usr/local/bundle/bin/puma)
bot_1 | NoMethodError: undefined method `post' for StealthWebhook::Route:Module
bot_1 | /bot/lib/stealth-webhook/lib/stealth-webhook/route.rb:6:in `<module:Route>'
bot_1 | /bot/lib/stealth-webhook/lib/stealth-webhook/route.rb:4:in `<module:StealthWebhook>'
bot_1 | /bot/lib/stealth-webhook/lib/stealth-webhook/route.rb:3:in `<top (required)>'
bot_1 | /bot/lib/stealth-webhook/lib/stealth-webhook/base.rb:5:in `require_relative'
bot_1 | /bot/lib/stealth-webhook/lib/stealth-webhook/base.rb:5:in `<top (required)>'
bot_1 | /bot/lib/stealth-webhook/lib/stealth-webhook.rb:1:in `require_relative'
bot_1 | /bot/lib/stealth-webhook/lib/stealth-webhook.rb:1:in `<top (required)>'
bot_1 | /bot/lib/aggregator.rb:5:in `require_relative'
bot_1 | /bot/lib/aggregator.rb:5:in `<top (required)>'
bot_1 | /bot/config/initializers/overridings.rb:1:in `require_relative'
bot_1 | /bot/config/initializers/overridings.rb:1:in `<top (required)>'
bot_1 | /usr/local/bundle/gems/stealth-1.0.4/lib/stealth/base.rb:85:in `require_relative'
bot_1 | /usr/local/bundle/gems/stealth-1.0.4/lib/stealth/base.rb:85:in `block in require_directory'
bot_1 | /usr/local/bundle/gems/stealth-1.0.4/lib/stealth/base.rb:93:in `each'
bot_1 | /usr/local/bundle/gems/stealth-1.0.4/lib/stealth/base.rb:93:in `for_each_file_in'
bot_1 | /usr/local/bundle/gems/stealth-1.0.4/lib/stealth/base.rb:85:in `require_directory'
bot_1 | /usr/local/bundle/gems/stealth-1.0.4/lib/stealth/base.rb:66:in `load_environment'
bot_1 | /usr/local/bundle/gems/stealth-1.0.4/lib/stealth/base.rb:33:in `boot'
bot_1 | /bot/config/boot.rb:6:in `<top (required)>'
bot_1 | config.ru:2:in `require_relative'
bot_1 | config.ru:2:in `block in <main>'
bot_1 | /usr/local/bundle/gems/rack-2.0.5/lib/rack/builder.rb:55:in `instance_eval'
bot_1 | /usr/local/bundle/gems/rack-2.0.5/lib/rack/builder.rb:55:in `initialize'
bot_1 | config.ru:in `new'
bot_1 | config.ru:in `<main>'
bot_1 | /usr/local/bundle/gems/rack-2.0.5/lib/rack/builder.rb:49:in `eval'
bot_1 | /usr/local/bundle/gems/rack-2.0.5/lib/rack/builder.rb:49:in `new_from_string'
bot_1 | /usr/local/bundle/gems/rack-2.0.5/lib/rack/builder.rb:40:in `parse_file'
bot_1 | /usr/local/bundle/gems/puma-3.12.0/lib/puma/configuration.rb:318:in `load_rackup'
bot_1 | /usr/local/bundle/gems/puma-3.12.0/lib/puma/configuration.rb:243:in `app'
bot_1 | /usr/local/bundle/gems/puma-3.12.0/lib/puma/runner.rb:145:in `load_and_bind'
bot_1 | /usr/local/bundle/gems/puma-3.12.0/lib/puma/single.rb:96:in `run'
bot_1 | /usr/local/bundle/gems/puma-3.12.0/lib/puma/launcher.rb:184:in `run'
bot_1 | /usr/local/bundle/gems/puma-3.12.0/lib/puma/cli.rb:78:in `run'
bot_1 | /usr/local/bundle/gems/puma-3.12.0/bin/puma:10:in `<top (required)>'
bot_1 | /usr/local/bundle/bin/puma:29:in `load'
bot_1 | /usr/local/bundle/bin/puma:29:in `<top (required)>'
编辑:
我解决了使用Stealth::Server.post
. 这是最好的方法吗?
module StealthWebhook
module Route
Stealth::Server.post '/api/v1/webhook' do
response_status, response_body = Webhook.send_request(request)
status response_status
body response_body
end
end
end
module Stealth
class Server < Sinatra::Base
prepend StealthWebhook::Route
end
end
解决方案
推荐阅读
- html - 如何修复 MaterializeCSS 垂直对齐
- apache-spark - 将 Spark 作业从 Windows 机器提交到远程纱线集群(Unix)
- javascript - Document.body.removeChild() 仅在调试模式下删除模式
- logging - 在 JBoss 7 中使用 Filter-spec 从堆栈跟踪日志中过滤中间件帧
- ios - 用户从打开的网址返回后如何防止屏幕重建
- docker - 使用音量时如何排除子文件夹?
- javascript - 具有超链接的图像地图工具提示在 Internet Explorer 版本 11 中不起作用
- android - 如何在 RecyclerView 适配器中显示材质对话框?
- javascript - 仅当用户完成输入并等待 n 秒时,如何在自定义自动完成中触发 ajax 调用
- ios - Firebase 实时数据库,检查 firebase 数据库值是否存在?