twirp_rails 0.1.7 → 0.2.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: 2a92255521f3ac2df3ae47a597caa01c1815a03c60a5ab0b7e8d459162a32dd9
4
- data.tar.gz: 7c6bc605775f5bbf1fafa1292bd116be996ab536a602335bc83c0b43cdedf529
3
+ metadata.gz: b29d60d23232039677f98e1adc290f50536ceba50a1175ff133739100c04321f
4
+ data.tar.gz: 25583997b7634a2ee6028b2faa3de4a7ad8d2ba5a9cd4973ec8f54805b0e07d0
5
5
  SHA512:
6
- metadata.gz: b515cb51dd041ded6089f55fde625cc8b52c23b3526179a102ef4145e6c12f38953c052cdf5fbc987fe0c177acc938fc959128d8f1cab5dc87ef8ac6800118fb
7
- data.tar.gz: 12b4c238b0ce754a205b84d49f00fb3edef603fe0152a49bc08d6b565886f59cc46932c6808824bbd9e678aeb00e22b45197ef1757b394de22827c2c36104ee2
6
+ metadata.gz: eea47f4d6fcd5b910a5d3a6b7803946cd971a6e357fc530e5aebc5ceb0b6eee5a385513c1ff1aa78e52ec32f6aa6acb561176c908d37faa0ef3467a1d7dc2110
7
+ data.tar.gz: 25d5c11dbba73159d0a08b30706e9d880ef1d92a81f8c4ce1183d810e83ba2b5be7ef21b316f64ebabc05cec1c646048c20122194ac7ba632f83184edc81ca18
data/CHANGELOG.md CHANGED
@@ -4,6 +4,19 @@ 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.2.0 - 2020-02-21
8
+
9
+ ### Breaking changes
10
+ - `mount_twirp` now (by default) mounts to path /twirp/Service instead of /Service. If you want to use old
11
+ behavior add `scope: nil` argument.
12
+
13
+ ### Added
14
+ - Services mounted by `mount_twirp` now correctly report errors to `Raven` (if `raven` gem used) and instrument
15
+ calls via `ActiveSupport::Notifications`.
16
+
17
+ ### Changed
18
+ - initial install rspec helper code moved to the ```twirp:rspec``` generator.
19
+
7
20
  ## 0.1.7 - 2020-02-11
8
21
 
9
22
  ### Changed
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- twirp_rails (0.1.7)
4
+ twirp_rails (0.2.0)
5
5
  railties (~> 6.0)
6
6
  twirp (~> 1)
7
7
 
@@ -1,6 +1,8 @@
1
1
  require 'rails/engine'
2
2
  require 'twirp_rails/routes'
3
3
  require 'twirp_rails/twirp'
4
+ require 'twirp_rails/logging_adapter'
5
+ require 'twirp_rails/raven_adapter'
4
6
 
5
7
  module TwirpRails
6
8
  module Routes
@@ -9,6 +11,14 @@ module TwirpRails
9
11
  TwirpRails::Routes::Helper.install
10
12
  end
11
13
 
14
+ initializer 'twirp_rails.raven' do
15
+ TwirpRails::RavenAdapter.install
16
+ end
17
+
18
+ initializer 'twirp_rails.logging' do
19
+ TwirpRails::LoggingAdapter.install
20
+ end
21
+
12
22
  initializer 'twirp_rails.require_generated_files' do
13
23
  TwirpRails::Twirp.auto_require_twirp_files
14
24
  end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+ require 'twirp_rails/routes'
