rails_twirp 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 57a19d4ec190c08eaece39ae6867d9aee3f2b83a3196384f8b0a16c03fbf9739
4
- data.tar.gz: 225ce5477525001c7c401af85a98e68d3fe7543bcf20e2fa9bdcd13f34a77a4b
3
+ metadata.gz: 8df8457b09993d7d5c986130e724948df049c201b6797f33a7b66458bde98918
4
+ data.tar.gz: 0a2100cbefd37754f9dbd6cf666def1c29d0936322f72837d7e60653b5510b79
5
5
  SHA512:
6
- metadata.gz: 7991a7f8707e4f6b28055808edc662c1ec93a9723b059421621cdf0cc461704bc766abeea63e9881382675280f6321b4e3cba941527a684a920733be3ce7a977
7
- data.tar.gz: ea05bca838330b1526da0a6844783d7b5f9ab6cd2403050c318670f2d7a96b8c892d017c2b052b99f5063dde40331cf7b4486b084764adf85613c84c6b8e0da0
6
+ metadata.gz: 1b50c23cd6fa8852686ca96066c0a65a532f06ec0225b5ed04ac860218760d187e868d0b3d499855527ed23d355dacf8bf33eb1aaf2fd1b590b29aa12c8e836a
7
+ data.tar.gz: 461fe47bbafdd0bcf304f48b4938f307fd380d7bbce7049a703c60f7bc781b0f58b1cf06b7410c4a168e39441b1a9bd49b5091a0dbfc3472870c002bef6f51e1
@@ -0,0 +1,83 @@
1
+ require "forwardable"
2
+
3
+ module RailsTwirp
4
+ class ServiceMapper
5
+ class Mapping
6
+ attr_reader :controller, :action
7
+
8
+ def initialize(to:, **options)
9
+ controller, @action = split_to(to)
10
+ @controller = add_controller_module(controller, options.delete(:module))
11
+ raise ArgumentError, "Unknown argument #{options.keys.first}" unless options.empty?
12
+ end
13
+
14
+ def to_s
15
+ "#{controller}##{action}"
16
+ end
17
+
18
+ private
19
+
20
+ # copied from Rails
21
+ def split_to(to)
22
+ if /#/.match?(to)
23
+ to.split("#").map!(&:-@)
24
+ else
25
+ []
26
+ end
27
+ end
28
+
29
+ def add_controller_module(controller, modyoule)
30
+ return controller unless modyoule
31
+
32
+ if controller&.start_with?("/")
33
+ -controller[1..]
34
+ else
35
+ -[modyoule, controller].compact.join("/")
36
+ end
37
+ end
38
+ end
39
+
40
+ extend Forwardable
41
+ def_delegator :@mapper, :scope
42
+
43
+ def initialize(service_route_set, mapper)
44
+ @service_route_set = service_route_set
45
+ @mapper = mapper
46
+ end
47
+
48
+ def rpc(name, to:)
49
+ mapping = Mapping.new(to: to, module: @mapper.send(:_module))
50
+ @service_route_set.add_route(name, mapping)
51
+ end
52
+ end
53
+
54
+ class Mapper
55
+ def initialize(route_set)
56
+ @route_set = route_set
57
+ @module = nil
58
+ end
59
+
60
+ def service(service_definition, **options, &block)
61
+ service_route_set = @route_set.services[service_definition]
62
+ service_mapper = ServiceMapper.new(service_route_set, self)
63
+ scope(**options) { service_mapper.instance_exec(&block) }
64
+ end
65
+
66
+ def scope(**options)
67
+ last_module = @module
68
+ if (modyoule = options.delete(:module))
69
+ @module = @module.nil? ? modyoule : "#{@module}/#{modyoule}"
70
+ end
71
+ raise ArgumentError, "Unknown scope argument #{options.keys.first}" unless options.empty?
72
+ yield
73
+ ensure
74
+ @module = last_module
75
+ end
76
+
77
+ private
78
+
79
+ def _module
80
+ @module
81
+ end
82
+ end
83
+ end
@@ -1,5 +1,7 @@
1
1
  # Most of this logic is stolen from Rails ActionDispatch::Routing::RouteSet
2
2
 
3
+ require "rails_twirp/mapper"
4
+
3
5
  module RailsTwirp
4
6
  class UnknownRpcError < StandardError; end
5
7
 
@@ -66,51 +68,5 @@ module RailsTwirp
66
68
  service
67
69
  end
68
70
  end
69
-
70
- class Mapping
71
- attr_reader :controller, :action
72
-
73
- def initialize(to:)
74
- @controller, @action = split_to(to)
75
- end
76
-
77
- def to_s
78
- "#{controller}##{action}"
79
- end
80
-
81
- private
82
-
83
- # copied from Rails
84
- def split_to(to)
85
- if /#/.match?(to)
86
- to.split("#").map!(&:-@)
87
- else
88
- []
89
- end
90
- end
91
- end
92
-
93
- class ServiceMapper
94
- def initialize(service_route_set)
95
- @service_route_set = service_route_set
96
- end
97
-
98
- def rpc(name, to:)
99
- mapping = Mapping.new(to: to)
100
- @service_route_set.add_route(name, mapping)
101
- end
102
- end
103
-
104
- class Mapper
105
- def initialize(route_set)
106
- @route_set = route_set
107
- end
108
-
109
- def service(service_definition, &block)
110
- service_route_set = @route_set.services[service_definition]
111
- service_mapper = ServiceMapper.new(service_route_set)
112
- service_mapper.instance_exec(&block)
113
- end
114
- end
115
71
  end
