protobuf-rpc-register 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 +11 -0
- data/.rspec +3 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +9 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/protobuf/rpc/clients/base.rb +97 -0
- data/lib/protobuf/rpc/generator.rb +41 -0
- data/lib/protobuf/rpc/interactions/base.rb +17 -0
- data/lib/protobuf/rpc/messages/error.rb +24 -0
- data/lib/protobuf/rpc/messages/rpc_compressed_message.rb +43 -0
- data/lib/protobuf/rpc/register.rb +15 -0
- data/lib/protobuf/rpc/register/version.rb +7 -0
- data/lib/protobuf/rpc/serializer.rb +130 -0
- data/lib/protobuf/rpc/services/base.rb +58 -0
- data/protobuf-rpc-register.gemspec +43 -0
- data/protos/protobuf/rpc/messages/error.proto +9 -0
- data/protos/protobuf/rpc/messages/rpc_compressed_message.proto +18 -0
- metadata +234 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bcea22d3283c089a3b9dac72872172d72f5a5098
|
4
|
+
data.tar.gz: afc4672eef580a483eb7d88738264df4f4f489cd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e4536ad1506dff261d3c95d432efe93d5a2d108e0084bf87f32f82444125d6e35d69af007a1fabaa4ab0b298ff54d456729ed73139b0a502ceabe81eda35a511
|
7
|
+
data.tar.gz: 636c4605381351148420a192dd3cdaabfc93fef75c954d4aa851bf1c5492d3242df66f11344f4f1999cbc812547fe9367ea8dc221a3208ace17dfd94b508e4b8
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at scorix@gmail.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 scorix
|
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,41 @@
|
|
1
|
+
# Protobuf::Rpc::Register
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/protobuf/rpc/register`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'protobuf-rpc-register'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install protobuf-rpc-register
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
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).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/protobuf-rpc-register. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "protobuf/rpc/register"
|
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
|
data/bin/setup
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
module Protobuf
|
2
|
+
module Rpc
|
3
|
+
module Clients
|
4
|
+
class Base
|
5
|
+
attr_accessor :logger
|
6
|
+
|
7
|
+
mattr_reader(:mutex) { Mutex.new }
|
8
|
+
|
9
|
+
def initialize(options = {})
|
10
|
+
@options = options
|
11
|
+
@namespace = options[:namespace]
|
12
|
+
@logger ||= Logger.new(STDOUT)
|
13
|
+
end
|
14
|
+
|
15
|
+
def send_rpc_request(method, msg)
|
16
|
+
res = nil
|
17
|
+
|
18
|
+
names = self.class.name.split('::')
|
19
|
+
svc_class = [names[0..-3], 'Services', names[-1]].flatten.join('::')
|
20
|
+
|
21
|
+
Object.const_get(svc_class).client(@options).send(method, Serializer.dump(msg)) do |c|
|
22
|
+
c.on_success do |rpc_compressed_message|
|
23
|
+
res = Protobuf::Rpc::Serializer.load(rpc_compressed_message)
|
24
|
+
end
|
25
|
+
|
26
|
+
c.on_failure do |error|
|
27
|
+
exception_name = Protobuf::Socketrpc::ErrorReason.name_for_tag(error.code).to_s.downcase
|
28
|
+
exception_class = Protobuf::Rpc.const_get(exception_name.camelize)
|
29
|
+
exception = exception_class.new(error.message)
|
30
|
+
logger.error exception
|
31
|
+
raise exception
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
check_response_error(res)
|
36
|
+
end
|
37
|
+
|
38
|
+
def check_response_error(res, raise_error: true)
|
39
|
+
if res.is_a?(Messages::Error)
|
40
|
+
error_class = self.class.mutex.synchronize do
|
41
|
+
module_name = @namespace.camelize
|
42
|
+
m = Object.const_defined?(module_name, false) ? Object.const_get(module_name, false) : Object.const_set(module_name, Module.new)
|
43
|
+
m.const_defined?(:Rpc, false) ? m.const_get(:Rpc, false) : m.const_set(:Rpc, Module.new)
|
44
|
+
|
45
|
+
if m.const_defined?(res.error_class, false)
|
46
|
+
m.const_get(res.error_class, false)
|
47
|
+
else
|
48
|
+
module_name = res.error_class.deconstantize
|
49
|
+
class_name = res.error_class.demodulize
|
50
|
+
base_module = m::Rpc
|
51
|
+
module_name.split('::').each do |m|
|
52
|
+
base_module.const_defined?(m, false) || base_module.const_set(m, Module.new)
|
53
|
+
base_module = base_module.const_get(m, false)
|
54
|
+
end
|
55
|
+
error_superclass = res.error_class.safe_constantize || begin
|
56
|
+
require res.error_class.split('::')[0].underscore
|
57
|
+
res.error_class.constantize
|
58
|
+
rescue LoadError
|
59
|
+
raise NameError.new("uninitialized constant #{res.error_class}", res.error_class)
|
60
|
+
end
|
61
|
+
base_module.const_set(class_name, Class.new(error_superclass) do
|
62
|
+
def initialize(message = nil)
|
63
|
+
@message = message
|
64
|
+
end
|
65
|
+
|
66
|
+
def inspect
|
67
|
+
"#{self.class}: #{@message}"
|
68
|
+
end
|
69
|
+
|
70
|
+
def message
|
71
|
+
@message
|
72
|
+
end
|
73
|
+
end)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
error = error_class.new(res.error_message)
|
78
|
+
error.set_backtrace(res.error_backtrace)
|
79
|
+
logger.error error
|
80
|
+
raise error if raise_error
|
81
|
+
error
|
82
|
+
else
|
83
|
+
res
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def self.implement_rpc(rpc_method)
|
88
|
+
define_method(rpc_method) do |*args|
|
89
|
+
names = self.class.name.split('::')
|
90
|
+
msg_class = [names[0..-3], 'Messages', names[-1]].flatten.join('::')
|
91
|
+
send_rpc_request(rpc_method, Object.const_get(msg_class).new(*args))
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Protobuf
|
2
|
+
module Rpc
|
3
|
+
class Generator
|
4
|
+
def initialize(mod)
|
5
|
+
@mutex = Mutex.new
|
6
|
+
@services = {}
|
7
|
+
@clients = {}
|
8
|
+
@module = mod
|
9
|
+
end
|
10
|
+
|
11
|
+
def generate(service, with: [])
|
12
|
+
@mutex.synchronize do
|
13
|
+
define_service_class(service)
|
14
|
+
with.each { |m| @services[service].define_rpc(m) }
|
15
|
+
define_client_class(service)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
def define_service_class(service)
|
21
|
+
@module.const_set(:Services, Module.new) unless @module.const_defined?(:Services, false)
|
22
|
+
@services[service] ||= if @module::Services.const_defined?(service, false)
|
23
|
+
@module::Services.const_get(service, false)
|
24
|
+
else
|
25
|
+
@module::Services.const_set(service, Class.new(Protobuf::Rpc::Services::Base))
|
26
|
+
end
|
27
|
+
@services[service].inherit_rpcs!
|
28
|
+
end
|
29
|
+
|
30
|
+
def define_client_class(service)
|
31
|
+
@module.const_set(:Clients, Module.new) unless @module.const_defined?(:Clients)
|
32
|
+
@clients[service] ||= if @module::Clients.const_defined?(service, false)
|
33
|
+
@module::Clients.const_get(service, false)
|
34
|
+
else
|
35
|
+
@module::Clients.const_set(service, Class.new(Protobuf::Rpc::Clients::Base))
|
36
|
+
end
|
37
|
+
@services[service].rpcs.keys.each { |m| @clients[service].implement_rpc(m) }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'active_interaction'
|
2
|
+
|
3
|
+
module Protobuf
|
4
|
+
module Rpc
|
5
|
+
module Interactions
|
6
|
+
class Base < ::ActiveInteraction::Base
|
7
|
+
def self.except_attributes
|
8
|
+
[].freeze
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.include_attributes
|
12
|
+
[].freeze
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
### Generated by rprotoc. DO NOT EDIT!
|
2
|
+
### <proto file: protos/protobuf/rpc/messages/error.proto>
|
3
|
+
# syntax = "proto2";
|
4
|
+
#
|
5
|
+
# package protobuf.rpc.messages;
|
6
|
+
#
|
7
|
+
# message Error {
|
8
|
+
# required string error_class = 1;
|
9
|
+
# optional string error_message = 2;
|
10
|
+
# optional string error_backtrace = 3;
|
11
|
+
# }
|
12
|
+
|
13
|
+
|
14
|
+
module Protobuf
|
15
|
+
module Rpc
|
16
|
+
module Messages
|
17
|
+
class Error < ::Protobuf::Message
|
18
|
+
required :string, :error_class, 1
|
19
|
+
optional :string, :error_message, 2
|
20
|
+
repeated :string, :error_backtrace, 3
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
### Generated by rprotoc. DO NOT EDIT!
|
2
|
+
### <proto file: protos/protobuf/rpc/messages/rpc_compressed_message.proto>
|
3
|
+
# syntax = "proto2";
|
4
|
+
#
|
5
|
+
# package protobuf.rpc.messages;
|
6
|
+
#
|
7
|
+
# message RpcCompressedMessage {
|
8
|
+
# optional bool compressed = 1 [default = true];
|
9
|
+
# optional string response_type = 2;
|
10
|
+
# required bytes response_body = 3;
|
11
|
+
#
|
12
|
+
# enum Serializer{
|
13
|
+
# RAW = 0;
|
14
|
+
# MSGPACK = 1;
|
15
|
+
# YAML = 2;
|
16
|
+
# JSON = 3;
|
17
|
+
# MARSHAL = 4;
|
18
|
+
# }
|
19
|
+
# optional Serializer serializer = 4 [default = 1];
|
20
|
+
# }
|
21
|
+
|
22
|
+
|
23
|
+
module Protobuf
|
24
|
+
module Rpc
|
25
|
+
module Messages
|
26
|
+
class RpcCompressedMessage < ::Protobuf::Message
|
27
|
+
optional :bool, :compressed, 1, :default => true
|
28
|
+
optional :string, :response_type, 2
|
29
|
+
required :bytes, :response_body, 3
|
30
|
+
class Serializer < ::Protobuf::Enum
|
31
|
+
set_option :allow_alias
|
32
|
+
define :RAW, 0
|
33
|
+
define :MSGPACK, 1
|
34
|
+
define :YAML, 2
|
35
|
+
define :JSON, 3
|
36
|
+
define :MARSHAL, 4
|
37
|
+
end
|
38
|
+
optional Serializer, :serializer, 4, :default => 1
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'protobuf/rpc/register/version'
|
2
|
+
require 'protobuf/rpc/generator'
|
3
|
+
require 'protobuf/rpc/clients/base'
|
4
|
+
require 'protobuf/rpc/interactions/base'
|
5
|
+
require 'protobuf/rpc/services/base'
|
6
|
+
require 'protobuf/rpc/messages/error'
|
7
|
+
require 'protobuf/rpc/messages/rpc_compressed_message'
|
8
|
+
|
9
|
+
module Protobuf
|
10
|
+
module Rpc
|
11
|
+
module Register
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
@@ -0,0 +1,130 @@
|
|
1
|
+
module Protobuf
|
2
|
+
module Rpc
|
3
|
+
class Serializer
|
4
|
+
def self.dump(msg, serializer = nil)
|
5
|
+
return msg if msg.is_a?(Messages::RpcCompressedMessage)
|
6
|
+
dumped_message = Messages::RpcCompressedMessage.new(compressed: false)
|
7
|
+
|
8
|
+
# serialize the message
|
9
|
+
case msg
|
10
|
+
when ::Protobuf::Message
|
11
|
+
proto(dumped_message, msg)
|
12
|
+
when StandardError
|
13
|
+
error = Messages::Error.new(error_class: msg.class.name,
|
14
|
+
error_message: msg.message,
|
15
|
+
error_backtrace: msg.backtrace)
|
16
|
+
proto(dumped_message, error)
|
17
|
+
else
|
18
|
+
case serializer.to_s.upcase.to_sym
|
19
|
+
when :MSGPACK
|
20
|
+
msgpack(dumped_message, msg)
|
21
|
+
when :OJ
|
22
|
+
oj(dumped_message, msg)
|
23
|
+
when :MULTI_JSON
|
24
|
+
multi_json(dumped_message, msg)
|
25
|
+
when :JSON
|
26
|
+
json(dumped_message, msg)
|
27
|
+
when :YAML
|
28
|
+
yaml(dumped_message, msg)
|
29
|
+
when :MARSHAL
|
30
|
+
marshal(dumped_message, msg)
|
31
|
+
when :RAW
|
32
|
+
raw(dumped_message, msg)
|
33
|
+
else
|
34
|
+
if defined?(MessagePack) && msg.respond_to?(:to_msgpack)
|
35
|
+
msgpack(dumped_message, msg)
|
36
|
+
elsif defined?(Oj)
|
37
|
+
oj(dumped_message, msg)
|
38
|
+
elsif defined?(MultiJson)
|
39
|
+
multi_json(dumped_message, msg)
|
40
|
+
else
|
41
|
+
yaml(dumped_message, msg)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
dumped_message.compressed = true if msg.is_a?(String)
|
47
|
+
dumped_message.response_body = ActiveSupport::Gzip.compress(dumped_message.response_body) if dumped_message.compressed
|
48
|
+
dumped_message
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.load(msg)
|
52
|
+
body = msg.compressed ? ActiveSupport::Gzip.decompress(msg.response_body) : msg.response_body
|
53
|
+
|
54
|
+
if msg.response_type.present?
|
55
|
+
Object.const_get(msg.response_type).decode(body)
|
56
|
+
else
|
57
|
+
case msg.serializer.name
|
58
|
+
when :RAW
|
59
|
+
body
|
60
|
+
when :MSGPACK
|
61
|
+
require 'msgpack'
|
62
|
+
MessagePack.unpack(body)
|
63
|
+
when :MARSHAL
|
64
|
+
Marshal.load(body)
|
65
|
+
when :JSON
|
66
|
+
begin
|
67
|
+
require 'multi_json'
|
68
|
+
MultiJson.load(body)
|
69
|
+
rescue LoadError
|
70
|
+
require 'json'
|
71
|
+
JSON.parse(body)
|
72
|
+
end
|
73
|
+
when :YAML
|
74
|
+
require 'yaml'
|
75
|
+
YAML.load(body)
|
76
|
+
else
|
77
|
+
body
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
class << self
|
83
|
+
private
|
84
|
+
def proto(dumped_message, msg)
|
85
|
+
dumped_message.compressed = true
|
86
|
+
dumped_message.response_type = msg.class.name
|
87
|
+
dumped_message.response_body = msg.bytes
|
88
|
+
dumped_message.serializer = :RAW
|
89
|
+
end
|
90
|
+
|
91
|
+
def msgpack(dumped_message, msg)
|
92
|
+
dumped_message.response_body = msg.to_msgpack
|
93
|
+
dumped_message.serializer = :MSGPACK
|
94
|
+
end
|
95
|
+
|
96
|
+
def yaml(dumped_message, msg)
|
97
|
+
require 'yaml' unless msg.respond_to?(:to_yaml)
|
98
|
+
dumped_message.response_body = msg.to_yaml
|
99
|
+
dumped_message.serializer = :YAML
|
100
|
+
end
|
101
|
+
|
102
|
+
def oj(dumped_message, msg)
|
103
|
+
dumped_message.response_body = Oj.dump(msg)
|
104
|
+
dumped_message.serializer = :JSON
|
105
|
+
end
|
106
|
+
|
107
|
+
def multi_json(dumped_message, msg)
|
108
|
+
dumped_message.response_body = MultiJson.dump(msg)
|
109
|
+
dumped_message.serializer = :JSON
|
110
|
+
end
|
111
|
+
|
112
|
+
def marshal(dumped_message, msg)
|
113
|
+
dumped_message.response_body = Marshal.dump(msg)
|
114
|
+
dumped_message.serializer = :MARSHAL
|
115
|
+
end
|
116
|
+
|
117
|
+
def json(dumped_message, msg)
|
118
|
+
require 'json' unless msg.respond_to?(:to_json)
|
119
|
+
dumped_message.response_body = msg.to_json
|
120
|
+
dumped_message.serializer = :JSON
|
121
|
+
end
|
122
|
+
|
123
|
+
def raw(dumped_message, msg)
|
124
|
+
dumped_message.response_body = msg
|
125
|
+
dumped_message.serializer = :RAW
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'protobuf'
|
2
|
+
require 'protobuf/rpc/serializer'
|
3
|
+
|
4
|
+
module Protobuf
|
5
|
+
module Rpc
|
6
|
+
module Services
|
7
|
+
class Base < ::Protobuf::Rpc::Service
|
8
|
+
|
9
|
+
def compress_with(msg)
|
10
|
+
if msg.is_a?(StandardError)
|
11
|
+
msg = Messages::Error.new(error_class: msg.class.name, error_message: msg.message, error_backtrace: msg.backtrace)
|
12
|
+
end
|
13
|
+
respond_with Serializer.dump(msg)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.namespace
|
17
|
+
self.name.deconstantize.deconstantize
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.inherit_rpcs!
|
21
|
+
superclass.rpcs.keys.each { |x| define_rpc(x, superclass.msgclass) }
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.define_rpc(method, req = ::Protobuf::Rpc::Messages::RpcCompressedMessage, res = ::Protobuf::Rpc::Messages::RpcCompressedMessage)
|
25
|
+
self.rpc method, req, res
|
26
|
+
|
27
|
+
define_method(method) do
|
28
|
+
class_name = method.to_s.camelize.gsub('!', 'Bang').gsub('?', 'QuestionMark')
|
29
|
+
result = nil
|
30
|
+
|
31
|
+
begin
|
32
|
+
interaction = Object.const_get("#{self.class.namespace}::Interactions::#{self.class.name.demodulize}::#{class_name}", false)
|
33
|
+
rescue NameError => e
|
34
|
+
result = Protobuf::Rpc::MethodNotFound.new(e.message)
|
35
|
+
result.set_backtrace(e.backtrace)
|
36
|
+
else
|
37
|
+
begin
|
38
|
+
result = interaction.run!(request.to_hash)
|
39
|
+
case result
|
40
|
+
when Protobuf::Message
|
41
|
+
result
|
42
|
+
when ::ActiveRecord::Base, ::ActiveRecord::Relation
|
43
|
+
result = result.to_proto(deprecated: false, except: interaction.except_attributes, include: interaction.include_attributes)
|
44
|
+
else
|
45
|
+
result.respond_to?(:to_proto) ? result.to_proto : result
|
46
|
+
end
|
47
|
+
rescue => e
|
48
|
+
result = e
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
compress_with result
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'protobuf/rpc/register/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "protobuf-rpc-register"
|
8
|
+
spec.version = Protobuf::Rpc::Register::VERSION
|
9
|
+
spec.authors = ["scorix"]
|
10
|
+
spec.email = ["scorix@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Register for rpc services using protobuf rpc.}
|
13
|
+
spec.description = %q{Register for rpc services using protobuf rpc.}
|
14
|
+
spec.homepage = "https://github.com/scorix/protobuf-rpc-register"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
|
+
# delete this section to allow pushing this gem to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
32
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
33
|
+
spec.add_development_dependency 'rspec-its', '~> 1.2'
|
34
|
+
spec.add_development_dependency 'pry', "~> 0.10"
|
35
|
+
spec.add_development_dependency 'simplecov', '> 0.11'
|
36
|
+
spec.add_development_dependency 'msgpack', "~> 0.5"
|
37
|
+
spec.add_development_dependency 'multi_json', "~> 1.0"
|
38
|
+
spec.add_development_dependency 'oj', "~> 2.0"
|
39
|
+
|
40
|
+
spec.add_runtime_dependency 'protobuf', '~> 3.5'
|
41
|
+
spec.add_runtime_dependency 'active_interaction', '~> 3.0'
|
42
|
+
spec.add_runtime_dependency 'activesupport', '~> 4.0'
|
43
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
syntax = "proto2";
|
2
|
+
|
3
|
+
package protobuf.rpc.messages;
|
4
|
+
|
5
|
+
message RpcCompressedMessage {
|
6
|
+
optional bool compressed = 1 [default = true];
|
7
|
+
optional string response_type = 2;
|
8
|
+
required bytes response_body = 3;
|
9
|
+
|
10
|
+
enum Serializer{
|
11
|
+
RAW = 0;
|
12
|
+
MSGPACK = 1;
|
13
|
+
YAML = 2;
|
14
|
+
JSON = 3;
|
15
|
+
MARSHAL = 4;
|
16
|
+
}
|
17
|
+
optional Serializer serializer = 4 [default = 1];
|
18
|
+
}
|
metadata
ADDED
@@ -0,0 +1,234 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: protobuf-rpc-register
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- scorix
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-05-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec-its
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.2'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.2'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.10'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.10'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: simplecov
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.11'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0.11'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: msgpack
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0.5'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0.5'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: multi_json
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '1.0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '1.0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: oj
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '2.0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '2.0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: protobuf
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - "~>"
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '3.5'
|
146
|
+
type: :runtime
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - "~>"
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '3.5'
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: active_interaction
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - "~>"
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '3.0'
|
160
|
+
type: :runtime
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - "~>"
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '3.0'
|
167
|
+
- !ruby/object:Gem::Dependency
|
168
|
+
name: activesupport
|
169
|
+
requirement: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - "~>"
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '4.0'
|
174
|
+
type: :runtime
|
175
|
+
prerelease: false
|
176
|
+
version_requirements: !ruby/object:Gem::Requirement
|
177
|
+
requirements:
|
178
|
+
- - "~>"
|
179
|
+
- !ruby/object:Gem::Version
|
180
|
+
version: '4.0'
|
181
|
+
description: Register for rpc services using protobuf rpc.
|
182
|
+
email:
|
183
|
+
- scorix@gmail.com
|
184
|
+
executables: []
|
185
|
+
extensions: []
|
186
|
+
extra_rdoc_files: []
|
187
|
+
files:
|
188
|
+
- ".gitignore"
|
189
|
+
- ".rspec"
|
190
|
+
- CODE_OF_CONDUCT.md
|
191
|
+
- Gemfile
|
192
|
+
- LICENSE.txt
|
193
|
+
- README.md
|
194
|
+
- Rakefile
|
195
|
+
- bin/console
|
196
|
+
- bin/setup
|
197
|
+
- lib/protobuf/rpc/clients/base.rb
|
198
|
+
- lib/protobuf/rpc/generator.rb
|
199
|
+
- lib/protobuf/rpc/interactions/base.rb
|
200
|
+
- lib/protobuf/rpc/messages/error.rb
|
201
|
+
- lib/protobuf/rpc/messages/rpc_compressed_message.rb
|
202
|
+
- lib/protobuf/rpc/register.rb
|
203
|
+
- lib/protobuf/rpc/register/version.rb
|
204
|
+
- lib/protobuf/rpc/serializer.rb
|
205
|
+
- lib/protobuf/rpc/services/base.rb
|
206
|
+
- protobuf-rpc-register.gemspec
|
207
|
+
- protos/protobuf/rpc/messages/error.proto
|
208
|
+
- protos/protobuf/rpc/messages/rpc_compressed_message.proto
|
209
|
+
homepage: https://github.com/scorix/protobuf-rpc-register
|
210
|
+
licenses:
|
211
|
+
- MIT
|
212
|
+
metadata:
|
213
|
+
allowed_push_host: https://rubygems.org
|
214
|
+
post_install_message:
|
215
|
+
rdoc_options: []
|
216
|
+
require_paths:
|
217
|
+
- lib
|
218
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
219
|
+
requirements:
|
220
|
+
- - ">="
|
221
|
+
- !ruby/object:Gem::Version
|
222
|
+
version: '0'
|
223
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
224
|
+
requirements:
|
225
|
+
- - ">="
|
226
|
+
- !ruby/object:Gem::Version
|
227
|
+
version: '0'
|
228
|
+
requirements: []
|
229
|
+
rubyforge_project:
|
230
|
+
rubygems_version: 2.5.1
|
231
|
+
signing_key:
|
232
|
+
specification_version: 4
|
233
|
+
summary: Register for rpc services using protobuf rpc.
|
234
|
+
test_files: []
|