rubypitaya 3.4.2 → 3.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 782f0fbd940b77a8ec951dec8df3f63cf673feddc6a68219829c5a7a1d9883d4
4
- data.tar.gz: 9d107b921e8dabe4d410eae3025d6bd187c61718bf3ee3bc69a7cf449edd0516
3
+ metadata.gz: bea764ce3cb4595677dc5ff2b0af555d0fa0db47500e0ae28a4431275ff0b3d0
4
+ data.tar.gz: b4d3ac9a4b66605396f1fd27fb83618e8897f5284e6fc9b97426b0ece545ef10
5
5
  SHA512:
6
- metadata.gz: '055149df21395547a80b581a6d31ef9aa2e7f3edce05a797e0d4e48152bfb22c3133ad0d30430bb2ee062f33f06e608611a5b3e51f8d3535ebc64e546a202535'
7
- data.tar.gz: '0292348893de9daa8a9e2590de75755459da6a26517ff77e20842bbb37d48729e5908d6edd46aa9ac4ab0b13c35ac16dd16de624f034528adec3482e88c9a999'
6
+ metadata.gz: 38152d121b929079ba9315832bfac315771bb640c7cabbd7c66fdd286662706c54f9a0412d575bc2024e3aa929aae950c679c6189969ecfe454e41d333e38fb7
7
+ data.tar.gz: c3b9b060a58f659a32fbff7baa950eade738672d426e1aaadb295df58bed56cbd5ddde2b09f1ce3a8caca47c4b1ce85e086b10598798c858fb45fdc0483604cb
@@ -1,6 +1,6 @@
1
1
  source "https://rubygems.org"
2
2
 
3
- gem 'rubypitaya', '3.4.2'
3
+ gem 'rubypitaya', '3.5.0'
4
4
 
5
5
  group :development do
6
6
  gem 'pry', '0.14.1'
@@ -99,7 +99,7 @@ GEM
99
99
  rspec-support (~> 3.11.0)
100
100
  rspec-support (3.11.0)
101
101
  ruby2_keywords (0.0.5)
102
- rubypitaya (3.4.2)
102
+ rubypitaya (3.5.0)
103
103
  activerecord (= 7.0.2)
104
104
  etcdv3 (= 0.11.5)
105
105
  google-protobuf (= 3.19.4)
@@ -138,7 +138,7 @@ DEPENDENCIES
138
138
  listen (= 3.7.1)
139
139
  pry (= 0.14.1)
140
140
  rspec (= 3.11.0)
141
- rubypitaya (= 3.4.2)
141
+ rubypitaya (= 3.5.0)
142
142
  sinatra-contrib (= 2.1.0)
143
143
 
144
144
  BUNDLED WITH
@@ -99,6 +99,12 @@ prod-push-image:
99
99
  prod-deploy-image:
100
100
  kubectl --context='$(KUBECONTEXT)' -n $(KUBE_NAMESPACE) set image deployment $(KUBE_DEPLOYMENT_SERVER) $(KUBE_DEPLOYMENT_SERVER)=$(IMAGE_REGISTRY):$(IMAGE_TAG)
101
101
 
102
+ ## + Other Commands
103
+
104
+ ## Clear docker containers and volumes
105
+ clear:
106
+ @docker rm -f $$(docker ps -aq) ; docker volume rm -f $$(docker volume ls)
107
+
102
108
  .DEFAULT_GOAL := show-help
103
109
 
104
110
  .PHONY: show-help
@@ -1,10 +1,12 @@
1
+ require 'rubypitaya/core/redis_service'
2
+
1
3
  class AppInitializer < RubyPitaya::InitializerBase
2
4
 
3
5
  # method: run
4
6
  # parameter: initializer_content
5
7
  # attributes:
6
- # - redis
7
- # - link: https://github.com/redis/redis-rb/
8
+ # - services
9
+ # - link: https://gitlab.com/LucianoPC/ruby-pitaya/-/blob/master/lib/rubypitaya/core/service_holder.rb
8
10
  #
9
11
  # - mongo