3
+
4
+ module TwirpRails
5
+ module LoggingAdapter # :nodoc:
6
+ def self.install
7
+ return unless defined?(ActiveSupport::Notifications)
8
+
9
+ TwirpRails::Routes::Helper.on_create_service do |service|
10
+ LoggingAdapter.instrument service
11
+ end
12
+ end
13
+
14
+ def self.instrument(service)
15
+ instrumenter = ActiveSupport::Notifications.instrumenter
16
+
17
+ service.before do |rack_env, env|
18
+ payload = {
19
+ rack_env: rack_env,
20
+ env: env
21
+ }
22
+ instrumenter.start 'instrumenter.twirp', payload
23
+ end
24
+
25
+ service.on_error do |_twerr, _env|
26
+ instrumenter.finish 'instrumenter.twirp', nil
27
+ end
28
+
29
+ service.on_success do |_env|
30
+ instrumenter.finish 'instrumenter.twirp', nil
31
+ end
32
+
33
+ service.exception_raised do |e, env|
34
+ env[:exception] = {
35
+ class: e.class,
36
+ message: e.message,
37
+ backtrace: e.backtrace.join("\n")
38
+ }
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+ require 'twirp_rails/routes'
3
+
4
+ module TwirpRails
5
+ module RavenAdapter # :nodoc:
6
+ def self.install
7
+ return unless defined?(::Raven)
8
+
9
+ TwirpRails::Routes::Helper.on_create_service do |service|
10
+ RavenAdapter.attach service
11
+ end
12
+ end
13
+
14
+ def self.attach(service)
15
+ service.exception_raised do |e, _env|
16
+ ::Raven.capture_exception e
17
+ end
18
+ end
19
+ end
20
+ end
@@ -3,7 +3,7 @@ require 'action_dispatch'
3
3
  module TwirpRails
4
4
  module Routes # :nodoc:
5
5
  module Helper
6
- def mount_twirp(name, handler: nil)
6
+ def mount_twirp(name, handler: nil, scope: 'twirp')
7
7
  case name
8
8
  when Class
9
9
  raise 'handler param required when name is a class' unless handler&.is_a?(Class)
@@ -11,7 +11,10 @@ module TwirpRails
11
11
  service_class = name
12
12
 
13
13
  when String, Symbol
14
- service_class = "#{name}_service".camelize.constantize rescue name.camelize.constantize
14
+ service_class = Helper.constantize_first "#{name}_service", name
15
+
16
+ raise "#{name.camelize}Service or #{name.camelize} is not found" unless service_class
17
+
15
18
  handler ||= "#{name}_handler".camelize.constantize
16
19
 
17
20
  else
@@ -19,11 +22,44 @@ module TwirpRails
19
22
  end
20
23
 
21
24
  service = service_class.new(handler.new)
22
- mount service, at: service.full_name
25
+ Helper.run_create_hooks service
26
+
27
+ if scope
28
+ scope scope do
29
+ mount service, at: service.full_name
30
+ end
31
+ else
32
+ mount service, at: service.full_name
33
+ end
34
+ end
35
+
36
+ def self.constantize_first(*variants)
37
+ variants.each do |name|
38
+ clazz = name.to_s.camelize.constantize
39
+
40
+ return clazz if clazz
41
+ end
42
+
43
+ nil
23
44
  end
24
45
 
25
46
  def self.install
26
- ActionDispatch::Routing::Mapper.send :include, TwirpRails::Routes::Helper
47
+ ActionDispatch::Routing::Mapper.include TwirpRails::Routes::Helper
48
+ end
49
+
50
+ cattr_accessor :create_service_hooks
51
+
52
+ def self.on_create_service(&block)
53
+ Helper.create_service_hooks ||= []
54
+ Helper.create_service_hooks << block
55
+ end
56
+
57
+ def self.run_create_hooks(service)
58
+ return unless Helper.create_service_hooks
59
+
60
+ Helper.create_service_hooks.each do |hook|
61
+ hook.call service
62
+ end
27
63
  end
28
64
  end
29
65
  end
@@ -1,3 +1,3 @@
1
1
  module TwirpRails
2
- VERSION = '0.1.7'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
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.7
4
+ version: 0.2.0
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-02-12 00:00:00.000000000 Z
11
+ date: 2020-02-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: twirp
@@ -145,6 +145,8 @@ files:
145
145
  - lib/twirp_rails/generators/twirp/USAGE
146
146
  - lib/twirp_rails/generators/twirp/twirp_generator.rb
147
147
  - lib/twirp_rails/generators/twirp/twirp_rspec_generator.rb
148
+ - lib/twirp_rails/logging_adapter.rb
149
+ - lib/twirp_rails/raven_adapter.rb
148
150
  - lib/twirp_rails/routes.rb
149
151
  - lib/twirp_rails/rspec/helper.rb
150
152
  - lib/twirp_rails/twirp.rb