grpc-rest 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 624b3de8eba78302edff97ecf421a9d660fe781c72452b7f642fa5772e3a6234
4
- data.tar.gz: 39143f78f27e6c0905cfb2092b23c4427c70c5810d78243935baf5c38bee9443
3
+ metadata.gz: '097d1a366f7eb41bd6a4b1e68f8cacfa84e74013aaed85d3348a8be300503f64'
4
+ data.tar.gz: ef6e2ca8a7f43577a51d0fb9b85ad2ed6576f8591b33520732c6a62dd976daf2
5
5
  SHA512:
6
- metadata.gz: 1d3897f28befb1aadf27163d67771605116de984a80d86d9ae3b41d131729bfa75c0bd04c84f63ef622cf77637fe1958078d62e59afb1fc8cb95a073137e8733
7
- data.tar.gz: 415c00d2059a1cbd9923d5689333f6ae8c07ac9f2c595e0abaed8183106aa6f30e2da751fdeefb5b856969a62fac5f6ae63e0e36cd146c409768f23df7e02a34
6
+ metadata.gz: 6a181f74f58f23e2c89b73e24833197e365a167cdea63b55a31635d06349062becc15f9b1ead3572801514587be288fa5aca02f180b8c519a505350ea8e475f5
7
+ data.tar.gz: fd0c20102f445578d5c21a5a984e95323fbdc9a5d102c8e9d8ccadd17a9b9196896e0a6e6c55ac3fd1c98f6e3b3b6b4669e4ec5573970d1dbf1c1663b9fab46d
@@ -0,0 +1,31 @@
1
+ name: Release Gem
2
+ on:
3
+ push:
4
+ branches:
5
+ - main
6
+ tags:
7
+ - 'v*.*.*' # Matches semantic versioning tags like v1.0.0
8
+ workflow_dispatch: # Allows manual triggering of the workflow
9
+
10
+ jobs:
11
+ push:
12
+ name: Push gem to RubyGems.org
13
+ runs-on: ubuntu-latest
14
+
15
+ permissions:
16
+ id-token: write # IMPORTANT: this permission is mandatory for trusted publishing
17
+ contents: write # IMPORTANT: this permission is required for `rake release` to push the release tag
18
+
19
+ steps:
20
+ # Set up
21
+ - uses: actions/checkout@v4
22
+ with:
23
+ persist-credentials: false
24
+ - name: Set up Ruby
25
+ uses: ruby/setup-ruby@v1
26
+ with:
27
+ bundler-cache: true
28
+ ruby-version: ruby
29
+
30
+ # Release
31
+ - uses: rubygems/release-gem@v1
data/CHANGELOG CHANGED
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## UNRELEASED
9
9
 
10
+ # 0.5.0 - 2025-08-25
11
+
12
+ - ***MAJOR CHANGE***: `grpc-rest` now no longer requires any code generation. All functionality is dynamic and loads on startup.
13
+ - Added `GrpcRest.services` configuration option.
14
+
10
15
  # 0.4.0 - 2025-07-08
11
16
 
12
17
  - Feature: Forward request headers to the gRPC call as metadata.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- grpc-rest (0.3.1)
4
+ grpc-rest (0.5.0)
5
5
  google-protobuf (>= 4.30.2)
6
6
  grpc
7
7
  rails (>= 6.0)
@@ -9,98 +9,109 @@ PATH
9
9
  GEM
10
10
  remote: https://rubygems.org/
11
11
  specs:
12
- actioncable (7.0.8.1)
13
- actionpack (= 7.0.8.1)
14
- activesupport (= 7.0.8.1)
12
+ actioncable (8.0.2.1)
13
+ actionpack (= 8.0.2.1)
14
+ activesupport (= 8.0.2.1)
15
15
  nio4r (~> 2.0)
16
16
  websocket-driver (>= 0.6.1)
