twirp_rails 0.1.3 → 0.1.4

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: f76a67c6644535dc835e68bb6b47b28661dca6922b9733653f0480e297f65bb1
4
- data.tar.gz: f0a855d0cb9d1a0b693e5b11c03d7bdca9486ff12bf82ac103298bfdf67dfe88
3
+ metadata.gz: 6a7d8c6c8308b619d98a8719ec069a0896ba0997145646dec8d3be46b92bd3f5
4
+ data.tar.gz: f3211a52b907e61e49f546f5f34202b5a4e870b56d23f72fca8a28c877277a2b
5
5
  SHA512:
6
- metadata.gz: 22b8a3a2c75e2a5084a5184c1137726ce3f8978f111a2cd1749028492276b518ab2c03b19e799d9ec858023fe51ce01d79f6d7702357e761ec555e90ac832a5b
7
- data.tar.gz: 87bf3f8cc09b9694cdc1f949de56d38f1a6283bc2d6237b0e06f75b15266c4009015a13efa6f74966623d01db2773cc3e4a51fb7bce17401b14830f78c795d4d
6
+ metadata.gz: b8bc55ff11f35137d883335e92b0d49e50d7bafa75e5a9c7dab9f1632366a71379ff88093709dcccc08b2a2f643c9c2f637448cc6101b898f8c2faa7dd1c22f1
7
+ data.tar.gz: e94cff9b2181e4a46343f2a3f0be10fdee43ab88217ca91d01626375f721adffdf39237beab652b8c6c1cd8760abb1f110d68ab6cc92a024437093f2154d2e13
data/CHANGELOG.md CHANGED
@@ -4,11 +4,31 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
5
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
- ## 0.1.3 - 2020-01-28
7
+ ## 0.1.4 - 2020-02-03
8
8
 
9
9
  ### Added
10
10
  - convert package.message type to Package::Message
11
11
 
12
+ ## 0.1.3 - 2020-01-28
13
+
14
+ ### Added
15
+ - ```to_twirp``` and ```twirp_message``` methods to easy convert active record models to protobuf DTOs
16
+ - rspec helper to test twirp handlers with ```rpc``` helper
17
+ ```ruby
18
+ config.include TwirpRails::RSpec::Helper, type: :rpc, file_path: %r{spec/rpc}
19
+ # ...
20
+
21
+ describe TeamsHandler do
22
+ let!(:team) { create :team }
23
+
24
+ context '.get' do
25
+ rpc { [:get, id: team.id] }
26
+
27
+ it { should match(team: team.to_twirp.symbolize_keys) }
28
+ end
29
+ end
30
+ ```
31
+
12
32
  ### Fixed
13
33
  - twirp_ruby missing module workaround https://github.com/twitchtv/twirp-ruby/issues/48
14
34
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- twirp_rails (0.1.3)
4
+ twirp_rails (0.1.4)
5
5
  railties (~> 6.0)
6
6
  twirp (~> 1)
7
7
 
@@ -0,0 +1,58 @@
1
+ module TwirpRails
2
+ module ActiveRecordExtension
3
+ extend ActiveSupport::Concern
4
+
5
+ class_methods do
6
+ # Using to set twirp class used by to_twirp method
7
+ # @example
8
+ # twirp_message TwirpModel
9
+ def twirp_message(message_class)
10
+ @twirp_message = message_class
11
+ end
12
+
13
+ def twirp_message_class
14
+ @twirp_message = @twirp_message.constantize if @twirp_message.is_a?(String)
15
+
16
+ @twirp_message
17
+ end
18
+
19
+ def to_twirp(*args)
20
+ all.map { |entity| entity.to_twirp(*args) }
21
+ end
22
+ end
23
+
24
+ # Converts to twirp hash,
25
+ # @return [Hash] of attributes
26
+ # used by relation method to_twirp
27
+ # @param [Array|Class] fields_or_class - array of converted fields or message class to
28
+ def to_twirp(*fields_or_class)
29
+ fields = fields_or_class
30
+ result = attributes
31
+
32
+ if fields.empty? && self.class.twirp_message_class
33
+ fields = [self.class.twirp_message_class]
34
+ end
35
+
36
+ if fields.one? && fields.first.is_a?(Class)
37
+ message_class = fields.first
38
+
39
+ unless message_class.respond_to?(:descriptor)
40
+ raise "Class #{message_class} must me a protobuf message class"
41
+ end
42
+
43
+ # TODO performance optimization needed
44
+ fields = message_class.descriptor.map &:name
45
+
46
+ result = result.slice(*fields)
47
+ elsif fields.any?
48
+ result = result.slice(*fields)
49
+ end
50
+
51
+ result
52
+ end
53
+ end
54
+ end
55
+
56
+ if defined? ActiveRecord::Base
57
+ ActiveRecord::Base.include TwirpRails::ActiveRecordExtension
58
+ end
@@ -8,7 +8,6 @@ class TwirpGenerator < Rails::Generators::NamedBase
8
8
 
9
9
  def check_requirements
