teneo_grpc 0.1.5 → 0.1.7

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 175d8a54e092662e21400c371a32748bbb4ded241d7b5a879573626afcdd595a
4
- data.tar.gz: d2dc821e57960d193864c1911e44ac7276fc49908e537ad5381f6edf618ea062
3
+ metadata.gz: d01a40c3bc2bcb7e3e0e15239d29e3105b208c40b8b7ed831d16c461d7a89604
4
+ data.tar.gz: 2970c61f8ca9b54210af598dcd5793e9f5e87894255ba68dd05df4286edec3ff
5
5
  SHA512:
6
- metadata.gz: 9b2b5d31ce39eac6e53afbbcc727078865e3b25aba994370349a6a28a77766bb234b05636c1c3d7549cdf0e8a96479cc1780fd1c0154d1d4e8dc04e3ed80ad73
7
- data.tar.gz: 4f970f30de576c6b6ed6e93a49e4b17da5b8105ca75931444d59e5a8ee721a25c21feae100af4ff882c99c2310352308a18ddc72985a3189b6f950bada55476e
6
+ metadata.gz: 99b0ccac0819bf4b72bd5c7d7283603a057b4d2189e2d6252d2fc4de9f4608e4fa8e5d3357dafd5a8df85d2335aa1d4d2ff0fbc16acbe6e17a2933997f51b55b
7
+ data.tar.gz: 7adeca1870b3ae0ac88f0ce9afda1c09d0e2c6022bb0737222ec295ed2999b44bae287bc55ec258715029c7db4e68b002c958dfdfb52e6c52c91d2d2b388c049
data/Gemfile CHANGED
@@ -3,6 +3,7 @@
3
3
  source 'https://rubygems.org'
4
4
 
5
5
  gem 'debug', require: false # requires libyaml-dev to be installed on system (Ubuntu)
6
+ gem 'dotenv', groups: %i[development test]
6
7
  gem 'grpc'
7
8
  gem 'grpc-tools'
8
9
  gem 'protobuf'
data/Gemfile.lock CHANGED
@@ -25,6 +25,7 @@ GEM
25
25
  irb (~> 1.10)
26
26
  reline (>= 0.3.8)
27
27
  diff-lcs (1.5.1)
28
+ dotenv (3.1.7)
28
29
  drb (2.2.1)
29
30
  google-protobuf (4.29.3)
30
31
  bigdecimal
@@ -117,6 +118,7 @@ PLATFORMS
117
118
 
118
119
  DEPENDENCIES
119
120
  debug
121
+ dotenv
120
122
  grpc
121
123
  grpc-tools
122
124
  protobuf
data/Rakefile CHANGED
@@ -10,16 +10,8 @@ RSpec::Core::RakeTask.new(:spec)
10
10
 
11
11
  RuboCop::RakeTask.new
12
12
 
13
- task default: %w[spec rubocop run]
14
-
15
- task :setup do
16
- sh 'bundle install'
17
- end
13
+ task default: %w[spec rubocop]
18
14
 
19
15
  task :proto do
20
16
  sh 'grpc_tools_ruby_protoc -I proto --ruby_out=lib/teneo/proto --grpc_out=lib/teneo/proto proto/*.proto'
21
17
  end
22
-
23
- task :run do
24
- ruby 'bin/app.rb'
25
- end
data/bin/dotenv ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby3.0
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'dotenv' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
12
+
13
+ bundle_binstub = File.expand_path("bundle", __dir__)
14
+
15
+ if File.file?(bundle_binstub)
16
+ if File.read(bundle_binstub, 300).include?("This file was generated by Bundler")
17
+ load(bundle_binstub)
18
+ else
19
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
20
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
21
+ end
22
+ end
23
+
24
+ require "rubygems"
25
+ require "bundler/setup"
26
+
27
+ load Gem.bin_path("dotenv", "dotenv")
@@ -1,80 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'cloud_event_helpers/cloud_event_helper'
3
+ require_relative 'util/env'
4
+ require_relative 'util/convert'
4
5
  require_relative 'grpc_services/cloud_event_enum'
5
6
  require_relative 'proto/cloud_event_services_pb'