17
- actionmailbox (7.0.8.1)
18
- actionpack (= 7.0.8.1)
19
- activejob (= 7.0.8.1)
20
- activerecord (= 7.0.8.1)
21
- activestorage (= 7.0.8.1)
22
- activesupport (= 7.0.8.1)
23
- mail (>= 2.7.1)
24
- net-imap
25
- net-pop
26
- net-smtp
27
- actionmailer (7.0.8.1)
28
- actionpack (= 7.0.8.1)
29
- actionview (= 7.0.8.1)
30
- activejob (= 7.0.8.1)
31
- activesupport (= 7.0.8.1)
32
- mail (~> 2.5, >= 2.5.4)
33
- net-imap
34
- net-pop
35
- net-smtp
36
- rails-dom-testing (~> 2.0)
37
- actionpack (7.0.8.1)
38
- actionview (= 7.0.8.1)
39
- activesupport (= 7.0.8.1)
40
- rack (~> 2.0, >= 2.2.4)
17
+ zeitwerk (~> 2.6)
18
+ actionmailbox (8.0.2.1)
19
+ actionpack (= 8.0.2.1)
20
+ activejob (= 8.0.2.1)
21
+ activerecord (= 8.0.2.1)
22
+ activestorage (= 8.0.2.1)
23
+ activesupport (= 8.0.2.1)
24
+ mail (>= 2.8.0)
25
+ actionmailer (8.0.2.1)
26
+ actionpack (= 8.0.2.1)
27
+ actionview (= 8.0.2.1)
28
+ activejob (= 8.0.2.1)
29
+ activesupport (= 8.0.2.1)
30
+ mail (>= 2.8.0)
31
+ rails-dom-testing (~> 2.2)
32
+ actionpack (8.0.2.1)
33
+ actionview (= 8.0.2.1)
34
+ activesupport (= 8.0.2.1)
35
+ nokogiri (>= 1.8.5)
36
+ rack (>= 2.2.4)
37
+ rack-session (>= 1.0.1)
41
38
  rack-test (>= 0.6.3)
42
- rails-dom-testing (~> 2.0)
43
- rails-html-sanitizer (~> 1.0, >= 1.2.0)
44
- actiontext (7.0.8.1)
45
- actionpack (= 7.0.8.1)
46
- activerecord (= 7.0.8.1)
47
- activestorage (= 7.0.8.1)
48
- activesupport (= 7.0.8.1)
39
+ rails-dom-testing (~> 2.2)
40
+ rails-html-sanitizer (~> 1.6)
41
+ useragent (~> 0.16)
42
+ actiontext (8.0.2.1)
43
+ actionpack (= 8.0.2.1)
44
+ activerecord (= 8.0.2.1)
45
+ activestorage (= 8.0.2.1)
46
+ activesupport (= 8.0.2.1)
49
47
  globalid (>= 0.6.0)
50
48
  nokogiri (>= 1.8.5)
51
- actionview (7.0.8.1)
52
- activesupport (= 7.0.8.1)
49
+ actionview (8.0.2.1)
50
+ activesupport (= 8.0.2.1)
53
51
  builder (~> 3.1)
54
- erubi (~> 1.4)
55
- rails-dom-testing (~> 2.0)
56
- rails-html-sanitizer (~> 1.1, >= 1.2.0)
57
- activejob (7.0.8.1)
58
- activesupport (= 7.0.8.1)
52
+ erubi (~> 1.11)
53
+ rails-dom-testing (~> 2.2)
54
+ rails-html-sanitizer (~> 1.6)
55
+ activejob (8.0.2.1)
56
+ activesupport (= 8.0.2.1)
59
57
  globalid (>= 0.3.6)
60
- activemodel (7.0.8.1)
61
- activesupport (= 7.0.8.1)
62
- activerecord (7.0.8.1)
63
- activemodel (= 7.0.8.1)
64
- activesupport (= 7.0.8.1)
65
- activestorage (7.0.8.1)
66
- actionpack (= 7.0.8.1)
67
- activejob (= 7.0.8.1)
68
- activerecord (= 7.0.8.1)
69
- activesupport (= 7.0.8.1)
58
+ activemodel (8.0.2.1)
59
+ activesupport (= 8.0.2.1)
60
+ activerecord (8.0.2.1)
61
+ activemodel (= 8.0.2.1)
62
+ activesupport (= 8.0.2.1)
63
+ timeout (>= 0.4.0)
64
+ activestorage (8.0.2.1)
65
+ actionpack (= 8.0.2.1)
66
+ activejob (= 8.0.2.1)
67
+ activerecord (= 8.0.2.1)
68
+ activesupport (= 8.0.2.1)
70
69
  marcel (~> 1.0)