10
12
  # - class: Mongo::Client
@@ -29,9 +31,17 @@ class AppInitializer < RubyPitaya::InitializerBase
29
31
  # - info
30
32
  # - log information
31
33
 
34
+ # services:
35
+ # - redis
36
+ # - link: https://github.com/redis/redis-rb/
37
+
32
38
  def run(initializer_content)
33
- playerBll = PlayerBLL.new
39
+ setup = initializer_content.setup
40
+ services = initializer_content.services
34
41
 
42
+ services.add(:redis, RubyPitaya::RedisService.new(setup))
43
+
44
+ playerBll = PlayerBLL.new
35
45
  PlayerHandler.objects.add(:bll, playerBll)
36
46
  end
37
47
 
@@ -0,0 +1,18 @@
1
+ class ErrorHandler < RubyPitaya::HandlerBase
2
+
3
+ non_authenticated_actions :getError
4
+
5
+ def getError
6
+ error_code = RubyPitaya::StatusCodes::CODE_ERROR
7
+ error_message = "Some Error"
8
+
9
+ raise RubyPitaya::RouteError.new(error_code, error_message)
10
+
11
+ response = {
12
+ code: 'RP-200',
13
+ data: {
14
+ message: 'Ok!'
15
+ }
16
+ }
17
+ end
18
+ end
@@ -8,9 +8,6 @@ class PlayerHandler < RubyPitaya::HandlerBase
8
8
  # - [](key)
9
9
  # - info: get object by key
10
10
  #
11
- # - @redis
12
- # - link: https://github.com/redis/redis-rb/
13
- #
14
11
  # - @mongo
15
12
  # - class: Mongo::Client
16
13
  # - link: https://docs.mongodb.com/ruby-driver/current/tutorials/quick-start/
@@ -7,11 +7,11 @@ module RubyPitaya
7
7
  class_attribute :non_authenticated_routes, default: []
8
8
  class_attribute :handler_objects, default: nil, instance_reader: false, instance_writer: false, instance_accessor: false, instance_predicate: false
9
9
 
10
- attr_reader :objects, :log, :redis, :setup, :config, :params, :session, :postman
10
+ attr_reader :objects, :log, :services, :setup, :config, :params, :session, :postman
11
11
 
12
12
  def initialize
13
13
  @log = nil
14
- @redis = nil
14
+ @services = nil
15
15
  @setup = nil
16
16
  @config = nil
17
17
  @params = nil
@@ -21,9 +21,9 @@ module RubyPitaya
21
21
  @objects = self.class.objects
22
22
  end
23
23
 
24
- def set_attributes(log, redis, mongo, setup, config, params, session, postman)
24
+ def set_attributes(log, services, mongo, setup, config, params, session, postman)
25
25
  @log = log
26
- @redis = redis
26
+ @services = services
27
27
  @mongo = mongo
28
28
  @setup = setup
29
29
  @config = config
@@ -64,7 +64,7 @@ module RubyPitaya
64
64
  @handler_name_map = @handler_name_map.to_h
65
65
  end
66
66
 
