twirp-rails 0.1.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 +7 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +94 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/twirp/rails.rb +30 -0
- data/lib/twirp/rails/configuration.rb +17 -0
- data/lib/twirp/rails/engine.rb +14 -0
- data/lib/twirp/rails/helpers.rb +17 -0
- data/lib/twirp/rails/routes.rb +45 -0
- data/lib/twirp/rails/version.rb +5 -0
- data/twirp-rails.gemspec +32 -0
- metadata +129 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9dcf037d0a667d0d0279eb44e44f83b14483921b93c087bccf212e2961d97e82
|
4
|
+
data.tar.gz: ebd43a7b27ee04d31a5e07daa23af429003a9c6ecce93a241e7c76e54f402a34
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: eece932d46ad2dd988db38915d7e89e3a559072f82799a03fda573c531d66135e4e534f486235443be1f2cf1d682cc51b5ae9da29d3bc5e051f2f850f0a1f58a
|
7
|
+
data.tar.gz: ad9efed9706d6ff118a98228c27cab0cdb02cfb05cd98461047e98a2ad01f2930c3ff5c1fb8770f57ca2dc3c4aa42c79ee11267f9707ad0a9217228d1983ec2c
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Nobuhiro Nikushi
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
# Twirp::Rails [](https://travis-ci.org/nikushi/twirp-rails) [](https://badge.fury.io/rb/twirp-rails)
|
2
|
+
|
3
|
+
Twirp for Rails
|
4
|
+
|
5
|
+
## Features
|
6
|
+
|
7
|
+
[twirp-ruby](https://github.com/twitchtv/twirp-ruby) with Ruby on Rails. It provides auto routing installation, by using the `bind` helper method to bind a handler and a service.
|
8
|
+
|
9
|
+
```
|
10
|
+
$ bin/rails routes
|
11
|
+
|
12
|
+
Prefix Verb URI Pattern Controller#Action
|
13
|
+
POST /twirp/HelloService(.:format) hello#hello,hi,greet
|
14
|
+
```
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
|
18
|
+
Add this line to your application's Gemfile:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
gem 'twirp-rails'
|
22
|
+
```
|
23
|
+
|
24
|
+
And then execute:
|
25
|
+
|
26
|
+
$ bundle
|
27
|
+
|
28
|
+
Or install it yourself as:
|
29
|
+
|
30
|
+
$ gem install twirp-rails
|
31
|
+
|
32
|
+
## Configuration
|
33
|
+
|
34
|
+
After insalled, put the configuration file as follows:
|
35
|
+
|
36
|
+
```
|
37
|
+
# config/initializers/twirp_rails.rb
|
38
|
+
|
39
|
+
Twirp::Rails.configuration do |c|
|
40
|
+
# Modify the path below if you locates handlers under the different directory.
|
41
|
+
c.handlers_path = Rails.root.join('app', 'controllers', 'rpc')
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
### Routes
|
46
|
+
|
47
|
+
Add the line `use_twirp` in `config/routes.rb`. By this, you can tell Rails app what endpoints to be served.
|
48
|
+
|
49
|
+
```
|
50
|
+
# config/routes.rb
|
51
|
+
|
52
|
+
Rails.application.routes.draw do
|
53
|
+
use_twirp
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
### Binding
|
58
|
+
|
59
|
+
Next, let's link handlers(a.k.a controllers) with services. `bind` method can bind them.
|
60
|
+
|
61
|
+
```
|
62
|
+
class HelloHandler
|
63
|
+
include Twirp::Rails::Helpers
|
64
|
+
|
65
|
+
bind HelloService
|
66
|
+
|
67
|
+
def hello(_req, _env)
|
68
|
+
HelloResponse.new(message: 'hello')
|
69
|
+
end
|
70
|
+
end
|
71
|
+
```
|
72
|
+
|
73
|
+
So now corresponding routes will be defined.
|
74
|
+
|
75
|
+
```
|
76
|
+
$ bin/rails routes
|
77
|
+
|
78
|
+
Prefix Verb URI Pattern Controller#Action
|
79
|
+
POST /twirp/HelloService(.:format) hello#hello,hi,greet
|
80
|
+
```
|
81
|
+
|
82
|
+
## Development
|
83
|
+
|
84
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
85
|
+
|
86
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
87
|
+
|
88
|
+
## Contributing
|
89
|
+
|
90
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/twirp-rails.
|
91
|
+
|
92
|
+
## License
|
93
|
+
|
94
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "twirp/rails"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/twirp/rails.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "twirp/rails/configuration"
|
4
|
+
require "twirp/rails/helpers"
|
5
|
+
require "twirp/rails/routes"
|
6
|
+
require "twirp/rails/engine"
|
7
|
+
require "twirp/rails/version"
|
8
|
+
|
9
|
+
module Twirp
|
10
|
+
module Rails
|
11
|
+
class << self
|
12
|
+
def configuration(&block)
|
13
|
+
@configuration ||= Configuration.new
|
14
|
+
yield @configuration if block_given?
|
15
|
+
@configuration
|
16
|
+
end
|
17
|
+
attr_writer :configuration
|
18
|
+
|
19
|
+
# A store to register rack apps, which are the instantiated services.
|
20
|
+
# @return [Array<Twirp::Service>]
|
21
|
+
def services
|
22
|
+
@services ||= []
|
23
|
+
end
|
24
|
+
|
25
|
+
def load_handlers
|
26
|
+
Dir[File.join(configuration.handlers_path.to_s, '**', '*.rb')].each { |f| require f }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Twirp
|
4
|
+
module Rails
|
5
|
+
module Helpers
|
6
|
+
def self.included(klass)
|
7
|
+
klass.extend ClassMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def bind(service_klass)
|
12
|
+
Twirp::Rails.services << service_klass.new(new)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Twirp
|
4
|
+
module Rails
|
5
|
+
class Routes # :nodoc:
|
6
|
+
module Helper
|
7
|
+
def use_twirp(options = {})
|
8
|
+
services = Twirp::Rails.services
|
9
|
+
Twirp::Rails::Routes.new(self, services).generate_routes!(options)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# A module to modify the output of the controller part on `rails routes`
|
14
|
+
module Inspectable
|
15
|
+
REMOVABLE_SUFFIX_RE = /(_handler|_controller)\z/
|
16
|
+
|
17
|
+
def inspect
|
18
|
+
handler = instance_variable_get(:@handler).class.name.underscore.sub(REMOVABLE_SUFFIX_RE, '')
|
19
|
+
methods = self.class.rpcs.values.map { |h| h[:ruby_method].to_s }.sort.join(',')
|
20
|
+
[handler, methods].join('#')
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.install!
|
25
|
+
ActionDispatch::Routing::Mapper.send :include, Twirp::Rails::Routes::Helper
|
26
|
+
end
|
27
|
+
|
28
|
+
attr_reader :routes
|
29
|
+
|
30
|
+
def initialize(routes, services)
|
31
|
+
@routes = routes
|
32
|
+
@services = services
|
33
|
+
end
|
34
|
+
|
35
|
+
def generate_routes!(options)
|
36
|
+
routes.scope options[:scope] || 'twirp' do
|
37
|
+
@services.each do |service|
|
38
|
+
service.extend Inspectable
|
39
|
+
@routes.mount service, at: service.full_name
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/twirp-rails.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "twirp/rails/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "twirp-rails"
|
8
|
+
spec.version = Twirp::Rails::VERSION
|
9
|
+
spec.authors = ["Nobuhiro Nikushi"]
|
10
|
+
spec.email = ["deneb.ge@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "Twirp for Rails"
|
13
|
+
spec.description = spec.summary
|
14
|
+
spec.homepage = "https://github.com/nikushi/twirp-rails"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.required_ruby_version = '>= 2.0.0'
|
25
|
+
|
26
|
+
spec.add_dependency "railties", ">= 4.2"
|
27
|
+
spec.add_dependency 'twirp'
|
28
|
+
|
29
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
30
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
31
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: twirp-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nobuhiro Nikushi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-04-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: railties
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: twirp
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
description: Twirp for Rails
|
84
|
+
email:
|
85
|
+
- deneb.ge@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bin/console
|
98
|
+
- bin/setup
|
99
|
+
- lib/twirp/rails.rb
|
100
|
+
- lib/twirp/rails/configuration.rb
|
101
|
+
- lib/twirp/rails/engine.rb
|
102
|
+
- lib/twirp/rails/helpers.rb
|
103
|
+
- lib/twirp/rails/routes.rb
|
104
|
+
- lib/twirp/rails/version.rb
|
105
|
+
- twirp-rails.gemspec
|
106
|
+
homepage: https://github.com/nikushi/twirp-rails
|
107
|
+
licenses:
|
108
|
+
- MIT
|
109
|
+
metadata: {}
|
110
|
+
post_install_message:
|
111
|
+
rdoc_options: []
|
112
|
+
require_paths:
|
113
|
+
- lib
|
114
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: 2.0.0
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
requirements: []
|
125
|
+
rubygems_version: 3.0.3
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: Twirp for Rails
|
129
|
+
test_files: []
|