71
- mini_mime (>= 1.1.0)
72
- activesupport (7.0.8.1)
73
- concurrent-ruby (~> 1.0, >= 1.0.2)
70
+ activesupport (8.0.2.1)
71
+ base64
72
+ benchmark (>= 0.3)
73
+ bigdecimal
74
+ concurrent-ruby (~> 1.0, >= 1.3.1)
75
+ connection_pool (>= 2.2.5)
76
+ drb
74
77
  i18n (>= 1.6, < 2)
78
+ logger (>= 1.4.2)
75
79
  minitest (>= 5.1)
76
- tzinfo (~> 2.0)
80
+ securerandom (>= 0.3)
81
+ tzinfo (~> 2.0, >= 2.0.5)
82
+ uri (>= 0.13.1)
77
83
  awesome_print (1.9.2)
78
- bigdecimal (3.1.9)
79
- builder (3.2.4)
84
+ base64 (0.3.0)
85
+ benchmark (0.4.1)
86
+ bigdecimal (3.2.2)
87
+ builder (3.3.0)
80
88
  byebug (12.0.0)
81
- concurrent-ruby (1.2.3)
89
+ concurrent-ruby (1.3.5)
90
+ connection_pool (2.5.3)
82
91
  crass (1.0.6)
83
- date (3.3.4)
84
- diff-lcs (1.5.1)
85
- erubi (1.12.0)
92
+ date (3.4.1)
93
+ diff-lcs (1.6.2)
94
+ drb (2.2.3)
95
+ erb (5.0.2)
96
+ erubi (1.13.1)
86
97
  globalid (1.2.1)
87
98
  activesupport (>= 6.1)
88
- google-protobuf (4.30.2-arm64-darwin)
99
+ google-protobuf (4.32.0-arm64-darwin)
89
100
  bigdecimal
90
101
  rake (>= 13)
91
- google-protobuf (4.30.2-x86_64-linux)
102
+ google-protobuf (4.32.0-x86_64-linux-gnu)
92
103
  bigdecimal
93
104
  rake (>= 13)
94
- googleapis-common-protos-types (1.19.0)
105
+ googleapis-common-protos-types (1.20.0)
95
106
  google-protobuf (>= 3.18, < 5.a)
96
- grpc (1.71.0-arm64-darwin)
107
+ grpc (1.74.1-arm64-darwin)
97
108
  google-protobuf (>= 3.25, < 5.0)
98
109
  googleapis-common-protos-types (~> 1.0)
99
- grpc (1.71.0-x86_64-linux)
110
+ grpc (1.74.1-x86_64-linux-gnu)
100
111
  google-protobuf (>= 3.25, < 5.0)
101
112
  googleapis-common-protos-types (~> 1.0)
102
- grpc-tools (1.64.0)
103
- gruf (2.20.0)
113
+ grpc-tools (1.74.1)
114
+ gruf (2.21.1)
104
115
  activesupport (> 4)
105
116
  concurrent-ruby (> 1)
106
117
  grpc (~> 1.10)
@@ -108,10 +119,16 @@ GEM
108
119
  json (>= 2.3)
109
120
  slop (>= 4.6)
110
121
  zeitwerk (>= 2)
111
- i18n (1.14.4)
122
+ i18n (1.14.7)
112
123
  concurrent-ruby (~> 1.0)
113
- json (2.7.2)
114
- loofah (2.22.0)
124
+ io-console (0.8.1)
125
+ irb (1.15.2)
126
+ pp (>= 0.6.0)
127
+ rdoc (>= 4.0.0)
128
+ reline (>= 0.4.2)
129
+ json (2.13.2)
130
+ logger (1.7.0)
131
+ loofah (2.24.1)
115
132
  crass (~> 1.0.2)