6
7
 
7
8
  # Sends data to gRPC services
8
9
  class Client
9
- attr_accessor :socket
10
+ attr_accessor :socket, :stub
10
11
 
11
12
  def initialize(socket:)
13
+ Env.valid_settings?
14
+
12
15
  @socket = socket
13
16
  @stub = CloudEvents::Stub.new(@socket, :this_channel_is_insecure)
14
17
  end
15
-
16
- # Example function that sends 1 request and receives 1 response
17
- def send_1req_1resp(data:)
18
- puts "sending to socket #{@socket}"
19
-
20
- begin
21
- event = CloudEventHelper.convert(text_data: data)
22
- response = @stub.get_cloud_event(event)
23
- rescue GRPC::Unavailable
24
- puts "ERROR: failed to connect to host '#{@socket}'"
25
- rescue StandardError => e
26
- p e
27
- end
28
-
29
- response
30
- end
31
-
32
- # Example function that sends 1 request and receives a stream of responses
33
- def send_1req_streamresp(data:)
34
- puts "sending to socket #{@socket}"
35
-
36
- begin
37
- event = CloudEventHelper.convert(text_data: data)
38
- responses = @stub.send_cloud_events(event)
39
- rescue GRPC::Unavailable
40
- puts "ERROR: failed to connect to host '#{@socket}'"
41
- rescue StandardError => e
42
- p e
43
- end
44
-
45
- responses
46
- end
47
-
48
- # Example function that sends a stream of requests and receives 1 response
49
- def send_streamreq_1resp(data:)
50
- puts "sending to socket #{@socket}"
51
-
52
- begin
53
- requests = CloudEventEnum.new(data: data, amount: 3)
54
- response = @stub.get_cloud_events(requests.each)
55
- rescue GRPC::Unavailable
56
- puts "ERROR: failed to connect to host '#{@socket}'"
57
- rescue StandardError => e
58
- p e
59
- end
60
-
61
- response
62
- end
63
-
64
- # Example function that sends a stream of requests
65
- # and receives a stream of responses
66
- def send_streamreq_streamresp(data:)
67
- puts "sending to socket #{@socket}"
68
-
69
- begin
70
- requests = CloudEventEnum.new(data: data, amount: 3)
71
- responses = @stub.chat(requests.each)
72
- rescue GRPC::Unavailable
73
- puts "ERROR: failed to connect to host '#{@socket}'"
74
- rescue StandardError => e
75
- p e
76
- end
77
-
78
- responses
79
- end
80
18
  end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This error should get raised when the environment does not provide
4
+ # the required attributes.
5
+ class EnvSettingsError < StandardError
6
+ def initialize(msg = 'ERROR: invalid settings in .env file')
7
+ super(msg)
8
+ end
9
+ end
@@ -2,7 +2,7 @@
2
2
 
3
3
  # This error should get raised when a socket string has an invalid format
4
4
  class SocketFormatError < StandardError
5
- def initialize(msg = 'ERROR: invalid socket')
5
+ def initialize(msg = 'invalid socket')
6
6
  super(msg)
7
7
  end
8
8
  end
@@ -2,24 +2,19 @@
2
2
 
3
3
  require 'securerandom'
4
4
 
5
+ require_relative '../util/convert'
6
+
5
7
  # CloudEventEnum provides an Enumerator of cloud events
6
8
  class CloudEventEnum
7
- def initialize(data:, amount:)
8
- @data = data
9
- @amount = amount
9
+ def initialize(array:)
10
+ @array = array
10
11
  end
11
12
 
12
13
  def each
13
14
  return enum_for(:each) unless block_given?
14
15
 
15
- (1..@amount).each do
16
- yield CloudEvent.new(
17
- id: SecureRandom.uuid,
18
- source: '/server-context',
19
- spec_version: '1.0',
20
- type: 'com.example.server-event',
21
- text_data: @data
22
- )
16
+ @array.each do |element|
17
+ yield Convert.text_to_cloudevent(data: element)
23
18
  end
24
19
  end
25
20
  end
@@ -2,15 +2,17 @@
2
2
 
3
3
  require_relative 'client'
