rails_twirp 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/test.yml +21 -0
- data/.gitignore +1 -0
- data/.standard.yml +4 -0
- data/Gemfile +3 -3
- data/lib/rails_twirp.rb +1 -0
- data/lib/rails_twirp/base.rb +29 -10
- data/lib/rails_twirp/engine.rb +11 -6
- data/lib/rails_twirp/errors.rb +11 -0
- data/lib/rails_twirp/render_pb.rb +18 -0
- data/lib/rails_twirp/rescue.rb +14 -0
- data/lib/rails_twirp/route_set.rb +3 -0
- data/lib/rails_twirp/testing/integration_test.rb +6 -1
- data/lib/rails_twirp/version.rb +1 -1
- data/test/dummy/Rakefile +6 -0
- data/test/dummy/app/assets/config/manifest.js +2 -0
- data/test/dummy/app/assets/images/.keep +0 -0
- data/test/dummy/app/assets/stylesheets/application.css +15 -0
- data/test/dummy/app/channels/application_cable/channel.rb +4 -0
- data/test/dummy/app/channels/application_cable/connection.rb +4 -0
- data/test/dummy/app/controllers/application_controller.rb +2 -0
- data/test/dummy/app/controllers/application_twirp_controller.rb +7 -0
- data/test/dummy/app/controllers/concerns/.keep +0 -0
- data/test/dummy/app/controllers/pings_controller.rb +39 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/javascript/packs/application.js +15 -0
- data/test/dummy/app/jobs/application_job.rb +7 -0
- data/test/dummy/app/mailers/application_mailer.rb +4 -0
- data/test/dummy/app/models/application_record.rb +3 -0
- data/test/dummy/app/models/concerns/.keep +0 -0
- data/test/dummy/app/twirp/views/pings/ping_template.pb.pbbuilder +1 -0
- data/test/dummy/app/views/layouts/application.html.erb +15 -0
- data/test/dummy/app/views/layouts/mailer.html.erb +13 -0
- data/test/dummy/app/views/layouts/mailer.text.erb +1 -0
- data/test/dummy/bin/generate +3 -0
- data/test/dummy/bin/rails +4 -0
- data/test/dummy/bin/rake +4 -0
- data/test/dummy/bin/setup +33 -0
- data/test/dummy/config.ru +6 -0
- data/test/dummy/config/application.rb +23 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/cable.yml +10 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +76 -0
- data/test/dummy/config/environments/production.rb +120 -0
- data/test/dummy/config/environments/test.rb +59 -0
- data/test/dummy/config/initializers/application_controller_renderer.rb +8 -0
- data/test/dummy/config/initializers/assets.rb +12 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +8 -0
- data/test/dummy/config/initializers/content_security_policy.rb +28 -0
- data/test/dummy/config/initializers/cookies_serializer.rb +5 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +6 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +4 -0
- data/test/dummy/config/initializers/permissions_policy.rb +11 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +33 -0
- data/test/dummy/config/puma.rb +43 -0
- data/test/dummy/config/routes.rb +3 -0
- data/test/dummy/config/storage.yml +34 -0
- data/test/dummy/config/twirp/routes.rb +13 -0
- data/test/dummy/lib/assets/.keep +0 -0
- data/test/dummy/log/.keep +0 -0
- data/test/dummy/proto/api.proto +23 -0
- data/test/dummy/proto/api_pb.rb +22 -0
- data/test/dummy/proto/api_twirp.rb +23 -0
- data/test/dummy/public/404.html +67 -0
- data/test/dummy/public/422.html +67 -0
- data/test/dummy/public/500.html +66 -0
- data/test/dummy/public/apple-touch-icon-precomposed.png +0 -0
- data/test/dummy/public/apple-touch-icon.png +0 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/ping_controller_test.rb +55 -0
- data/test/test_helper.rb +10 -1
- metadata +127 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 78af7d754c6fbaf90cbf22f5ef83b116c54840ab97fc2daffb09a5a37bcd7c91
|
4
|
+
data.tar.gz: '0078e09678ea09ccda0d2db94c1ff241222cf3074f4febd580197efc9d257cd1'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fd943fdaa9b54614c268b6d7471bec3726bffb2125f5afa6992b9b30db2c03bbc2027869e1b23f449addf81cd2140d3abe97677f35d6a5ba924be515b01998b4
|
7
|
+
data.tar.gz: ab7b4e53cdfc9eb84a6f4875674b643c668adc15f103cf60ba367078393e23257477b13a0eb5355e69248eb1250feb73a37eb5e689a7a501cd9236c4630a600a
|
@@ -0,0 +1,21 @@
|
|
1
|
+
name: Ruby Test
|
2
|
+
on: push
|
3
|
+
|
4
|
+
jobs:
|
5
|
+
test:
|
6
|
+
runs-on: ubuntu-latest
|
7
|
+
steps:
|
8
|
+
- name: Checkout code
|
9
|
+
uses: actions/checkout@v2
|
10
|
+
|
11
|
+
- name: Setup Ruby
|
12
|
+
uses: ruby/setup-ruby@v1
|
13
|
+
with:
|
14
|
+
ruby-version: 3.0.0
|
15
|
+
bundler-cache: true
|
16
|
+
|
17
|
+
- name: Run tests
|
18
|
+
run: bin/test
|
19
|
+
|
20
|
+
- name: Run standardrb
|
21
|
+
run: bundle exec standardrb
|
data/.gitignore
CHANGED
data/.standard.yml
ADDED
data/Gemfile
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
source "https://rubygems.org"
|
2
|
-
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
|
3
2
|
|
4
3
|
# Specify your gem's dependencies in rails_twirp.gemspec.
|
5
4
|
gemspec
|
6
5
|
|
7
|
-
|
8
|
-
|
6
|
+
gem "sqlite3"
|
7
|
+
gem "pbbuilder", "~> 0.3.0"
|
8
|
+
gem "standard"
|
data/lib/rails_twirp.rb
CHANGED
data/lib/rails_twirp/base.rb
CHANGED
@@ -1,13 +1,33 @@
|
|
1
|
+
require "abstract_controller/base"
|
2
|
+
require "abstract_controller/rendering"
|
3
|
+
require "action_view/rendering"
|
4
|
+
require "rails_twirp/render_pb"
|
5
|
+
require "rails_twirp/errors"
|
6
|
+
require "abstract_controller/asset_paths"
|
7
|
+
require "abstract_controller/caching"
|
8
|
+
require "abstract_controller/logger"
|
9
|
+
require "abstract_controller/callbacks"
|
10
|
+
require "rails_twirp/rescue"
|
11
|
+
|
1
12
|
module RailsTwirp
|
2
13
|
class Base < AbstractController::Base
|
3
14
|
abstract!
|
4
15
|
|
5
|
-
|
6
|
-
|
7
|
-
include AbstractController::Callbacks
|
8
|
-
include AbstractController::Caching
|
16
|
+
# The order of these includes matter.
|
17
|
+
# The rendering modules extend each other, so need to be in this order.
|
9
18
|
include AbstractController::Rendering
|
10
19
|
include ActionView::Rendering
|
20
|
+
include RenderPb
|
21
|
+
include Errors
|
22
|
+
|
23
|
+
# These add helpers for the controller
|
24
|
+
include AbstractController::AssetPaths
|
25
|
+
include AbstractController::Caching
|
26
|
+
include AbstractController::Logger
|
27
|
+
|
28
|
+
# These need to be last so errors can be handled as early as possible.
|
29
|
+
include AbstractController::Callbacks
|
30
|
+
include Rescue
|
11
31
|
|
12
32
|
attr_internal :request, :env, :response_class
|
13
33
|
def initialize
|
@@ -35,16 +55,15 @@ module RailsTwirp
|
|
35
55
|
response_body
|
36
56
|
end
|
37
57
|
|
38
|
-
def render(*args)
|
39
|
-
options = {formats: :pb, handlers: :pbbuilder, locals: {response_class: response_class}}
|
40
|
-
options.deep_merge! args.extract_options!
|
41
|
-
super(*args, options)
|
42
|
-
end
|
43
|
-
|
44
58
|
def self.dispatch(action, request, response_class, env = {})
|
45
59
|
new.dispatch(action, request, response_class, env)
|
46
60
|
end
|
47
61
|
|
62
|
+
# Used by the template renderer to figure out which template to use
|
63
|
+
def details_for_lookup
|
64
|
+
{formats: [:pb], handlers: [:pbbuilder]}
|
65
|
+
end
|
66
|
+
|
48
67
|
ActiveSupport.run_load_hooks(:rails_twirp, self)
|
49
68
|
end
|
50
69
|
end
|
data/lib/rails_twirp/engine.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
require "rails/railtie"
|
2
1
|
require "rails_twirp/application"
|
2
|
+
require "rails_twirp/route_set"
|
3
|
+
require "rails/railtie"
|
3
4
|
|
4
5
|
module RailsTwirp
|
5
6
|
# Even though this is an engine, we don't inherit from Rails::Engine because we don't want anything it provides.
|
@@ -26,8 +27,11 @@ module RailsTwirp
|
|
26
27
|
app.config.paths.add "app/twirp/views", load_path: true
|
27
28
|
end
|
28
29
|
|
29
|
-
initializer :set_controller_view_path do
|
30
|
-
|
30
|
+
initializer :set_controller_view_path do |app|
|
31
|
+
views = app.config.paths["app/twirp/views"].existent
|
32
|
+
unless views.empty?
|
33
|
+
ActiveSupport.on_load(:rails_twirp) { prepend_view_path views }
|
34
|
+
end
|
31
35
|
end
|
32
36
|
|
33
37
|
initializer :add_twirp do |app|
|
@@ -37,11 +41,12 @@ module RailsTwirp
|
|
37
41
|
|
38
42
|
initializer :load_twirp_routes do |app|
|
39
43
|
# Load route files
|
40
|
-
|
44
|
+
[
|
41
45
|
*app.config.paths["config/twirp/routes.rb"].existent,
|
42
46
|
*app.config.paths["config/twirp/routes"].existent
|
43
|
-
]
|
44
|
-
|
47
|
+
].each do |path|
|
48
|
+
load path
|
49
|
+
end
|
45
50
|
|
46
51
|
# Create a router that knows how to route all the registered services
|
47
52
|
services = app.twirp.routes.to_services
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require "twirp/error"
|
2
|
+
|
3
|
+
module RailsTwirp
|
4
|
+
module Errors
|
5
|
+
# Helper that sets the response to a Twirp Error
|
6
|
+
# The valid error codes can be found in Twirp::ERROR_CODES
|
7
|
+
def error(code, message, meta = nil)
|
8
|
+
self.response_body = Twirp::Error.new(code, message, meta)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module RailsTwirp
|
2
|
+
# RenderPb makes it possible to do 'render pb: <proto object>', skipping templates
|
3
|
+
# The way this module is written is inspired by ActionController::Renderers
|
4
|
+
module RenderPb
|
5
|
+
def render_to_body(options)
|
6
|
+
_render_to_body_with_pb(options) || super
|
7
|
+
end
|
8
|
+
|
9
|
+
def _render_to_body_with_pb(options)
|
10
|
+
if options.include? :pb
|
11
|
+
_process_options(options)
|
12
|
+
return options[:pb]
|
13
|
+
end
|
14
|
+
|
15
|
+
nil
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -1,6 +1,8 @@
|
|
1
1
|
# Most of this logic is stolen from Rails ActionDispatch::Routing::RouteSet
|
2
2
|
|
3
3
|
module RailsTwirp
|
4
|
+
class UnknownRpcError < StandardError; end
|
5
|
+
|
4
6
|
class RouteSet
|
5
7
|
attr_reader :services
|
6
8
|
|
@@ -42,6 +44,7 @@ module RailsTwirp
|
|
42
44
|
handler = Class.new
|
43
45
|
@rpcs.each do |name, mapping|
|
44
46
|
rpc_info = @service_class.rpcs[name]
|
47
|
+
raise UnknownRpcError, "Unknown RPC #{name} on #{@service_class.service_name} service" unless rpc_info
|
45
48
|
method_name = rpc_info[:ruby_method]
|
46
49
|
|
47
50
|
# Stolen from Rails in ActionDispatch::Request#controller_class_for
|
@@ -30,7 +30,12 @@ module RailsTwirp
|
|
30
30
|
hook.call(env)
|
31
31
|
end
|
32
32
|
|
33
|
-
response =
|
33
|
+
response = begin
|
34
|
+
service.call_rpc rpc, request, env
|
35
|
+
rescue => e
|
36
|
+
Twirp::Error.internal_with(e)
|
37
|
+
end
|
38
|
+
|
34
39
|
@response = response
|
35
40
|
@controller = http_request.controller_instance
|
36
41
|
response
|
data/lib/rails_twirp/version.rb
CHANGED
data/test/dummy/Rakefile
ADDED
File without changes
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
|
10
|
+
* files in this directory. Styles in this file should be added after the last require_* statement.
|
11
|
+
* It is generally better to create a new file per style scope.
|
12
|
+
*
|
13
|
+
*= require_tree .
|
14
|
+
*= require_self
|
15
|
+
*/
|
File without changes
|
@@ -0,0 +1,39 @@
|
|
1
|
+
class PingsController < ApplicationTwirpController
|
2
|
+
before_action :respond_error, only: :before_error
|
3
|
+
|
4
|
+
def ping
|
5
|
+
response = RPC::DummyAPI::PingResponse.new(double_name: request.name * 2)
|
6
|
+
self.response_body = response
|
7
|
+
end
|
8
|
+
|
9
|
+
def ping_render
|
10
|
+
response = RPC::DummyAPI::PingResponse.new(double_name: request.name * 2)
|
11
|
+
render pb: response
|
12
|
+
end
|
13
|
+
|
14
|
+
def ping_template
|
15
|
+
@double_name = request.name * 2
|
16
|
+
end
|
17
|
+
|
18
|
+
def error_response
|
19
|
+
error :unauthenticated, "You are not authenticated!!"
|
20
|
+
end
|
21
|
+
|
22
|
+
def raise_error
|
23
|
+
# This error is rescued in ApplicationTwirpController
|
24
|
+
raise ActiveRecord::RecordNotFound, "Not found"
|
25
|
+
end
|
26
|
+
|
27
|
+
def uncaught_raise
|
28
|
+
raise StandardError, "Uncaught"
|
29
|
+
end
|
30
|
+
|
31
|
+
def before_error
|
32
|
+
# This error won't be reached because of the before_action
|
33
|
+
raise NotImplementedError
|
34
|
+
end
|
35
|
+
|
36
|
+
def respond_error
|
37
|
+
error :malformed, "yOuR ReQuEsT Is mAlFoRmEd"
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file. JavaScript code in this file should be added after the last require_* statement.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require rails-ujs
|
14
|
+
//= require activestorage
|
15
|
+
//= require_tree .
|
@@ -0,0 +1,7 @@
|
|
1
|
+
class ApplicationJob < ActiveJob::Base
|
2
|
+
# Automatically retry jobs that encountered a deadlock
|
3
|
+
# retry_on ActiveRecord::Deadlocked
|
4
|
+
|
5
|
+
# Most jobs are safe to ignore if the underlying records are no longer available
|
6
|
+
# discard_on ActiveJob::DeserializationError
|
7
|
+
end
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
pb.double_name @double_name
|
@@ -0,0 +1,15 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Dummy</title>
|
5
|
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
6
|
+
<%= csrf_meta_tags %>
|
7
|
+
<%= csp_meta_tag %>
|
8
|
+
|
9
|
+
<%= stylesheet_link_tag 'application', media: 'all' %>
|
10
|
+
</head>
|
11
|
+
|
12
|
+
<body>
|
13
|
+
<%= yield %>
|
14
|
+
</body>
|
15
|
+
</html>
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= yield %>
|