116
133
  nokogiri (>= 1.12.0)
117
134
  mail (2.8.1)
@@ -120,72 +137,88 @@ GEM
120
137
  net-pop
121
138
  net-smtp
122
139
  marcel (1.0.4)
123
- method_source (1.0.0)
124
140
  mini_mime (1.1.5)
125
- minitest (5.22.3)
126
- net-imap (0.4.10)
141
+ minitest (5.25.5)
142
+ net-imap (0.5.9)
127
143
  date
128
144
  net-protocol
129
145
  net-pop (0.1.2)
130
146
  net-protocol
131
147
  net-protocol (0.2.2)
132
148
  timeout
133
- net-smtp (0.5.0)
149
+ net-smtp (0.5.1)
134
150
  net-protocol
135
- nio4r (2.7.1)
136
- nokogiri (1.16.3-arm64-darwin)
151
+ nio4r (2.7.4)
152
+ nokogiri (1.18.9-arm64-darwin)
137
153
  racc (~> 1.4)
138
- nokogiri (1.16.3-x86_64-linux)
154
+ nokogiri (1.18.9-x86_64-linux-gnu)
139
155
  racc (~> 1.4)
140
- racc (1.7.3)
141
- rack (2.2.9)
142
- rack-test (2.1.0)
156
+ pp (0.6.2)
157
+ prettyprint
158
+ prettyprint (0.2.0)
159
+ psych (5.2.6)
160
+ date
161
+ stringio
162
+ racc (1.8.1)
163
+ rack (3.2.0)
164
+ rack-session (2.1.1)
165
+ base64 (>= 0.1.0)
166
+ rack (>= 3.0.0)
167
+ rack-test (2.2.0)
143
168
  rack (>= 1.3)
144
- rails (7.0.8.1)
145
- actioncable (= 7.0.8.1)
146
- actionmailbox (= 7.0.8.1)
147
- actionmailer (= 7.0.8.1)
148
- actionpack (= 7.0.8.1)
149
- actiontext (= 7.0.8.1)
150
- actionview (= 7.0.8.1)
151
- activejob (= 7.0.8.1)
152
- activemodel (= 7.0.8.1)
153
- activerecord (= 7.0.8.1)
154
- activestorage (= 7.0.8.1)
155
- activesupport (= 7.0.8.1)
169
+ rackup (2.2.1)
170
+ rack (>= 3)
171
+ rails (8.0.2.1)
172
+ actioncable (= 8.0.2.1)
173
+ actionmailbox (= 8.0.2.1)
174
+ actionmailer (= 8.0.2.1)
175
+ actionpack (= 8.0.2.1)
176
+ actiontext (= 8.0.2.1)
177
+ actionview (= 8.0.2.1)
178
+ activejob (= 8.0.2.1)
179
+ activemodel (= 8.0.2.1)
180
+ activerecord (= 8.0.2.1)
181
+ activestorage (= 8.0.2.1)
182
+ activesupport (= 8.0.2.1)
156
183
  bundler (>= 1.15.0)
157
- railties (= 7.0.8.1)
158
- rails-dom-testing (2.2.0)
184
+ railties (= 8.0.2.1)
185
+ rails-dom-testing (2.3.0)
159
186
  activesupport (>= 5.0.0)
160
187
  minitest
161
188
  nokogiri (>= 1.6)
162
- rails-html-sanitizer (1.6.0)
189
+ rails-html-sanitizer (1.6.2)
163
190
  loofah (~> 2.21)
164
- nokogiri (~> 1.14)
165
- railties (7.0.8.1)
166
- actionpack (= 7.0.8.1)
167
- activesupport (= 7.0.8.1)
168
- method_source
191
+ nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
192
+ railties (8.0.2.1)
193
+ actionpack (= 8.0.2.1)
194
+ activesupport (= 8.0.2.1)
195
+ irb (~> 1.13)
196
+ rackup (>= 1.0.0)
169
197
  rake (>= 12.2)