116
72
  end
@@ -1,3 +1,3 @@
1
1
  module RailsTwirp
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -0,0 +1,10 @@
1
+ module Testmod
2
+ module Nested
3
+ class OtherController < ApplicationTwirpController
4
+ def ping
5
+ response = RPC::DummyAPI::PingResponse.new(double_name: request.name * 2)
6
+ self.response_body = response
7
+ end
8
+ end
9
+ end
10
+ end
@@ -10,4 +10,12 @@ Rails.application.twirp.routes.draw do
10
10
  rpc "UncaughtError", to: "pings#uncaught_raise"
11
11
  rpc "BeforeError", to: "pings#before_error"
12
12
  end
13
+
14
+ scope module: :testmod do
15
+ service RPC::DummyAPI::DummyService do
16
+ scope module: "nested" do
17
+ rpc "Nested", to: "other#ping"
18
+ end
19
+ end
20
+ end
13
21
  end
@@ -12,6 +12,7 @@ service Dummy {
12
12
  rpc RaiseError(PingRequest) returns (PingResponse);
13
13
  rpc UncaughtError(PingRequest) returns (PingResponse);
14
14
  rpc BeforeError(PingRequest) returns (PingResponse);
15
+ rpc Nested(PingRequest) returns (PingResponse);
15
16
  }
16
17
 
17
18
  message PingRequest {
@@ -14,6 +14,7 @@ module RPC
14
14
  rpc :RaiseError, PingRequest, PingResponse, :ruby_method => :raise_error
15
15
  rpc :UncaughtError, PingRequest, PingResponse, :ruby_method => :uncaught_error
16
16
  rpc :BeforeError, PingRequest, PingResponse, :ruby_method => :before_error
17
+ rpc :Nested, PingRequest, PingResponse, :ruby_method => :nested
17
18
  end
18
19
 
19
20
  class DummyClient < Twirp::Client
@@ -0,0 +1,9 @@
1
+ require "test_helper"
2
+
3
+ class OtherControllerTest < RailsTwirp::IntegrationTest
4
+ test "modules work" do
5
+ req = RPC::DummyAPI::PingRequest.new(name: "Bouke")
6
+ rpc RPC::DummyAPI::DummyService, "Nested", req
7
+ assert_equal "BoukeBouke", response.double_name
8
+ end
9
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_twirp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bouke van der Bijl
@@ -59,6 +59,7 @@ files:
59
59
  - lib/rails_twirp/base.rb
60
60
  - lib/rails_twirp/engine.rb
61
61
  - lib/rails_twirp/errors.rb
62
+ - lib/rails_twirp/mapper.rb
62
63
  - lib/rails_twirp/render_pb.rb
63
64
  - lib/rails_twirp/rescue.rb
64
65
  - lib/rails_twirp/route_set.rb
@@ -76,6 +77,7 @@ files:
76
77
  - test/dummy/app/controllers/application_twirp_controller.rb
77
78
  - test/dummy/app/controllers/concerns/.keep
78
79
  - test/dummy/app/controllers/pings_controller.rb
80
+ - test/dummy/app/controllers/testmod/nested/other_controller.rb
79
81
  - test/dummy/app/helpers/application_helper.rb
80
82
  - test/dummy/app/helpers/random_helper.rb
81
83
  - test/dummy/app/javascript/packs/application.js
@@ -126,6 +128,7 @@ files:
126
128
  - test/dummy/public/apple-touch-icon-precomposed.png
127
129
  - test/dummy/public/apple-touch-icon.png
128
130
  - test/dummy/public/favicon.ico
131
+ - test/other_controller_test.rb
129
132
  - test/ping_controller_test.rb
130
133
  - test/rails_twirp_test.rb
131
134
  - test/test_helper.rb
@@ -163,6 +166,7 @@ test_files:
163
166
  - test/dummy/app/controllers/application_twirp_controller.rb
164
167
  - test/dummy/app/controllers/concerns/.keep
165
168
  - test/dummy/app/controllers/pings_controller.rb
169
+ - test/dummy/app/controllers/testmod/nested/other_controller.rb
166
170
  - test/dummy/app/helpers/application_helper.rb
167
171
  - test/dummy/app/helpers/random_helper.rb
168
172
  - test/dummy/app/javascript/packs/application.js
@@ -213,6 +217,7 @@ test_files:
213
217
  - test/dummy/public/apple-touch-icon-precomposed.png
214
218
  - test/dummy/public/apple-touch-icon.png
215
219
  - test/dummy/public/favicon.ico
220
+ - test/other_controller_test.rb
216
221
  - test/ping_controller_test.rb
217
222
  - test/rails_twirp_test.rb
218
223
  - test/test_helper.rb