4
4
  require_relative 'service_orchestrator'
5
- require_relative 'grpc_services/cloud_events_service'
5
+ require_relative 'util/env'
6
6
 
7
7
  # Encapsulating class for all gRPC communication
8
8
  class Server
9
9
  attr_accessor :socket, :remote_hosts, :clients
10
10
 
11
- def initialize(socket:, remote_hosts:)
12
- @socket = socket
13
- @remote_hosts = remote_hosts
11
+ def initialize
12
+ Env.valid_settings?
13
+
14
+ @socket = "#{ENV["LISTEN_ADDRESS"]}:#{ENV["PORT"]}"
15
+ @remote_hosts = ENV['REMOTE_HOSTS'].split('|')
14
16
 
15
17
  @clients = []
16
18
  @remote_hosts&.each do |remote|
@@ -20,7 +22,10 @@ class Server
20
22
 
21
23
  @orchestrator = ServiceOrchestrator.new(socket: @socket)
22
24
  @orchestrator.initialize_listener
23
- @orchestrator.add(CloudEventsService.new(socket: @socket, clients: @clients))
25
+ end
26
+
27
+ def register_service(service:)
28
+ @orchestrator.add(service)
24
29
  end
25
30
 
26
31
  def start_listening_for_data
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'securerandom'
4
+
5
+ require_relative '../proto/cloud_event_services_pb'
6
+ require_relative '../grpc_services/cloud_event_enum'
7
+
8
+ # The Convert module provides functionality on custom data conversion operations.
9
+ module Convert
10
+ def self.text_to_cloudevent(data:)
11
+ source = ENV['CLOUDEVENTS_SOURCE']
12
+ source = "#{ENV["LISTEN_ADDRESS"]}:#{ENV["PORT"]}" if source.nil?
13
+
14
+ CloudEvent.new(
15
+ id: SecureRandom.uuid,
16
+ source: source,
17
+ spec_version: ENV['CLOUDEVENTS_SPEC_VERSION'],
18
+ type: ENV['CLOUDEVENTS_TYPE_PREFIX'],
19
+ text_data: data
20
+ )
21
+ end
22
+
23
+ def self.array_to_cloudevent_enum(array:)
24
+ CloudEventEnum.new(array: array)
25
+ end
26
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'securerandom'
4
+
5
+ require_relative '../error_classes/env_settings_error'
6
+ require_relative '../error_classes/socket_format_error'
7
+
8
+ # The Env module provides functionality for env variable settings.
9
+ module Env
10
+ def self.valid_settings?
11
+ mandatory_settings = %w[LISTEN_ADDRESS PORT CLOUDEVENTS_SPEC_VERSION CLOUDEVENTS_TYPE_PREFIX]
12
+ mandatory_settings.each do |setting|
13
+ raise EnvSettingsError, "environment variable '#{setting}' not found" if ENV[setting].nil?
14
+ end
15
+
16
+ socket = "#{ENV["LISTEN_ADDRESS"]}:#{ENV["PORT"]}"
17
+ raise SocketFormatError unless socket =~ /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}:\d{1,5}$/
18
+ end
19
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: teneo_grpc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ruben Vanoverschelde
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-02-24 00:00:00.000000000 Z
11
+ date: 2025-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: grpc
@@ -38,6 +38,7 @@ files:
38
38
  - Rakefile
39
39
  - bin/bundle
40
40
  - bin/console
41
+ - bin/dotenv
41
42
  - bin/grpc_tools_ruby_protoc
42
43
  - bin/grpc_tools_ruby_protoc_plugin
43
44
  - bin/htmldiff
@@ -58,15 +59,15 @@ files:
58
59
  - bin/thor
59
60
  - lib/teneo_grpc.rb
60
61
  - lib/teneo_grpc/client.rb
61
- - lib/teneo_grpc/cloud_event_helpers/cloud_event_helper.rb
62
+ - lib/teneo_grpc/error_classes/env_settings_error.rb
62
63
  - lib/teneo_grpc/error_classes/socket_format_error.rb
63
- - lib/teneo_grpc/grpc_services/cloud_event_chat_enum.rb
64
64
  - lib/teneo_grpc/grpc_services/cloud_event_enum.rb