170
- thor (~> 1.0)
171
- zeitwerk (~> 2.5)
172
- rake (13.2.1)
173
- rspec (3.13.0)
198
+ thor (~> 1.0, >= 1.2.2)
199
+ zeitwerk (~> 2.6)
200
+ rake (13.3.0)
201
+ rdoc (6.14.2)
202
+ erb
203
+ psych (>= 4.0.0)
204
+ reline (0.6.2)
205
+ io-console (~> 0.5)
206
+ rspec (3.13.1)
174
207
  rspec-core (~> 3.13.0)
175
208
  rspec-expectations (~> 3.13.0)
176
209
  rspec-mocks (~> 3.13.0)
177
- rspec-core (3.13.0)
210
+ rspec-core (3.13.5)
178
211
  rspec-support (~> 3.13.0)
179
- rspec-expectations (3.13.0)
212
+ rspec-expectations (3.13.5)
180
213
  diff-lcs (>= 1.2.0, < 2.0)
181
214
  rspec-support (~> 3.13.0)
182
- rspec-mocks (3.13.0)
215
+ rspec-mocks (3.13.5)
183
216
  diff-lcs (>= 1.2.0, < 2.0)
184
217
  rspec-support (~> 3.13.0)
185
- rspec-rails (6.1.2)
186
- actionpack (>= 6.1)
187
- activesupport (>= 6.1)
188
- railties (>= 6.1)
218
+ rspec-rails (8.0.2)
219
+ actionpack (>= 7.2)
220
+ activesupport (>= 7.2)
221
+ railties (>= 7.2)
189
222
  rspec-core (~> 3.13)
190
223
  rspec-expectations (~> 3.13)
191
224
  rspec-mocks (~> 3.13)
@@ -193,16 +226,21 @@ GEM
193
226
  rspec-snapshot (2.0.3)
194
227
  awesome_print (> 1.0.0)
195
228
  rspec (> 3.0.0)
196
- rspec-support (3.13.1)
229
+ rspec-support (3.13.5)
230
+ securerandom (0.4.1)
197
231
  slop (4.10.1)
198
- thor (1.3.1)
199
- timeout (0.4.1)
232
+ stringio (3.1.7)
233
+ thor (1.4.0)
234
+ timeout (0.4.3)
200
235
  tzinfo (2.0.6)
201
236
  concurrent-ruby (~> 1.0)
202
- websocket-driver (0.7.6)
237
+ uri (1.0.3)
238
+ useragent (0.16.11)
239
+ websocket-driver (0.8.0)
240
+ base64
203
241
  websocket-extensions (>= 0.1.0)
204
242
  websocket-extensions (0.1.5)
205
- zeitwerk (2.6.13)
243
+ zeitwerk (2.7.3)
206
244
 
207
245
  PLATFORMS
208
246
  arm64-darwin
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # grpc-rest
2
- Generate Rails routes and controllers from protobuf files.
2
+ Generate dynamic Rails routes and controllers from protobuf files.
3
3
 
4
4
  grpc-rest allows you to have a single codebase that serves both gRPC and classic Rails routes, with
5
5
  a single handler (the gRPC handler) for both types of requests. It will add Rails routes to your
@@ -12,14 +12,13 @@ the default port for Rails is 3000 and for gRPC is 9001).
12
12
 
13
13
  With this, you get the automatic client code generation via Swagger and gRPC, the input validation that's automatic with gRPC, and the ease of use of tools like `curl` and Postman with REST.
14
14
 
15
- ## Components
15
+ ## Upgrading to 0.5
16
16
 