10
10
  in_root do
11
- puts `pwd`
12
11
  unless File.exists?(proto_file_name)
13
12
  raise "#{proto_file_name} not found #{`pwd`} #{`ls`}"
14
13
  end
@@ -64,7 +63,7 @@ class TwirpGenerator < Rails::Generators::NamedBase
64
63
  end
65
64
 
66
65
  def generate_rspec_files
67
- puts 'RSpec TODO'
66
+ # TODO
68
67
  end
69
68
 
70
69
  private
@@ -73,14 +72,6 @@ class TwirpGenerator < Rails::Generators::NamedBase
73
72
  result_type.split('.').map(&:camelize).join('::')
74
73
  end
75
74
 
76
- def from_rails_root(&block)
77
- old_dir = Dir.pwd
78
- Dir.chdir Rails.root
79
- yield
80
- ensure
81
- Dir.chdir old_dir
82
- end
83
-
84
75
  def protoc_cmd(files)
85
76
  FileUtils.mkdir_p 'lib/twirp'
86
77
  flags = "--proto_path=app/protos --ruby_out=lib/twirp --twirp_ruby_out=lib/twirp --plugin=#{PLUGIN_PATH}"
@@ -0,0 +1,56 @@
1
+ module TwirpRails
2
+ module RSpec
3
+ module Helper
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+ # places to subject service method returned value converted to_h
8
+ # @param [Boolean] to_h - default true, set to false to use pure result
9
+ # @param [Proc] block - should return array with method name and arguments
10
+ # @example
11
+ # rpc { [:get, id: 1] }
12
+ # it { should match(id: 1)}
13
+ def rpc(to_h: true, &block)
14
+ let :rpc_args, &block
15
+ subject do
16
+ result = service.call_rpc(*rpc_args)
17
+ to_h ? result.to_h : result
18
+ end
19
+ end
20
+
21
+ def service_class_from_describe
22
+ result = metadata[:service]
23
+
24
+ result = result.constantize if result && !result.is_a?(Class)
25
+
26
+ result
27
+ end
28
+
29
+ def infer_service_class
30
+ service_class_name = described_class.name.gsub(/Handler$/, '') + 'Service'
31
+
32
+ service_class_name.constantize
33
+ rescue NameError
34
+ msg = "Cannot infer Service class from handler #{described_class.name}."
35
+ msg += " Inferred name #{service_class_name}" if service_class_name
36
+
37
+ raise msg
38
+ end
39
+
40
+ def service_class
41
+ @service_class ||=
42
+ service_class_from_describe || infer_service_class
43
+ end
44
+ end
45
+
46
+ def service_class
47
+ self.class.service_class
48
+ end
49
+
50
+ included do
51
+ let(:handler) { described_class.new }
52
+ let(:service) { service_class.new(handler) }
53
+ end
54
+ end
55
+ end
56
+ end
@@ -1,3 +1,3 @@
1
1
  module TwirpRails
2
- VERSION = '0.1.3'.freeze
2
+ VERSION = '0.1.4'.freeze
3
3
  end
data/lib/twirp_rails.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require 'twirp_rails/version'
4
4
  require 'twirp_rails/engine'
5
5
  require 'twirp_rails/generators/twirp/twirp_generator'
6
+ require 'twirp_rails/active_record_extension'
6
7
 
7
8
  module TwirpRails
8
9
  class Error < StandardError; end
data/twirp_rails.gemspec CHANGED
@@ -16,7 +16,7 @@ Gem::Specification.new do |spec|
16
16
  # Specify which files should be added to the gem when it is released.
17
17
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
18
  spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(\.cicleci|bin|test|spec|features)/}) }
20
20
  end
21
21
  spec.bindir = 'exe'
22
22
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twirp_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexandr Zimin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-01-28 00:00:00.000000000 Z
11
+ date: 2020-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: twirp
@@ -139,14 +139,13 @@ files:
139
139
  - LICENSE.txt
140
140
  - README.md
141
141
  - Rakefile
142
- - bin/console
143
- - bin/push
144
- - bin/setup
145
142
  - lib/twirp_rails.rb
143
+ - lib/twirp_rails/active_record_extension.rb
146
144
  - lib/twirp_rails/engine.rb
147
145
  - lib/twirp_rails/generators/twirp/USAGE
148
146
  - lib/twirp_rails/generators/twirp/twirp_generator.rb
149
147
  - lib/twirp_rails/routes.rb
148
+ - lib/twirp_rails/rspec/helper.rb
150
149
  - lib/twirp_rails/twirp.rb
151
150
  - lib/twirp_rails/version.rb
152
151
  - twirp_rails.gemspec
data/bin/console DELETED
@@ -1,14 +0,0 @@
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/push DELETED
@@ -1,10 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -v
5
-
6
- rspec
7
-
8
- gem build twirp_rails.gemspec
9
-
10
- gem push twirp_rails-$(ruby -r./lib/twirp_rails/version.rb -e "print TwirpRails::VERSION").gem
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here