65
- - lib/teneo_grpc/grpc_services/cloud_events_service.rb
66
65
  - lib/teneo_grpc/proto/cloud_event_pb.rb
67
66
  - lib/teneo_grpc/proto/cloud_event_services_pb.rb
68
67
  - lib/teneo_grpc/server.rb
69
68
  - lib/teneo_grpc/service_orchestrator.rb
69
+ - lib/teneo_grpc/util/convert.rb
70
+ - lib/teneo_grpc/util/env.rb
70
71
  homepage: https://github.com/libis/teneo-grpc_skeleton_ruby
71
72
  licenses:
72
73
  - MIT
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'securerandom'
4
-
5
- require_relative '../proto/cloud_event_services_pb'
6
-
7
- # Helper module for all thing CloudEvent related
8
- module CloudEventHelper
9
- def self.convert(text_data:)
10
- CloudEvent.new(
11
- id: SecureRandom.uuid,
12
- source: '/context',
13
- spec_version: '1.0',
14
- type: 'com.example.event',
15
- text_data: text_data
16
- )
17
- end
18
- end
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../cloud_event_helpers/cloud_event_helper'
4
-
5
- # CloudEventChatEnum provides an Enumerator for bidirectional communication
6
- class CloudEventChatEnum
7
- def initialize(requests:, default_response:)
8
- @requests = requests
9
- @default_response = default_response
10
- end
11
-
12
- def each
13
- return enum_for(:each) unless block_given?
14
-
15
- @requests.each do |request|
16
- puts "Received request: '#{request}'"
17
- yield CloudEventHelper.convert(text_data: @default_response)
18
- # Pause 1 second between messages to show how the messages get exchanged
19
- sleep 1
20
- end
21
- end
22
- end
@@ -1,74 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../cloud_event_helpers/cloud_event_helper'
4
- require_relative '../proto/cloud_event_services_pb'
5
- require_relative 'cloud_event_chat_enum'
6
- require_relative 'cloud_event_enum'
7
-
8
- # Implements the CloudEvents service defined in proto/cloud_event.proto
9
- class CloudEventsService < CloudEvents::Service
10
- def initialize(socket:, clients:) # rubocop:disable Lint/MissingSuper
11
- @socket = socket
12
- @message = "ping from #{@socket}"
13
- @clients = clients
14
- end
15
-
16
- def get_cloud_event(request, _call)
17
- puts "Received request: '#{request}'"
18
-
19
- # Forward request to other services if message equals "forward"
20
- if request.text_data == 'forward'
21
- @clients.each do |client|
22
- response = client.send_1req_1resp(data: @message)
23
- puts "Reply from: '#{response}'"
24
- end
25
- end
26
-
27
- CloudEventHelper.convert(text_data: @message)
28
- end
29
-
30
- def send_cloud_events(request, _call)
31
- puts "Received request: '#{request}'"
32
-
33
- # Forward request to other services if message equals "forward"
34
- if request.text_data == 'forward'
35
- @clients.each do |client|
36
- responses = client.send_1req_streamresp(data: @message)
37
-
38
- responses.each do |response|
39
- puts "Reply from: '#{response}'"
40
- end
41
- end
42
- end
43
-
44
- CloudEventEnum.new(data: @message, amount: 3).each
45
- end
46
-
47
- def forward_to_services?(call)
48
- forward = true
49
- call.each_remote_read do |request|
50
- puts "Received request: '#{request}'"
51
- forward = false unless request.text_data == 'forward'
52
- end
53
-
54
- forward
55
- end
56
-
57
- def get_cloud_events(call)
58
- forward = forward_to_services?(call)
59
-
60
- # Forward request to other services if message equals "forward"
61
- if forward
62
- @clients.each do |client|
63
- response = client.send_streamreq_1resp(data: @message)
64
- puts "Reply from: '#{response}'"
65
- end
66
- end
67
-
68
- CloudEventHelper.convert(text_data: @message)
69
- end
70
-
71
- def chat(call)
72
- CloudEventChatEnum.new(requests: call, default_response: @message).each
73
- end
74
- end