17
- `grpc-rest` comes with two main components:
18
-
19
- * A protobuf generator plugin that generates Ruby files for Rails routes and controllers.
20
- * The Rails library that powers these generated files.
21
-
22
- The protobuf generator uses the same annotations as [grpc-gateway](https://github.com/grpc-ecosystem/grpc-gateway). This also gives you the benefit of being able to generate Swagger files by using protoc.
17
+ Before 0.5, you needed to generate Rails controller and route files. As of 0.5, these are no longer necessary and you can remove the following:
18
+ * Generated files in `app/controllers`
19
+ * Generated files in `config/routes`
20
+ * The `draw(:grpc)` line in `config/routes.rb`
21
+ * Add `require 'grpc_rest'` to your `config/application.rb`
23
22
 
24
23
  ## Installation
25
24
 
@@ -29,9 +28,17 @@ Add the following to your `Gemfile`:
29
28
  gem 'grpc-rest'
30
29
  ```
31
30
 
32
- and run `bundle install`. Then when using `protoc` or `buf generate`, ensure you prefix the command with `bundle exec`, e.g.
31
+ and run `bundle install`.
33
32
 
34
- `bundle exec buf generate`
33
+ Then add this to your `config/application.rb`:
34
+
35
+ ```ruby
36
+ require 'grpc_rest'
37
+ ```
38
+
39
+ ## Routes
40
+
41
+ `grpc-rest` uses the same annotations as [grpc-gateway](https://github.com/grpc-ecosystem/grpc-gateway) to define routes. This also gives you the benefit of being able to generate Swagger files by using protoc.
35
42
 
36
43
  ## Example
37
44
 
@@ -61,7 +68,7 @@ service ExampleService {
61
68
  }
62
69
  ```
63
70
 
64
- First, you need to generate the Ruby files from this. You can do this with plain `protoc`, but it's much easier to handle if you use [buf](https://buf.build/). Here's an example `buf.gen.yaml` file:
71
+ You need to generate the Ruby files from this, the same way you would for any Protobuf Ruby code. You can do this with plain `protoc`, but it's much easier to handle if you use [buf](https://buf.build/). Here's an example `buf.gen.yaml` file:
65
72
 
66
73
  ```yaml
67
74
  version: v1
@@ -76,59 +83,16 @@ plugins:
76
83
  out: app/gen
77
84
  opt:
78
85
  - paths=source_relative
79
- - name: rails
80
- out: .
81
- opt:
82
- # this should be the same directory as the output of your Ruby plugin
83
- - require=app/gen
84
86
  ```
85
87
 
86
- Then, you can run `buf generate` to generate the Ruby files. This will generate:
87
- * the Protobuf Ruby files for grpc, in `app/gen`
88
- * A new route file, in `config/routes/grpc.rb`
89
- * A new controller file, in `app/controllers`.
88
+ Then, you can run `buf generate` to generate the Ruby files.
90
89
 
91
- The generated route file will look like this:
90
+ The dynamic route will act like this:
92
91
 
93
92
  ```ruby
94
93
  get "example/:name", to: "example#get_example"
95
94
  ```
96
95
 
97
- and the generated controller will look like this:
98
-
99
- ```ruby
100
- require 'services/example/example_services_pb'
101
- class ExampleServiceController < ActionController::Base
102
- protect_from_forgery with: :null_session
103
-
104
- METHOD_PARAM_MAP = {
105
-
106
- "example" => [
107
- {name: "name", val: nil, split_name:["name"]},
108
- ],
109
- }.freeze
110
-
111
- rescue_from StandardError do |e|
112
- render json: GrpcRest.error_msg(e)
113
- end
114
-
115
- def example
116
- grpc_request = Services::Example::ExampleRequest.new
117
- GrpcRest.assign_params(grpc_request, "/example/{name}", "", request.parameters)
118
- render json: GrpcRest.send_request("Services::Example::ExampleService", "example", grpc_request)
119
- end
120
-
121
- end
122
- ```
123
-
124
- To power it on, all you have to do is add the following to your `config/routes.rb`:
125
-
126
- ```ruby
127
- Rails.application.routes.draw do
128
- draw(:grpc) # Add this line
129
- end
130
- ```
131
-
132
96
  ## Caveats
133
97
 
134
98
  This gem does not currently support the full path expression capabilities of grpc-gateway or the Google [http proto](https://github.com/googleapis/googleapis/blob/master/google/api/http.proto). It only supports very basic single wildcard globbing (`*`). Contributions are welcome for more complex cases if they are needed.
@@ -177,6 +141,14 @@ end
177
141
 
178
142
  ### Configuration
179
143
 
144
+ If you're using `gruf`, grpc-rest will automatically collect the known Gruf controllers. If you're not using it, you have to tell grpc-rest which services you want to generate for by setting:
145
+
146
+ ```ruby
147
+ GrpcRest.services = [
148
+ Example::ExampleService::Service
149
+ ]
150
+ ```
151
+
180
152
  GrpcRest currently has one configuration option, `strict_mode`, which defaults to false. When set to true, any JSON request that includes an unknown field will be rejected.
181
153
 
182
154
  ```ruby
@@ -193,12 +165,6 @@ To regenerate Ruby protobuf code for tests, install the `grpc-tools` gem and run
193
165
  grpc_tools_ruby_protoc -I=./spec/testdata --proto_path=./spec/google-deps --ruby_out=./spec --grpc_out=./spec ./spec/testdata/test_service.proto
194
166
  ```
195
167
 
196
- To regenerate the controller and route files for tests, run:
197
-
198
- ```
199
- bundle exec grpc_tools_ruby_protoc -I=./spec/testdata --proto_path=./spec/google-deps --rails_out=spec/testdata/base --rails_opt=require=spec/gen ./spec/testdata/test_service.proto
200
- ```
201
-
202
168
  ## License
203
169
 
204
170
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
@@ -0,0 +1,51 @@
1
+ require 'google/protobuf'
2
+ require 'grpc'
3
+
4
+ module GrpcRestControllers
5
+ end
6
+
7
+ module GrpcRest
8
+ class Controller < ActionController::Base
9
+ protect_from_forgery with: :null_session
10
+
11
+ rescue_from StandardError do |e|
12
+ render json: GrpcRest.error_msg(e), status: :internal_server_error
13
+ end
14
+ rescue_from GRPC::BadStatus do |e|
15
+ render json: GrpcRest.error_msg(e), status: GrpcRest.grpc_http_status(e.code)
16
+ end
17
+ rescue_from ActionDispatch::Http::Parameters::ParseError, Google::Protobuf::TypeError do |e|
18
+ render json: GrpcRest.error_msg(e), status: :bad_request
19
+ end
20
+ end
21
+
22
+ def self.controller(service)
23
+ # e.g. GrpcRestControllers::MyServiceController
24
+ klass = Class.new(GrpcRest::Controller) do
25
+ service.methods.each do |method|
26
+ # e.g. def my_service_test_method
27
+ define_method(method.name) do
28
+ parameters = request.parameters.to_h.deep_transform_keys(&:underscore).
29
+ except('controller', 'action', service.name.underscore)
30
+ grpc_request = GrpcRest.init_request(method.request_type, parameters)
31
+ GrpcRest.assign_params(grpc_request,
32
+ method.path_info.map(&:to_h),
33
+ method.option_body,
34
+ request.parameters)
35
+ render json: GrpcRest.send_request(service.service,
36
+ method.name.underscore,
37
+ grpc_request,
38
+ method.rest_options,
39
+ headers: request.headers)
40
+ end
41
+ Rails.application.routes.append do
42
+ service_name = service.name.demodulize.underscore
43
+ path = "grpc_rest_controllers/#{service_name}##{method.name.underscore}"
44
+ # e.g. get '/my_service/test_method' => '/grpc_rest_controllers/my_service#test_method'
45
+ self.send(method.http_method, method.sanitized_path => path)
46
+ end
47
+ end
48
+ end
49
+ GrpcRestControllers.const_set("#{service.name.demodulize}Controller", klass)
50
+ end
51
+ end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module ProtocGenRails
4
- class Method
3
+ module GrpcRest
4
+ class GeneratedMethod
5
5
  PathInfo = Struct.new(:name, :val, :split_name)
6
6
 
7
7
  attr_accessor :http_method, :path, :path_info, :rest_options
@@ -22,7 +22,7 @@ module ProtocGenRails
22
22
 
23
23
  # @return [String]
24
24
  def request_type
25
- @method.input_type.name.split('.').map(&:camelcase).join('::')
25
+ @method.input_type.msgclass
26
26
  end
27
27
 
28
28
  # @return [String]