rails_twirp 0.13.1 → 0.14
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/CHANGELOG.md +6 -0
- data/Gemfile +2 -0
- data/Rakefile +2 -0
- data/bin/test +5 -0
- data/lib/commands/twirp/routes_command.rb +2 -0
- data/lib/rails_twirp/application.rb +2 -0
- data/lib/rails_twirp/engine.rb +2 -0
- data/lib/rails_twirp/errors.rb +2 -0
- data/lib/rails_twirp/exception_handling.rb +2 -0
- data/lib/rails_twirp/implicit_render.rb +2 -0
- data/lib/rails_twirp/instrumentation.rb +2 -0
- data/lib/rails_twirp/log_subscriber.rb +2 -0
- data/lib/rails_twirp/mapper.rb +2 -0
- data/lib/rails_twirp/render_pb.rb +2 -0
- data/lib/rails_twirp/rescue.rb +2 -0
- data/lib/rails_twirp/route_set.rb +2 -0
- data/lib/rails_twirp/testing/integration_test.rb +108 -0
- data/lib/rails_twirp/url_for.rb +2 -0
- data/lib/rails_twirp/version.rb +3 -1
- data/lib/rails_twirp.rb +2 -0
- data/rails_twirp.gemspec +3 -1
- data/test/dummy/Rakefile +8 -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 +6 -0
- data/test/dummy/app/channels/application_cable/connection.rb +6 -0
- data/test/dummy/app/controllers/application_controller.rb +4 -0
- data/test/dummy/app/controllers/application_twirp_controller.rb +9 -0
- data/test/dummy/app/controllers/concerns/.keep +0 -0
- data/test/dummy/app/controllers/dummy_controller.rb +7 -0
- data/test/dummy/app/controllers/pings_controller.rb +42 -0
- data/test/dummy/app/controllers/testmod/nested/other_controller.rb +12 -0
- data/test/dummy/app/helpers/application_helper.rb +4 -0
- data/test/dummy/app/helpers/random_helper.rb +7 -0
- data/test/dummy/app/javascript/packs/application.js +15 -0
- data/test/dummy/app/jobs/application_job.rb +9 -0
- data/test/dummy/app/mailers/application_mailer.rb +6 -0
- data/test/dummy/app/models/application_record.rb +5 -0
- data/test/dummy/app/models/concerns/.keep +0 -0
- data/test/dummy/app/views/dummy/rpc_name_check.pb.pbbuilder +3 -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/app/views/pings/ping_template.pb.pbbuilder +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/application.rb +25 -0
- data/test/dummy/config/boot.rb +7 -0
- data/test/dummy/config/cable.yml +10 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +7 -0
- data/test/dummy/config/environments/development.rb +78 -0
- data/test/dummy/config/environments/production.rb +122 -0
- data/test/dummy/config/environments/test.rb +61 -0
- data/test/dummy/config/initializers/application_controller_renderer.rb +10 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +10 -0
- data/test/dummy/config/initializers/content_security_policy.rb +30 -0
- data/test/dummy/config/initializers/cookies_serializer.rb +7 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +8 -0
- data/test/dummy/config/initializers/inflections.rb +18 -0
- data/test/dummy/config/initializers/mime_types.rb +6 -0
- data/test/dummy/config/initializers/permissions_policy.rb +13 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +16 -0
- data/test/dummy/config/locales/en.yml +33 -0
- data/test/dummy/config/puma.rb +45 -0
- data/test/dummy/config/routes.rb +5 -0
- data/test/dummy/config/storage.yml +34 -0
- data/test/dummy/config/twirp/routes.rb +24 -0
- data/test/dummy/config.ru +8 -0
- data/test/dummy/lib/assets/.keep +0 -0
- data/test/dummy/log/.keep +0 -0
- data/test/dummy/proto/api.proto +30 -0
- data/test/dummy/proto/api_pb.rb +31 -0
- data/test/dummy/proto/api_twirp.rb +27 -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/dummy_test.rb +12 -0
- data/test/other_controller_test.rb +11 -0
- data/test/ping_controller_test.rb +116 -0
- data/test/rails_twirp_test.rb +9 -0
- data/test/test_helper.rb +18 -0
- metadata +72 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9b7e742d4821b2a3745b2463d19aae223822eac419bd773d371f379143454d71
|
4
|
+
data.tar.gz: 1abf4446ebeea434ecdd7064a19e5ad7375c0e3e8f936af4afaf67a938923844
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6dfdf309ddc0f01d4da60b51205de47414fc11aff123a15f1e65fdbbd31d532c9c417fe7e73f5e5a4a9c0fb0d9f030c6a758bae1fc4a40df4f776167a64a2c84
|
7
|
+
data.tar.gz: 74840175fc2b2bac370dcddb5c906f89cebf072c7b5f678baca72f4c678d45c87ff15f0cfbe62c9e7d15ac3b8904eedc99ffd4d90fdb632439c18d575ad40a95
|
@@ -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@v3
|
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/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/Rakefile
CHANGED
data/bin/test
ADDED
data/lib/rails_twirp/engine.rb
CHANGED
data/lib/rails_twirp/errors.rb
CHANGED
data/lib/rails_twirp/mapper.rb
CHANGED
data/lib/rails_twirp/rescue.rb
CHANGED
@@ -0,0 +1,108 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "twirp/encoding"
|
4
|
+
|
5
|
+
module RailsTwirp
|
6
|
+
class IntegrationTest < ActiveSupport::TestCase
|
7
|
+
DEFAULT_HOST = "www.example.com"
|
8
|
+
Response = Struct.new(:status, :body, :headers)
|
9
|
+
|
10
|
+
attr_reader :response, :request, :controller
|
11
|
+
attr_writer :mount_path
|
12
|
+
alias_method :mount_path!, :mount_path=
|
13
|
+
|
14
|
+
def initialize(name)
|
15
|
+
super
|
16
|
+
reset!
|
17
|
+
@before_rpc = []
|
18
|
+
end
|
19
|
+
|
20
|
+
def host
|
21
|
+
@host || DEFAULT_HOST
|
22
|
+
end
|
23
|
+
attr_writer :host
|
24
|
+
alias_method :host!, :host=
|
25
|
+
|
26
|
+
def https?
|
27
|
+
@https
|
28
|
+
end
|
29
|
+
|
30
|
+
def https!(value = true)
|
31
|
+
@https = value
|
32
|
+
end
|
33
|
+
|
34
|
+
def reset!
|
35
|
+
@request = nil
|
36
|
+
@response = nil
|
37
|
+
@host = nil
|
38
|
+
@host = nil
|
39
|
+
@https = false
|
40
|
+
@mount_path = "/twirp"
|
41
|
+
end
|
42
|
+
|
43
|
+
def before_rpc(&block)
|
44
|
+
@before_rpc << block
|
45
|
+
end
|
46
|
+
|
47
|
+
def rpc(service, rpc, request, headers: nil)
|
48
|
+
@request = request
|
49
|
+
|
50
|
+
env = build_rack_env(service, rpc, request, headers)
|
51
|
+
@before_rpc.each do |hook|
|
52
|
+
hook.call(env)
|
53
|
+
end
|
54
|
+
|
55
|
+
status, headers, body = app.call(env)
|
56
|
+
@response = decode_rack_response(service, rpc, status, headers, body)
|
57
|
+
set_controller_from_rack_env(env)
|
58
|
+
|
59
|
+
@response
|
60
|
+
end
|
61
|
+
|
62
|
+
def app
|
63
|
+
RailsTwirp.test_app
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def build_rack_env(service, rpc, request, headers)
|
69
|
+
env = {
|
70
|
+
"CONTENT_TYPE" => request_content_type,
|
71
|
+
"HTTPS" => https? ? "on" : "off",
|
72
|
+
"HTTP_HOST" => host,
|
73
|
+
"PATH_INFO" => "#{@mount_path}/#{service.service_full_name}/#{rpc}",
|
74
|
+
"REQUEST_METHOD" => "POST",
|
75
|
+
"SERVER_NAME" => host,
|
76
|
+
"SERVER_PORT" => https? ? "443" : "80",
|
77
|
+
"rack.url_scheme" => https? ? "https" : "http"
|
78
|
+
}
|
79
|
+
if headers.present?
|
80
|
+
http_request = ActionDispatch::Request.new(env)
|
81
|
+
http_request.headers.merge! headers
|
82
|
+
end
|
83
|
+
|
84
|
+
input_class = service.rpcs[rpc][:input_class]
|
85
|
+
env["rack.input"] = StringIO.new(Twirp::Encoding.encode(request, input_class, request_content_type))
|
86
|
+
env
|
87
|
+
end
|
88
|
+
|
89
|
+
def request_content_type
|
90
|
+
Twirp::Encoding::PROTO
|
91
|
+
end
|
92
|
+
|
93
|
+
def decode_rack_response(service, rpc, status, headers, body)
|
94
|
+
body = body.join # body is an Enumerable
|
95
|
+
|
96
|
+
if status === 200
|
97
|
+
output_class = service.rpcs[rpc][:output_class]
|
98
|
+
Twirp::Encoding.decode(body, output_class, headers["Content-Type"])
|
99
|
+
else
|
100
|
+
Twirp::Client.error_from_response(Response.new(status, body, headers))
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def set_controller_from_rack_env(env)
|
105
|
+
@controller = ActionDispatch::Request.new(env).controller_instance
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
data/lib/rails_twirp/url_for.rb
CHANGED
data/lib/rails_twirp/version.rb
CHANGED
data/lib/rails_twirp.rb
CHANGED
data/rails_twirp.gemspec
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require_relative "lib/rails_twirp/version"
|
2
4
|
|
3
5
|
Gem::Specification.new do |spec|
|
@@ -9,7 +11,7 @@ Gem::Specification.new do |spec|
|
|
9
11
|
spec.summary = "Integrate Twirp into Rails"
|
10
12
|
spec.license = "MIT"
|
11
13
|
|
12
|
-
spec.files = `git ls-files
|
14
|
+
spec.files = `git ls-files`.split("\n")
|
13
15
|
|
14
16
|
spec.add_dependency "rails", ">= 6.1.3"
|
15
17
|
spec.add_dependency "twirp", ">= 1.9", "< 1.11"
|
data/test/dummy/Rakefile
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Add your own tasks in files placed in lib/tasks ending in .rake,
|
4
|
+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
|
5
|
+
|
6
|
+
require_relative "config/application"
|
7
|
+
|
8
|
+
Rails.application.load_tasks
|
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,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class PingsController < ApplicationTwirpController
|
4
|
+
before_action :respond_error, only: :before_error
|
5
|
+
|
6
|
+
def ping
|
7
|
+
response = RPC::DummyAPI::PingResponse.new(double_name: request.name * 2)
|
8
|
+
self.response_body = response
|
9
|
+
end
|
10
|
+
|
11
|
+
def ping_render
|
12
|
+
url = rails_twirp_engine_url
|
13
|
+
response = RPC::DummyAPI::PingResponse.new(double_name: "#{url} #{helpers.does_this_work(request.name)}")
|
14
|
+
render pb: response
|
15
|
+
end
|
16
|
+
|
17
|
+
def ping_template
|
18
|
+
@double_name = request.name * 2
|
19
|
+
end
|
20
|
+
|
21
|
+
def error_response
|
22
|
+
error :unauthenticated, "You are not authenticated!!"
|
23
|
+
end
|
24
|
+
|
25
|
+
def raise_error
|
26
|
+
# This error is rescued in ApplicationTwirpController
|
27
|
+
raise ActiveRecord::RecordNotFound, "Not found"
|
28
|
+
end
|
29
|
+
|
30
|
+
def uncaught_raise
|
31
|
+
raise StandardError, "Uncaught"
|
32
|
+
end
|
33
|
+
|
34
|
+
def before_error
|
35
|
+
# This error won't be reached because of the before_action
|
36
|
+
raise NotImplementedError
|
37
|
+
end
|
38
|
+
|
39
|
+
def respond_error
|
40
|
+
error :malformed, "yOuR ReQuEsT Is mAlFoRmEd"
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Testmod
|
4
|
+
module Nested
|
5
|
+
class OtherController < ApplicationTwirpController
|
6
|
+
def ping
|
7
|
+
response = RPC::DummyAPI::PingResponse.new(double_name: request.name * 2)
|
8
|
+
self.response_body = response
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
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,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class ApplicationJob < ActiveJob::Base
|
4
|
+
# Automatically retry jobs that encountered a deadlock
|
5
|
+
# retry_on ActiveRecord::Deadlocked
|
6
|
+
|
7
|
+
# Most jobs are safe to ignore if the underlying records are no longer available
|
8
|
+
# discard_on ActiveJob::DeserializationError
|
9
|
+
end
|
File without changes
|
@@ -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 %>
|
@@ -0,0 +1 @@
|
|
1
|
+
pb.double_name @double_name
|
data/test/dummy/bin/rake
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require "fileutils"
|
3
|
+
|
4
|
+
# path to your application root.
|
5
|
+
APP_ROOT = File.expand_path("..", __dir__)
|
6
|
+
|
7
|
+
def system!(*args)
|
8
|
+
system(*args) || abort("\n== Command #{args} failed ==")
|
9
|
+
end
|
10
|
+
|
11
|
+
FileUtils.chdir APP_ROOT do
|
12
|
+
# This script is a way to set up or update your development environment automatically.
|
13
|
+
# This script is idempotent, so that you can run it at any time and get an expectable outcome.
|
14
|
+
# Add necessary setup steps to this file.
|
15
|
+
|
16
|
+
puts "== Installing dependencies =="
|
17
|
+
system! "gem install bundler --conservative"
|
18
|
+
system("bundle check") || system!("bundle install")
|
19
|
+
|
20
|
+
# puts "\n== Copying sample files =="
|
21
|
+
# unless File.exist?('config/database.yml')
|
22
|
+
# FileUtils.cp 'config/database.yml.sample', 'config/database.yml'
|
23
|
+
# end
|
24
|
+
|
25
|
+
puts "\n== Preparing database =="
|
26
|
+
system! "bin/rails db:prepare"
|
27
|
+
|
28
|
+
puts "\n== Removing old logs and tempfiles =="
|
29
|
+
system! "bin/rails log:clear tmp:clear"
|
30
|
+
|
31
|
+
puts "\n== Restarting application server =="
|
32
|
+
system! "bin/rails restart"
|
33
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "boot"
|
4
|
+
|
5
|
+
require "rails/all"
|
6
|
+
|
7
|
+
require "rails_twirp/engine"
|
8
|
+
|
9
|
+
# Require the gems listed in Gemfile, including any gems
|
10
|
+
# you've limited to :test, :development, or :production.
|
11
|
+
Bundler.require(*Rails.groups)
|
12
|
+
|
13
|
+
module Dummy
|
14
|
+
class Application < Rails::Application
|
15
|
+
config.load_defaults Rails::VERSION::STRING.to_f
|
16
|
+
|
17
|
+
# Configuration for the application, engines, and railties goes here.
|
18
|
+
#
|
19
|
+
# These settings can be overridden in specific environments using the files
|
20
|
+
# in config/environments, which are processed later.
|
21
|
+
#
|
22
|
+
# config.time_zone = "Central Time (US & Canada)"
|
23
|
+
# config.eager_load_paths << Rails.root.join("extras")
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Set up gems listed in the Gemfile.
|
4
|
+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../../Gemfile", __dir__)
|
5
|
+
|
6
|
+
require "bundler/setup" if File.exist?(ENV["BUNDLE_GEMFILE"])
|
7
|
+
$LOAD_PATH.unshift File.expand_path("../../../lib", __dir__)
|