67
- def call(handler_name, action_name, session, postman, redis, mongo, setup,
67
+ def call(handler_name, action_name, session, postman, services, mongo, setup,
68
68
  config, log, params)
69
69
  unless @handler_name_map.include?(handler_name)
70
70
  return {
@@ -83,11 +83,11 @@ module RubyPitaya
83
83
  handler = @handler_name_map[handler_name]
84
84
 
85
85
  if !handler.class.authenticated_action_name?(action_name)
86
- handler.set_attributes(log, redis, mongo, setup, config, params, session, postman)
86
+ handler.set_attributes(log, services, mongo, setup, config, params, session, postman)
87
87
  handler.send(action_name)
88
88
  else
89
89
  if session.authenticated?
90
- handler.set_attributes(log, redis, mongo, setup, config, params, session, postman)
90
+ handler.set_attributes(log, services, mongo, setup, config, params, session, postman)
91
91
  handler.send(action_name)
92
92
  else
93
93
  return {
@@ -26,7 +26,7 @@ module RubyPitaya
26
26
  @log = settings.log
27
27
  @mongo = settings.mongo
28
28
  @setup = settings.setup
29
- @redis = settings.redis
29
+ @services = settings.services
30
30
  @config = settings.config
31
31
  @objects = settings.objects
32
32
  @session = nil
@@ -2,12 +2,12 @@ module RubyPitaya
2
2
 
3
3
  class InitializerContent
4
4
 
5
- attr_reader :log, :redis, :mongo, :setup, :config
5
+ attr_reader :log, :services, :mongo, :setup, :config
6
6
 
7
- def initialize(log, redis, mongo, setup, config)
7
+ def initialize(log, services, mongo, setup, config)
8
8
  @log = log
9
+ @services = services
9
10
  @mongo = mongo
10
- @redis = redis
11
11
  @setup = setup
12
12
  @config = config
13
13
  end
@@ -13,13 +13,13 @@ require 'rubypitaya/core/http_routes'
13
13
  require 'rubypitaya/core/route_error'
14
14
  require 'rubypitaya/core/routes_base'
15
15
  require 'rubypitaya/core/status_codes'
16
- require 'rubypitaya/core/handler_router'
17
16
  require 'rubypitaya/core/etcd_connector'
17
+ require 'rubypitaya/core/handler_router'
18
18
  require 'rubypitaya/core/nats_connector'
19
+ require 'rubypitaya/core/service_holder'
19
20
  require 'rubypitaya/core/database_config'
20
21
  require 'rubypitaya/core/instance_holder'
21
22
  require 'rubypitaya/core/mongo_connector'
22
- require 'rubypitaya/core/redis_connector'
23
23
  require 'rubypitaya/core/database_connector'
24
24
  require 'rubypitaya/core/initializer_content'
25
25
  require 'rubypitaya/core/initializer_broadcast'
@@ -64,10 +64,6 @@ module RubyPitaya
64
64
  @nats_connector = NatsConnector.new(@nats_address, @service_uuid,
65
65
  @server_name)
66
66
 
67
- @redis_address = @setup['rubypitaya.redis.url']
68
- @redis_connector = RedisConnector.new(@redis_address)
69
- @redis_connector.connect
70
-
71
67
  @database_config = DatabaseConfig.new
72
68
  @database_connector = DatabaseConnector.new(@database_config, @log)
73
69
  @database_connector.connect
@@ -86,10 +82,12 @@ module RubyPitaya
86
82
  @config = Config.new
87
83
  @config.auto_reload if @is_development_environment
88
84
 
85
+ @services = ServiceHolder.new
86
+
89
87
  @objects = InstanceHolder.new
90
88
 
91
89
  @initializer_content = InitializerContent.new(@log,
92
- @redis_connector.redis,
90
+ @services,
93
91
  @mongo_connector.mongo,
94
92
  @setup,
95
93
  @config)
@@ -98,15 +96,25 @@ module RubyPitaya
98
96
 
99
97
  @handler_router = HandlerRouter.new(@is_cheats_enabled)
100
98
 
99
+ connect_services
100
+
101
101
  run_http
102
102
  run_server
103
103
  end
104
104
 
105
105
  private
106
106
 
107
+ def connect_services
108
+ @services.all_services.map(&:connect)
109
+ end
110
+
111
+ def disconnect_services
112
+ @services.all_services.map(&:disconnect)
113
+ end
114
+
107
115
  def run_http
108
116
  HttpRoutes.set :log, @log
109
- HttpRoutes.set :redis, @redis_connector.redis
117
+ HttpRoutes.set :services, @services
110
118
  HttpRoutes.set :mongo, @mongo_connector.mongo
111
119
  HttpRoutes.set :setup, @setup
112
120
  HttpRoutes.set :config, @config
@@ -147,6 +155,8 @@ module RubyPitaya
147
155
 
148
156
  @log.info "Server shutting down..."
149
157
 
158
+ disconnect_services
159
+
150
160
  @etcd_connector.disconnect
151
161
  @nats_connector.disconnect
152
162
  @database_connector.disconnect
@@ -186,17 +196,17 @@ module RubyPitaya
186
196
 
187
197
  begin
188
198
  response = @handler_router.call(handler_name, action_name, @session,
189
- @postman, @redis_connector.redis,
199
+ @postman, @services,
190
200
  @mongo_connector.mongo, @setup, @config,
191
201
  @log, params)
192
202
  rescue RouteError => error
193
- @log.error "ROUTE ERROR: #{error.class} | #{error.message}} \n #{error.backtrace.join("\n")}"
203
+ @log.error "ROUTE ERROR: #{error.class} | #{error.message} \n #{error.backtrace.join("\n")}"
194
204
  response = {
195
205
  code: error.code,
196
206
  message: error.message
197
207
  }
198
208
  rescue Exception => error
199
- @log.error "INTERNAL ERROR: #{error.class} | #{error.message}} \n #{error.backtrace.join("\n")}"
209
+ @log.error "INTERNAL ERROR: #{error.class} | #{error.message} \n #{error.backtrace.join("\n")}"
200
210
  response = {
201
211
  code: StatusCodes::CODE_INTERNAL_ERROR,
202
212
  message: StatusCodes::MESSAGE_INTERNAL_ERROR,
@@ -1,14 +1,15 @@
1
1
  require 'redis'
2
+ require 'rubypitaya/core/service_base'
2
3
 
3
4
  module RubyPitaya
4
5
 
5
- class RedisConnector
6
+ class RedisService < ServiceBase
6
7
 
7
8
  attr_reader :redis
8
9
 
9
- def initialize(redis_address)
10
+ def initialize(setup)
10
11
  @redis = nil
11
- @redis_address = redis_address
12
+ @redis_address = setup['rubypitaya.redis.url']
12
13
  end
13
14
 
14
15
  def connect
@@ -21,5 +22,17 @@ module RubyPitaya
21
22
 
22
23
  @redis.ping
23
24
  end
25
+
26
+ def disconnect
27
+ @redis.close
28
+ end
29
+
30
+ def client
31
+ @redis
32
+ end
33
+
34
+ def clear_all_data
35
+ @redis.flushall
36
+ end
24
37
  end
25
38
  end
@@ -0,0 +1,21 @@
1
+ module RubyPitaya
2
+
3
+ class ServiceBase
4
+
5
+ def connect
6
+ raise "Service connect method not implemented"
7
+ end
8
+
9
+ def disconnect
10
+ raise "Service disconnect method not implemented"
11
+ end
12
+
13
+ # def get_service_instance
14
+ # raise "Service get_service_instance method not implemented"
15
+ # end
16
+
17
+ def clear_all_data
18
+ raise "Service disconnect method not implemented"
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,32 @@
1
+ module RubyPitaya
2
+
3
+ class ServiceHolder
4
+
5
+ def initialize
6
+ @services = {}
7
+ end
8
+
9
+ def add(key, service)
10
+ check_service_instance(service)
11
+ @services[key] = service
12
+ end
13
+
14
+ def [](key)
15
+ @services[key].client
16
+ end
17
+
18
+ def all_services
19
+ @services.values
20
+ end
21
+
22
+ private
23
+
24
+ def check_service_instance(service)
25
+ raise not_a_service_message(service) unless service.is_a? ServiceBase
26
+ end
27
+
28
+ def not_a_service_message(service)
29
+ "Service #{service.class} is not inheriting from RubyPitaya::ServiceBase"
30
+ end
31
+ end
32
+ end
@@ -1,6 +1,6 @@
1
1
  require 'rubypitaya/core/setup'
2
2
  require 'rubypitaya/core/handler_router'
3
- require 'rubypitaya/core/redis_connector'
3
+ require 'rubypitaya/core/service_holder'
4
4
  require 'rubypitaya/core/spec-helpers/setup_spec_helper'
5
5
  require 'rubypitaya/core/spec-helpers/config_spec_helper'
6
6
  require 'rubypitaya/core/spec-helpers/postman_spec_helper'
@@ -17,10 +17,10 @@ module RubyPitaya
17
17
  @@config = ConfigSpecHelper.new
18
18
  @@session = Session.new
19
19
  @@postman = PostmanSpecHelper.new
20
+ @@services = ServiceHolder.new
20
21
 
21
22
  @@response = {}
22
23
 
23
- initialize_redis
24
24
  initialize_mongo
25
25
 
26
26
  is_cheats_enabled = @@setup.fetch('rubypitaya.server.cheats', false)
@@ -28,7 +28,7 @@ module RubyPitaya
28
28
 
29
29
 
30
30
  @@initializer_content = InitializerContent.new(@@log,
31
- @@redis_connector.redis,
31
+ @@services,
32
32
  @@mongo_connector.mongo,
33
33
  @@setup,
34
34
  @@config)
@@ -40,7 +40,7 @@ module RubyPitaya
40
40
  handler_name, action_name = route.split('.')[1..-1]
41
41
 
42
42
  @@response = @@handler_router.call(handler_name, action_name, @@session,
43
- @@postman, @@redis_connector.redis,
43
+ @@postman, @@services,
44
44
  @@mongo_connector.mongo, @@setup,
45
45
  @@config, @@log, params)
46
46
  rescue RouteError => error
@@ -98,25 +98,12 @@ module RubyPitaya
98
98
  @@postman
99
99
  end
100
100
 
101
- def redis
102
- @@redis_connector.redis
101
+ def services
102
+ @@services
103
103
  end
104
104
 
105
105
  private
106
106
 
107
- def initialize_redis
108
- @@redis_connector ||= nil
109
-
110
- if @@redis_connector.nil?
111
- @@core_setup ||= Setup.new
112
- redis_address = @@core_setup['rubypitaya.redis.url']
113
- @@redis_connector = RedisConnector.new(redis_address)
114
- @@redis_connector.connect
115
- end
116
-
117
- @@redis_connector.redis.flushall
118
- end
119
-
120
107
  def initialize_mongo
121
108
  @@mongo_connector ||= nil
122
109
 
@@ -1,3 +1,3 @@
1
1
  module RubyPitaya
2
- VERSION = '3.4.2'
2
+ VERSION = '3.5.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubypitaya
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.4.2
4
+ version: 3.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luciano Prestes Cavalcanti
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-02-11 00:00:00.000000000 Z
11
+ date: 2022-03-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg
@@ -268,6 +268,7 @@ files:
268
268
  - "./lib/rubypitaya/app-template/app/bll/player_bll.rb"
269
269
  - "./lib/rubypitaya/app-template/app/config/initial_player.json"
270
270
  - "./lib/rubypitaya/app-template/app/constants/status_codes.rb"
271
+ - "./lib/rubypitaya/app-template/app/handlers/error_handler.rb"
271
272
  - "./lib/rubypitaya/app-template/app/handlers/hello_world_handler.rb"
272
273
  - "./lib/rubypitaya/app-template/app/handlers/player_handler.rb"
273
274
  - "./lib/rubypitaya/app-template/app/http/hello_world_http.rb"
@@ -357,9 +358,11 @@ files:
357
358
  - "./lib/rubypitaya/core/postman.rb"
358
359
  - "./lib/rubypitaya/core/protos/nats_connector.proto"
359
360
  - "./lib/rubypitaya/core/protos/nats_connector_pb.rb"
360
- - "./lib/rubypitaya/core/redis_connector.rb"
361
+ - "./lib/rubypitaya/core/redis_service.rb"
361
362
  - "./lib/rubypitaya/core/route_error.rb"
362
363
  - "./lib/rubypitaya/core/routes_base.rb"
364
+ - "./lib/rubypitaya/core/service_base.rb"
365
+ - "./lib/rubypitaya/core/service_holder.rb"
363
366
  - "./lib/rubypitaya/core/session.rb"
364
367
  - "./lib/rubypitaya/core/setup.rb"
365
368
  - "./lib/rubypitaya/core/spec-helpers/config_spec_helper.rb"