bmx-event_hook_client_server 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (29) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +11 -0
  3. data/Gemfile.lock +80 -0
  4. data/event_hook_client_server.gemspec +65 -0
  5. data/lib/event_hook_client_server.rb +13 -0
  6. data/lib/event_hook_client_server/client.rb +177 -0
  7. data/lib/event_hook_client_server/client/models/error.rb +16 -0
  8. data/lib/event_hook_client_server/client/models/integration.rb +25 -0
  9. data/lib/event_hook_client_server/client/models/integrations/binding.rb +21 -0
  10. data/lib/event_hook_client_server/client/response.rb +43 -0
  11. data/lib/event_hook_client_server/errors.rb +5 -0
  12. data/lib/event_hook_client_server/protobuf/event_hook_client_server/v1/enums/integrator_pb.rb +20 -0
  13. data/lib/event_hook_client_server/protobuf/event_hook_client_server/v1/messages/error_pb.rb +23 -0
  14. data/lib/event_hook_client_server/protobuf/event_hook_client_server/v1/messages/integration_pb.rb +107 -0
  15. data/lib/event_hook_client_server/protobuf/event_hook_client_server/v1/messages/pagination_pb.rb +35 -0
  16. data/lib/event_hook_client_server/protobuf/event_hook_client_server/v1/services/webhook_configurator_pb.rb +17 -0
  17. data/lib/event_hook_client_server/protobuf/event_hook_client_server/v1/services/webhook_configurator_services_pb.rb +38 -0
  18. data/lib/event_hook_client_server/refinements/hash/bury.rb +19 -0
  19. data/lib/event_hook_client_server/refinements/hash/transmute.rb +27 -0
  20. data/lib/event_hook_client_server/refinements/object/bubbling_const_lookup.rb +60 -0
  21. data/lib/event_hook_client_server/refinements/protobuf/hashable_struct_values.rb +28 -0
  22. data/lib/event_hook_client_server/refinements/protobuf/nullable_values.rb +122 -0
  23. data/lib/event_hook_client_server/server.rb +100 -0
  24. data/lib/event_hook_client_server/server/request_processor.rb +61 -0
  25. data/lib/event_hook_client_server/service.rb +9 -0
  26. data/lib/event_hook_client_server/services/grpc/struct_encoder.rb +62 -0
  27. data/lib/event_hook_client_server/types.rb +9 -0
  28. data/lib/event_hook_client_server/version.rb +8 -0
  29. metadata +143 -0
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'event_hook_client_server/protobuf/event_hook_client_server/v1/services/webhook_configurator_services_pb'
4
+
5
+ module EventHookClientServer
6
+ class Server < EventHookClientServer::V1::Services::EventHook::Service
7
+ def self.generic_rpc(name, options = {})
8
+ [
9
+ [:create_],
10
+ [:update_],
11
+ [:destroy_],
12
+ [],
13
+ [nil, :s]
14
+ ].compact.each do |prefix, suffix|
15
+ method_name = [prefix, name, suffix].compact.join
16
+ handler_name = "#{method_name}_handler".to_sym
17
+
18
+ attr_accessor handler_name
19
+
20
+ # NOTE: this is ugly, but it's (performance wise) much faster than
21
+ # define_method
22
+ method_definition = <<-RUBY
23
+ def #{method_name}(input, request)
24
+ process_request(:#{method_name}, input, request)
25
+ end
26
+ RUBY
27
+
28
+ class_eval(method_definition, __FILE__, __LINE__ - 5)
29
+ end
30
+ end
31
+
32
+ attr_accessor :logger
33
+ attr_accessor :handler
34
+
35
+ def initialize(*args, logger: nil)
36
+ @logger = logger || self.logger
37
+ super(*args)
38
+ end
39
+
40
+ def start!(binding_address: '0.0.0.0:3000', logger: nil)
41
+ self.logger = logger unless logger.nil?
42
+
43
+ self.server = ::GRPC::RpcServer.new
44
+ server.add_http2_port(binding_address, :this_port_is_insecure)
45
+
46
+ self.logger
47
+ .info("EventHook SERVER starting on '#{binding_address}'")
48
+
49
+ server.handle(self)
50
+ server.run_till_terminated
51
+ end
52
+
53
+ def stop!
54
+ return unless server
55
+ server.stop
56
+ end
57
+
58
+ def logger
59
+ @logger ||= Logger.new(STDOUT)
60
+ end
61
+
62
+ generic_rpc :integration
63
+
64
+ attr_accessor :find_integration_from_headers_handler
65
+ def find_integration_from_headers(input, request)
66
+ process_request(:find_integration_from_headers, input, request)
67
+ end
68
+
69
+ protected
70
+
71
+ attr_accessor :server
72
+
73
+ private
74
+
75
+ def process_request(method, input, request)
76
+ RequestProcessor.call(
77
+ method,
78
+ input,
79
+ request,
80
+ determine_handler(method),
81
+ logger
82
+ )
83
+ end
84
+
85
+ def determine_handler(method)
86
+ handlers[method] ||=
87
+ if handler
88
+ handler.method(method)
89
+ else
90
+ send("#{method}_handler")
91
+ end
92
+ end
93
+
94
+ def handlers
95
+ @handlers ||= {}
96
+ end
97
+ end
98
+ end
99
+
100
+ require 'event_hook_client_server/server/request_processor'
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+ require 'securerandom'
5
+ require 'event_hook_client_server/service'
6
+
7
+ module EventHookClientServer
8
+ class Server
9
+ class RequestProcessor < EventHookClientServer::Service
10
+ attr_reader :method
11
+ attr_reader :input
12
+ attr_reader :request
13
+ attr_reader :handler
14
+ attr_reader :logger
15
+ attr_reader :id
16
+
17
+ def initialize(method, input, request, handler, logger)
18
+ @method = method
19
+ @input = input
20
+ @request = request
21
+ @handler = handler
22
+ @logger = logger
23
+ @id = SecureRandom.uuid
24
+ end
25
+
26
+ def call
27
+ start_timer!
28
+ log_start
29
+ result = handler.call(input, request)
30
+ log_end
31
+ result
32
+ end
33
+
34
+ private
35
+
36
+ attr_reader :start_time
37
+
38
+ def log_start
39
+ logger&.info("REQUEST [#{id}] #{method}")
40
+ end
41
+
42
+ def log_end
43
+ logger&.info(
44
+ "PROCESSED [#{id}] #{method} in #{request_duration}ms"
45
+ )
46
+ end
47
+
48
+ def start_timer!
49
+ @start_time = current_time
50
+ end
51
+
52
+ def current_time
53
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
54
+ end
55
+
56
+ def request_duration
57
+ ((current_time - start_time) * 1000).round(2)
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EventHookClientServer
4
+ class Service
5
+ def self.call(*args)
6
+ new(*args).call
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EventHookClientServer
4
+ module GRPC
5
+ class StructEncoder < Service
6
+ class Error < StandardError; end
7
+ class NonEncodeableObjectError < Error; end
8
+ class NonPrimitivValueError < Error; end
9
+
10
+ attr_reader :object
11
+
12
+ def initialize(object)
13
+ @object = object
14
+ end
15
+
16
+ def call
17
+ return if object.nil?
18
+
19
+ cast_hash(hash_value)
20
+ end
21
+
22
+ private
23
+
24
+ def hash_value
25
+ object.to_h
26
+ rescue NoMethoderror => error
27
+ raise(NonEncodeableObjectError, error)
28
+ end
29
+
30
+ def cast_hash(hash)
31
+ {
32
+ fields: hash.each_with_object({}) do |(key, value), struct|
33
+ struct[key.to_s] = cast_value(value)
34
+ end
35
+ }
36
+ end
37
+
38
+ def cast_value(value)
39
+ Google::Protobuf::Value.new(wrap_value(value))
40
+ end
41
+
42
+ def wrap_value(value)
43
+ case value
44
+ when nil then { null_value: value }
45
+ when Numeric then { number_value: value }
46
+ when String then { string_value: value }
47
+ when TrueClass then { bool_value: value }
48
+ when FalseClass then { bool_value: value }
49
+ when Array then { list_value: cast_array(value) }
50
+ else
51
+ raise(NonPrimitivValueError, value) unless value.respond_to?(:to_h)
52
+
53
+ { struct_value: cast_hash(value.to_h) }
54
+ end
55
+ end
56
+
57
+ def cast_array(array)
58
+ { values: array.map(&method(:cast_value)) }
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry-struct'
4
+
5
+ module EventHookClientServer
6
+ module Types
7
+ include Dry.Types()
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EventHookClientServer
4
+ MAJOR = 1
5
+ MINOR = 0
6
+ PATCH = 1
7
+ VERSION = [MAJOR, MINOR, PATCH].join('.')
8
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bmx-event_hook_client_server
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Stanko K.R.
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-09-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-struct
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: grpc
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: Client and server implementation for the Webhook Configurator service.
84
+ email:
85
+ - me@stanko.io
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - Gemfile
91
+ - Gemfile.lock
92
+ - event_hook_client_server.gemspec
93
+ - lib/event_hook_client_server.rb
94
+ - lib/event_hook_client_server/client.rb
95
+ - lib/event_hook_client_server/client/models/error.rb
96
+ - lib/event_hook_client_server/client/models/integration.rb
97
+ - lib/event_hook_client_server/client/models/integrations/binding.rb
98
+ - lib/event_hook_client_server/client/response.rb
99
+ - lib/event_hook_client_server/errors.rb
100
+ - lib/event_hook_client_server/protobuf/event_hook_client_server/v1/enums/integrator_pb.rb
101
+ - lib/event_hook_client_server/protobuf/event_hook_client_server/v1/messages/error_pb.rb
102
+ - lib/event_hook_client_server/protobuf/event_hook_client_server/v1/messages/integration_pb.rb
103
+ - lib/event_hook_client_server/protobuf/event_hook_client_server/v1/messages/pagination_pb.rb
104
+ - lib/event_hook_client_server/protobuf/event_hook_client_server/v1/services/webhook_configurator_pb.rb
105
+ - lib/event_hook_client_server/protobuf/event_hook_client_server/v1/services/webhook_configurator_services_pb.rb
106
+ - lib/event_hook_client_server/refinements/hash/bury.rb
107
+ - lib/event_hook_client_server/refinements/hash/transmute.rb
108
+ - lib/event_hook_client_server/refinements/object/bubbling_const_lookup.rb
109
+ - lib/event_hook_client_server/refinements/protobuf/hashable_struct_values.rb
110
+ - lib/event_hook_client_server/refinements/protobuf/nullable_values.rb
111
+ - lib/event_hook_client_server/server.rb
112
+ - lib/event_hook_client_server/server/request_processor.rb
113
+ - lib/event_hook_client_server/service.rb
114
+ - lib/event_hook_client_server/services/grpc/struct_encoder.rb
115
+ - lib/event_hook_client_server/types.rb
116
+ - lib/event_hook_client_server/version.rb
117
+ homepage: https://github.com/runslikebutter/event_hook_client_server
118
+ licenses: []
119
+ metadata:
120
+ allowed_push_host: https://rubygems.org
121
+ homepage_uri: https://github.com/runslikebutter/event_hook_client_server
122
+ source_code_uri: https://github.com/runslikebutter/event_hook_client_server
123
+ changelog_uri: https://github.com/runslikebutter/event_hook_client_server/blob/master/CHANGELOG.md
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubygems_version: 3.0.1
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: Client and server implementation for the Webhook Configurator service.
143
+ test_files: []