rubypitaya 3.4.2 → 3.7.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.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rubypitaya/app-template/Gemfile +1 -1
  3. data/lib/rubypitaya/app-template/Gemfile.lock +2 -2
  4. data/lib/rubypitaya/app-template/Makefile +6 -0
  5. data/lib/rubypitaya/app-template/Rakefile +6 -5
  6. data/lib/rubypitaya/app-template/app/app_initializer.rb +19 -7
  7. data/lib/rubypitaya/app-template/app/constants/status_codes.rb +1 -0
  8. data/lib/rubypitaya/app-template/app/handlers/error_handler.rb +18 -0
  9. data/lib/rubypitaya/app-template/app/handlers/hello_world_handler.rb +13 -2
  10. data/lib/rubypitaya/app-template/app/handlers/player_handler.rb +10 -12
  11. data/lib/rubypitaya/app-template/app/setup/rubypitaya.yml +0 -1
  12. data/lib/rubypitaya/app-template/features/hello_world.feature +24 -0
  13. data/lib/rubypitaya/app-template/features/step_definitions/rubypitaya_steps.rb +11 -0
  14. data/lib/rubypitaya/app-template/features/support/env.rb +8 -2
  15. data/lib/rubypitaya/app-template/spec/player_handler_spec.rb +6 -6
  16. data/lib/rubypitaya/app-template/spec/redis_service_spec.rb +19 -0
  17. data/lib/rubypitaya/core/app/services/mongo_service.rb +35 -0
  18. data/lib/rubypitaya/core/app/services/redis_service.rb +36 -0
  19. data/lib/rubypitaya/core/database_config.rb +9 -9
  20. data/lib/rubypitaya/core/database_connector.rb +6 -4
  21. data/lib/rubypitaya/core/handler_base.rb +4 -5
  22. data/lib/rubypitaya/core/handler_router.rb +3 -3
  23. data/lib/rubypitaya/core/helpers/setup_helper.rb +2 -3
  24. data/lib/rubypitaya/core/http_routes.rb +1 -2
  25. data/lib/rubypitaya/core/initializer_content.rb +3 -4
  26. data/lib/rubypitaya/core/main.rb +22 -27
  27. data/lib/rubypitaya/core/service_base.rb +21 -0
  28. data/lib/rubypitaya/core/service_holder.rb +32 -0
  29. data/lib/rubypitaya/core/session.rb +8 -0
  30. data/lib/rubypitaya/core/setup.rb +4 -0
  31. data/lib/rubypitaya/core/spec-helpers/config_spec_helper.rb +11 -1
  32. data/lib/rubypitaya/core/spec-helpers/handler_spec_helper.rb +35 -40
  33. data/lib/rubypitaya/core/spec-helpers/rubypitaya_spec_helper.rb +9 -0
  34. data/lib/rubypitaya/core/spec-helpers/setup_spec_helper.rb +22 -4
  35. data/lib/rubypitaya/version.rb +1 -1
  36. metadata +8 -4
  37. data/lib/rubypitaya/core/mongo_connector.rb +0 -25
  38. data/lib/rubypitaya/core/redis_connector.rb +0 -25
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 782f0fbd940b77a8ec951dec8df3f63cf673feddc6a68219829c5a7a1d9883d4
4
- data.tar.gz: 9d107b921e8dabe4d410eae3025d6bd187c61718bf3ee3bc69a7cf449edd0516
3
+ metadata.gz: 0d18288d711a487bc0b84dcd073c63ed0192ca0412fb99fcbc1e0305c587df97
4
+ data.tar.gz: 1c5b6be9550df23b8d625806ae0b2a0f3a6b454e01a9ba32f27d890b5f169272
5
5
  SHA512:
6
- metadata.gz: '055149df21395547a80b581a6d31ef9aa2e7f3edce05a797e0d4e48152bfb22c3133ad0d30430bb2ee062f33f06e608611a5b3e51f8d3535ebc64e546a202535'
7
- data.tar.gz: '0292348893de9daa8a9e2590de75755459da6a26517ff77e20842bbb37d48729e5908d6edd46aa9ac4ab0b13c35ac16dd16de624f034528adec3482e88c9a999'
6
+ metadata.gz: 0326e18138b4eeae46b59d4d43df908446072b95838e724dcf9fd46125c937519bdf3781e177ba97c7e7db5d3aeabbc5a25675f0602d2aeaa4a640637bc1fc2e
7
+ data.tar.gz: cfe3acbd44442ea99606bb6ba4e51b7103febecdad79551228a1bf2a2aaf5fda80b219d168fed4e8055eed6efc006aea0c0d582df6d0a15d6f7ab5e83769ee6c
@@ -1,6 +1,6 @@
1
1
  source "https://rubygems.org"
2
2
 
3
- gem 'rubypitaya', '3.4.2'
3
+ gem 'rubypitaya', '3.7.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.7.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.7.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,6 +1,7 @@
1
1
  require 'active_record'
2
2
 
3
3
  require 'rubypitaya/core/path'
4
+ require 'rubypitaya/core/setup'
4
5
  require 'rubypitaya/core/database_config'
5
6
 
6
7
 
@@ -8,7 +9,7 @@ namespace :db do
8
9
 
9
10
  desc 'Create the database'
10
11
  task :create do
11
- database_config = RubyPitaya::DatabaseConfig.new
12
+ database_config = RubyPitaya::DatabaseConfig.new(RubyPitaya::Setup.new)
12
13
  connection_data = database_config.connection_data_without_database
13
14
 
14
15
  ActiveRecord::Base.establish_connection(connection_data)
@@ -20,7 +21,7 @@ namespace :db do
20
21
 
21
22
  desc 'Migrate the database'
22
23
  task :migrate do
23
- database_config = RubyPitaya::DatabaseConfig.new
24
+ database_config = RubyPitaya::DatabaseConfig.new(RubyPitaya::Setup.new)
24
25
  connection_data = database_config.connection_data
25
26
  migrations_paths = [RubyPitaya::Path::Core::MIGRATIONS_FOLDER_PATH]
26
27
  migrations_paths += RubyPitaya::Path::Plugins::MIGRATIONS_FOLDER_PATHS
@@ -36,7 +37,7 @@ namespace :db do
36
37
 
37
38
  desc 'Rollback migrations'
38
39
  task :rollback do
39
- database_config = RubyPitaya::DatabaseConfig.new
40
+ database_config = RubyPitaya::DatabaseConfig.new(RubyPitaya::Setup.new)
40
41
  connection_data = database_config.connection_data
41
42
  migrations_paths = [RubyPitaya::Path::Core::MIGRATIONS_FOLDER_PATH]
42
43
  migrations_paths += RubyPitaya::Path::Plugins::MIGRATIONS_FOLDER_PATHS
@@ -61,7 +62,7 @@ namespace :db do
61
62
 
62
63
  desc 'Drop the database'
63
64
  task :drop do
64
- database_config = RubyPitaya::DatabaseConfig.new
65
+ database_config = RubyPitaya::DatabaseConfig.new(RubyPitaya::Setup.new)
65
66
  connection_data = database_config.connection_data_without_database
66
67
 
67
68
  ActiveRecord::Base.establish_connection(connection_data)
@@ -74,7 +75,7 @@ namespace :db do
74
75
 
75
76
  desc 'migration status'
76
77
  task :status do
77
- database_config = RubyPitaya::DatabaseConfig.new
78
+ database_config = RubyPitaya::DatabaseConfig.new(RubyPitaya::Setup.new)
78
79
  connection_data = database_config.connection_data
79
80
  migrations_paths = [RubyPitaya::Path::Core::MIGRATIONS_FOLDER_PATH]
80
81
  migrations_paths += RubyPitaya::Path::Plugins::MIGRATIONS_FOLDER_PATHS
@@ -1,14 +1,13 @@
1
+ require 'rubypitaya/core/app/services/mongo_service'
2
+ require 'rubypitaya/core/app/services/redis_service'
3
+
1
4
  class AppInitializer < RubyPitaya::InitializerBase
2
5
 
3
6
  # method: run
4
7
  # parameter: initializer_content
5
8
  # attributes:
6
- # - redis
7
- # - link: https://github.com/redis/redis-rb/
8
- #
9
- # - mongo
10
- # - class: Mongo::Client
11
- # - link: https://docs.mongodb.com/ruby-driver/current/tutorials/quick-start/
9
+ # - services
10
+ # - link: https://gitlab.com/LucianoPC/ruby-pitaya/-/blob/master/lib/rubypitaya/core/service_holder.rb
12
11
  #
13
12
  # - config
14
13
  # - class: RubyPitaya::Config
@@ -28,10 +27,23 @@ class AppInitializer < RubyPitaya::InitializerBase
28
27
  # - methods:
29
28
  # - info
30
29
  # - log information
30
+ #
31
+ # services:
32
+ # - redis
33
+ # - link: https://github.com/redis/redis-rb/
34
+ #
35
+ # - mongo
36
+ # - class: Mongo::Client
37
+ # - link: https://docs.mongodb.com/ruby-driver/current/tutorials/quick-start/
31
38
 
32
39
  def run(initializer_content)
33
- playerBll = PlayerBLL.new
40
+ setup = initializer_content.setup
41
+ services = initializer_content.services
34
42
 
43
+ services.add(:mongo, RubyPitaya::MongoService.new(setup))
44
+ services.add(:redis, RubyPitaya::RedisService.new(setup))
45
+
46
+ playerBll = PlayerBLL.new
35
47
  PlayerHandler.objects.add(:bll, playerBll)
36
48
  end
37
49
 
@@ -1,6 +1,7 @@
1
1
  class StatusCodes
2
2
 
3
3
  CODE_OK = RubyPitaya::StatusCodes::CODE_OK
4
+ CODE_AUTHENTICATION_ERROR = RubyPitaya::StatusCodes::CODE_AUTHENTICATION_ERROR
4
5
 
5
6
  ################
6
7
  ## Existent Codes
@@ -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: StatusCodes::CODE_OK,
13
+ data: {
14
+ message: 'Ok!'
15
+ }
16
+ }
17
+ end
18
+ end
@@ -1,13 +1,24 @@
1
1
  class HelloWorldHandler < RubyPitaya::HandlerBase
2
2
 
3
- non_authenticated_actions :sayHello
3
+ non_authenticated_actions :sayHello, :showMessage
4
4
 
5
5
  def sayHello
6
6
  response = {
7
- code: 'RP-200',
7
+ code: StatusCodes::CODE_OK,
8
8
  data: {
9
9
  message: 'Hello!'
10
10
  }
11
11
  }
12
12
  end
13
+
14
+ def showMessage
15
+ message = @params[:message]
16
+
17
+ response = {
18
+ code: StatusCodes::CODE_OK,
19
+ data: {
20
+ message: message
21
+ }
22
+ }
23
+ end
13
24
  end
@@ -1,6 +1,6 @@
1
1
  class PlayerHandler < RubyPitaya::HandlerBase
2
2
 
3
- # class: HandlerBase
3
+ # class: RubyPitaya::HandlerBase
4
4
  # attributes:
5
5
  # - @objects
6
6
  # - class: InstanceHolder
@@ -8,13 +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
- # - @mongo
15
- # - class: Mongo::Client
16
- # - link: https://docs.mongodb.com/ruby-driver/current/tutorials/quick-start/
17
- #
18
11
  # - @config
19
12
  # - info: Hash with config json files inside of 'app/config'
20
13
  # - example:
@@ -44,6 +37,14 @@ class PlayerHandler < RubyPitaya::HandlerBase
44
37
  # of the session, for example you can set an userId on
45
38
  # @session, like `@session.uid = '123'`, and then you can
46
39
  # bind this session with `@postman.bind_session(@session)`
40
+ #
41
+ # - @services:
42
+ # - redis
43
+ # - link: https://github.com/redis/redis-rb/
44
+ #
45
+ # - mongo
46
+ # - class: Mongo::Client
47
+ # - link: https://docs.mongodb.com/ruby-driver/current/tutorials/quick-start/
47
48
 
48
49
  non_authenticated_actions :authenticate
49
50
 
@@ -58,10 +59,7 @@ class PlayerHandler < RubyPitaya::HandlerBase
58
59
  bind_session_response = @postman.bind_session(@session)
59
60
 
60
61
  unless bind_session_response.dig(:error, :code).nil?
61
- return response = {
62
- code: RubyPitaya::StatusCodes::CODE_AUTHENTICATION_ERROR,
63
- msg: 'Error to authenticate',
64
- }
62
+ raise RubyPitaya::RouteError.new(StatusCodes::CODE_AUTHENTICATION_ERROR, 'Error to authenticate')
65
63
  end
66
64
 
67
65
  response = {
@@ -26,7 +26,6 @@ mongo:
26
26
  user: 'user'
27
27
  pass: 'password'
28
28
  database: admin
29
- enabled: true
30
29
 
31
30
  http:
32
31
  auth:
@@ -6,3 +6,27 @@ Feature: Hello World
6
6
  Given client call route 'rubypitaya.helloWorldHandler.sayHello'
7
7
  Then server should response 'code' as 'RP-200'
8
8
  And server should response 'data.message' as 'Hello!'
9
+
10
+ Scenario: Custom message
11
+ When client call route 'rubypitaya.helloWorldHandler.showMessage' with params:
12
+ | message |
13
+ | Hello World! |
14
+ Then server should response 'code' as 'RP-200'
15
+ And server should response 'data.message' as 'Hello World!'
16
+
17
+ Scenario: Custom message with json
18
+ When client call route 'rubypitaya.helloWorldHandler.showMessage' with json params:
19
+ """
20
+ {
21
+ "message": "Hello World!"
22
+ }
23
+ """
24
+ Then server should response the following json:
25
+ """
26
+ {
27
+ "code": "RP-200",
28
+ "data": {
29
+ "message": "Hello World!"
30
+ }
31
+ }
32
+ """
@@ -2,6 +2,16 @@ Given(/^[Cc]lient call route ["'](.+)["']$/) do |route|
2
2
  @handler_helper.request(route)
3
3
  end
4
4
 
5
+ Given(/^[Cc]lient call route ["'](.+)["'] with param[s]*[:]*$/) do |route, table|
6
+ params = table.hashes.first.symbolize_keys
7
+ @handler_helper.request(route, params)
8
+ end
9
+
10
+ Given(/^[Cc]lient call route ["'](.+)["'] with json param[s]*[:]*$/) do |route, json|
11
+ params = JSON.parse(json, symbolize_names: true)
12
+ @handler_helper.request(route, params)
13
+ end
14
+
5
15
  Given(/^[Ss]erver should response ["'](.+)["'] as ["'](.+)["']$/) do |response_key, expected_value|
6
16
  response_value = @handler_helper.response.dig(*response_key.split('.').map(&:to_sym))
7
17
 
@@ -52,6 +62,7 @@ end
52
62
 
53
63
  Before do
54
64
  ActiveRecord::Base.descendants.each { |c| c.delete_all unless c == ActiveRecord::SchemaMigration }
65
+ RubyPitaya::HandlerSpecHelper.update_before_each
55
66
  @handler_helper = RubyPitaya::HandlerSpecHelperClass.new('cucumber')
56
67
  end
57
68
 
@@ -1,7 +1,13 @@
1
1
  require 'rspec'
2
2
  require 'cucumber/rspec/doubles'
3
3
 
4
+ require 'rubypitaya/core/helpers/setup_helper'
5
+ require 'rubypitaya/core/spec-helpers/handler_spec_helper_class'
6
+
4
7
  ENV['RUBYPITAYA_ENV'] = 'test'
5
8
 
6
- require 'rubypitaya/core/helpers/setup_helper'
7
- require 'rubypitaya/core/spec-helpers/handler_spec_helper_class'
9
+ RubyPitaya::HandlerSpecHelper.initialize_before_suite
10
+
11
+ at_exit do
12
+ RubyPitaya::HandlerSpecHelper.finalize_after_suite
13
+ end
@@ -3,12 +3,12 @@ require 'spec_helper'
3
3
  RSpec.describe 'PlayerHandler', type: :request do
4
4
  context 'authenticate' do
5
5
  it 'create_new_user' do
6
- config = {'initial_player' => {
7
- 'name' => 'Guest',
8
- 'wallet' => {'gold' => 10},
9
- }}
10
-
11
- set_config(config)
6
+ set_config({
7
+ 'initial_player' => {
8
+ 'name' => 'Guest',
9
+ 'wallet' => {'gold' => 10},
10
+ }
11
+ })
12
12
 
13
13
  params = {}
14
14
  request("rubypitaya.playerHandler.authenticate", params)
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe 'RubyPitaya::RedisService', type: :request do
4
+ it 'mock service' do
5
+ allow(services[:redis]).to receive(:get).with('key').and_return('something')
6
+
7
+ value = services[:redis].get('key')
8
+
9
+ expect(value).to eq('something')
10
+ end
11
+
12
+ it 'not mock service' do
13
+ services[:redis].set('foo', 'bar')
14
+
15
+ value = services[:redis].get('foo')
16
+
17
+ expect(value).to eq('bar')
18
+ end
19
+ end
@@ -0,0 +1,35 @@
1
+ require 'mongo'
2
+ require 'rubypitaya/core/service_base'
3
+
4
+ module RubyPitaya
5
+
6
+ class MongoService < ServiceBase
7
+
8
+ def initialize(setup)
9
+ @mongo = nil
10
+ @mongo_address = setup['rubypitaya.mongo.url']
11
+ @mongo_user = setup['rubypitaya.mongo.user']
12
+ @mongo_password = setup['rubypitaya.mongo.pass']
13
+ @mongo_database_name = setup['rubypitaya.mongo.database']
14
+ end
15
+
16
+ def connect
17
+ @mongo = Mongo::Client.new([@mongo_address],
18
+ user: @mongo_user,
19
+ password: @mongo_password,
20
+ database: @mongo_database_name)
21
+ end
22
+
23
+ def disconnect
24
+ # TODO: implement it
25
+ end
26
+
27
+ def client
28
+ @mongo
29
+ end
30
+
31
+ def clear_all_data
32
+ # TODO: implement it
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,36 @@
1
+ require 'redis'
2
+ require 'rubypitaya/core/service_base'
3
+
4
+ module RubyPitaya
5
+
6
+ class RedisService < ServiceBase
7
+
8
+ def initialize(setup)
9
+ @redis = nil
10
+ @redis_address = setup['rubypitaya.redis.url']
11
+ end
12
+
13
+ def connect
14
+ @redis = Redis.new(
15
+ url: @redis_address,
16
+ :reconnect_attempts => 10,
17
+ :reconnect_delay => 1.5,
18
+ :reconnect_delay_max => 2.0,
19
+ )
20
+
21
+ @redis.ping
22
+ end
23
+
24
+ def disconnect
25
+ @redis.close
26
+ end
27
+
28
+ def client
29
+ @redis
30
+ end
31
+
32
+ def clear_all_data
33
+ @redis.flushall
34
+ end
35
+ end
36
+ end
@@ -7,20 +7,20 @@ module RubyPitaya
7
7
 
8
8
  class DatabaseConfig
9
9
 
10
- def initialize()
11
- @setup = Setup.new
12
-
10
+ def initialize(setup)
13
11
  @config = {
14
12
  'adapter' => 'postgresql',
15
13
  'encoding' => 'unicode',
16
- 'pool' => @setup.fetch('rubypitaya.database.pool', 5),
17
- 'host' => @setup['rubypitaya.database.host'],
18
- 'user' => @setup['rubypitaya.database.user'],
19
- 'password' => @setup['rubypitaya.database.password'],
20
- 'database' => @setup['rubypitaya.database.name'],
14
+ 'pool' => setup.fetch('rubypitaya.database.pool', 5),
15
+ 'host' => setup['rubypitaya.database.host'],
16
+ 'user' => setup['rubypitaya.database.user'],
17
+ 'password' => setup['rubypitaya.database.password'],
18
+ 'database' => setup['rubypitaya.database.name'],
21
19
  }
22
20
 
23
- @config['database'] = "#{@config['database']}_test" if @environment_name == 'test'
21
+ environment_name = setup.fetch('rubypitaya.server.environment', 'development')
22
+
23
+ @config['database'] = "#{@config['database']}_test" if environment_name == 'test'
24
24
  end
25
25
 
26
26
  def config
@@ -5,14 +5,16 @@ module RubyPitaya
5
5
 
6
6
  class DatabaseConnector
7
7
 
8
- def initialize(database_config, logger = nil)
9
- @database_config = database_config
10
- @logger ||= ActiveSupport::Logger.new(STDOUT)
8
+ def initialize(setup, logger = nil)
9
+ @database_config = DatabaseConfig.new(setup)
10
+ @logger = ActiveSupport::Logger.new(STDOUT)
11
+ # TODO: Use this
12
+ # @logger = logger || ActiveSupport::Logger.new(STDOUT)
11
13
  end
12
14
 
13
15
  def connect
14
16
  ActiveRecord::Base.establish_connection(@database_config.connection_data)
15
- ActiveRecord::Base.logger = ActiveSupport::Logger.new(STDOUT)
17
+ ActiveRecord::Base.logger = @logger
16
18
  ActiveSupport::LogSubscriber.colorize_logging = true
17
19
  end
18
20
 
@@ -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,10 +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, setup, config, params, session, postman)
25
25
  @log = log
26
- @redis = redis
27
- @mongo = mongo
26
+ @services = services
28
27
  @setup = setup
29
28
  @config = config
30
29
  @params = params
@@ -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, 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, 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, setup, config, params, session, postman)
91
91
  handler.send(action_name)
92
92
  else
93
93
  return {
@@ -1,12 +1,11 @@
1
1
  require 'active_record'
2
2
 
3
+ require 'rubypitaya/core/setup'
3
4
  require 'rubypitaya/core/database_config'
4
5
 
5
6
  # Database connection
6
- database_config = RubyPitaya::DatabaseConfig.new
7
+ database_config = RubyPitaya::DatabaseConfig.new(RubyPitaya::Setup.new)
7
8
  ActiveRecord::Base.establish_connection(database_config.connection_data)
8
- # ActiveRecord::Base.logger = ActiveSupport::Logger.new(STDOUT)
9
- # ActiveSupport::LogSubscriber.colorize_logging = true
10
9
 
11
10
  connection_data = database_config.connection_data
12
11
  migrations_paths = [RubyPitaya::Path::Core::MIGRATIONS_FOLDER_PATH]
@@ -24,9 +24,8 @@ module RubyPitaya
24
24
  content_type :json
25
25
 
26
26
  @log = settings.log
27
- @mongo = settings.mongo
28
27
  @setup = settings.setup
29
- @redis = settings.redis
28
+ @services = settings.services
30
29
  @config = settings.config
31
30
  @objects = settings.objects
32
31
  @session = nil
@@ -2,12 +2,11 @@ module RubyPitaya
2
2
 
3
3
  class InitializerContent
4
4
 
5
- attr_reader :log, :redis, :mongo, :setup, :config
5
+ attr_reader :log, :services, :setup, :config
6
6
 
7
- def initialize(log, redis, mongo, setup, config)
7
+ def initialize(log, services, setup, config)
8
8
  @log = log
9
- @mongo = mongo
10
- @redis = redis
9
+ @services = services
11
10
  @setup = setup
12
11
  @config = config
13
12
  end
@@ -13,13 +13,11 @@ 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/database_config'
19
+ require 'rubypitaya/core/service_holder'
20
20
  require 'rubypitaya/core/instance_holder'
21
- require 'rubypitaya/core/mongo_connector'
22
- require 'rubypitaya/core/redis_connector'
23
21
  require 'rubypitaya/core/database_connector'
24
22
  require 'rubypitaya/core/initializer_content'
25
23
  require 'rubypitaya/core/initializer_broadcast'
@@ -64,33 +62,20 @@ module RubyPitaya
64
62
  @nats_connector = NatsConnector.new(@nats_address, @service_uuid,
65
63
  @server_name)
66
64
 
67
- @redis_address = @setup['rubypitaya.redis.url']
68
- @redis_connector = RedisConnector.new(@redis_address)
69
- @redis_connector.connect
70
-
71
- @database_config = DatabaseConfig.new
72
- @database_connector = DatabaseConnector.new(@database_config, @log)
65
+ @database_connector = DatabaseConnector.new(@setup, @log)
73
66
  @database_connector.connect
74
67
 
75
- @mongo_enabled = @setup['rubypitaya.mongo.enabled']
76
- @mongo_address = @setup['rubypitaya.mongo.url']
77
- @mongo_user = @setup['rubypitaya.mongo.user']
78
- @mongo_password = @setup['rubypitaya.mongo.pass']
79
- @mongo_database_name = @setup['rubypitaya.mongo.database']
80
- @mongo_connector = MongoConnector.new(@mongo_address, @mongo_user,
81
- @mongo_password, @mongo_database_name)
82
- @mongo_connector.connect if @mongo_enabled
83
-
84
68
  @session = Session.new
85
69
  @postman = Postman.new(@nats_connector)
86
70
  @config = Config.new
87
71
  @config.auto_reload if @is_development_environment
88
72
 
73
+ @services = ServiceHolder.new
74
+
89
75
  @objects = InstanceHolder.new
90
76
 
91
77
  @initializer_content = InitializerContent.new(@log,
92
- @redis_connector.redis,
93
- @mongo_connector.mongo,
78
+ @services,
94
79
  @setup,
95
80
  @config)
96
81
  @initializer_broadcast = InitializerBroadcast.new
@@ -98,16 +83,25 @@ module RubyPitaya
98
83
 
99
84
  @handler_router = HandlerRouter.new(@is_cheats_enabled)
100
85
 
86
+ connect_services
87
+
101
88
  run_http
102
89
  run_server
103
90
  end
104
91
 
105
92
  private
106
93
 
94
+ def connect_services
95
+ @services.all_services.map(&:connect)
96
+ end
97
+
98
+ def disconnect_services
99
+ @services.all_services.map(&:disconnect)
100
+ end
101
+
107
102
  def run_http
108
103
  HttpRoutes.set :log, @log
109
- HttpRoutes.set :redis, @redis_connector.redis
110
- HttpRoutes.set :mongo, @mongo_connector.mongo
104
+ HttpRoutes.set :services, @services
111
105
  HttpRoutes.set :setup, @setup
112
106
  HttpRoutes.set :config, @config
113
107
  HttpRoutes.set :objects, @objects
@@ -147,6 +141,8 @@ module RubyPitaya
147
141
 
148
142
  @log.info "Server shutting down..."
149
143
 
144
+ disconnect_services
145
+
150
146
  @etcd_connector.disconnect
151
147
  @nats_connector.disconnect
152
148
  @database_connector.disconnect
@@ -186,17 +182,16 @@ module RubyPitaya
186
182
 
187
183
  begin
188
184
  response = @handler_router.call(handler_name, action_name, @session,
189
- @postman, @redis_connector.redis,
190
- @mongo_connector.mongo, @setup, @config,
185
+ @postman, @services, @setup, @config,
191
186
  @log, params)
192
187
  rescue RouteError => error
193
- @log.error "ROUTE ERROR: #{error.class} | #{error.message}} \n #{error.backtrace.join("\n")}"
188
+ @log.error "ROUTE ERROR: #{error.code} | #{error.class} | #{error.message} \n #{error.backtrace.join("\n")}"
194
189
  response = {
195
190
  code: error.code,
196
191
  message: error.message
197
192
  }
198
193
  rescue Exception => error
199
- @log.error "INTERNAL ERROR: #{error.class} | #{error.message}} \n #{error.backtrace.join("\n")}"
194
+ @log.error "INTERNAL ERROR: #{error.code} | #{error.class} | #{error.message} \n #{error.backtrace.join("\n")}"
200
195
  response = {
201
196
  code: StatusCodes::CODE_INTERNAL_ERROR,
202
197
  message: StatusCodes::MESSAGE_INTERNAL_ERROR,
@@ -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
@@ -33,5 +33,13 @@ module RubyPitaya
33
33
  def user_id=(value)
34
34
  @uid = value
35
35
  end
36
+
37
+ def clear
38
+ @id = ''
39
+ @uid = ''
40
+ @data = {}
41
+ @metadata = {}
42
+ @frontend_id = ''
43
+ end
36
44
  end
37
45
  end
@@ -34,6 +34,10 @@ module RubyPitaya
34
34
  @empty_hash.fetch(*args)
35
35
  end
36
36
 
37
+ def get_config
38
+ @config.deep_dup
39
+ end
40
+
37
41
  def auto_reload
38
42
  require 'listen'
39
43
 
@@ -27,6 +27,10 @@ module RubyPitaya
27
27
  @config_mock = merge_recursively(@config_mock, add_hash).deep_symbolize_keys.stringify_keys
28
28
  end
29
29
 
30
+ def clear
31
+ @config_mock = {}
32
+ end
33
+
30
34
  private
31
35
 
32
36
  def undig(*keys, value)
@@ -34,7 +38,13 @@ module RubyPitaya
34
38
  end
35
39
 
36
40
  def merge_recursively(a, b)
37
- a.merge(b) {|key, a_item, b_item| merge_recursively(a_item, b_item) }
41
+ a.merge(b) do |key, a_item, b_item|
42
+ if a_item.is_a?(Hash) && b_item.is_a?(Hash)
43
+ merge_recursively(a_item, b_item)
44
+ else
45
+ a[key] = b_item
46
+ end
47
+ end
38
48
  end
39
49
  end
40
50
  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'
@@ -9,39 +9,52 @@ module RubyPitaya
9
9
 
10
10
  module HandlerSpecHelper
11
11
 
12
- def initialize(context)
13
- @@context = context
12
+ def self.initialize_before_suite
13
+ default_setup = Setup.new.get_config
14
14
 
15
15
  @@log = Logger.new('/dev/null')
16
- @@setup = SetupSpecHelper.new
16
+ @@setup = SetupSpecHelper.new(default_setup)
17
17
  @@config = ConfigSpecHelper.new
18
18
  @@session = Session.new
19
19
  @@postman = PostmanSpecHelper.new
20
+ @@services = ServiceHolder.new
20
21
 
21
- @@response = {}
22
-
23
- initialize_redis
24
- initialize_mongo
22
+ @@default_setup = Setup.new
25
23
 
26
- is_cheats_enabled = @@setup.fetch('rubypitaya.server.cheats', false)
24
+ is_cheats_enabled = @@setup.fetch('rubypitaya.server.cheats', true)
27
25
  @@handler_router ||= HandlerRouter.new(is_cheats_enabled)
28
26
 
29
27
 
30
28
  @@initializer_content = InitializerContent.new(@@log,
31
- @@redis_connector.redis,
32
- @@mongo_connector.mongo,
29
+ @@services,
33
30
  @@setup,
34
31
  @@config)
35
32
  @@initializer_broadcast = InitializerBroadcast.new
36
33
  @@initializer_broadcast.run(@@initializer_content)
34
+
35
+ connect_services
36
+ end
37
+
38
+ def self.finalize_after_suite
39
+ disconnect_services
40
+ end
41
+
42
+ def self.update_before_each
43
+ @@response = {}
44
+ @@setup.clear
45
+ @@config.clear
46
+ @@session.clear
47
+ clear_all_services_data
48
+ end
49
+
50
+ def initialize(context)
37
51
  end
38
52
 
39
53
  def request(route, params = {})
40
54
  handler_name, action_name = route.split('.')[1..-1]
41
55
 
42
56
  @@response = @@handler_router.call(handler_name, action_name, @@session,
43
- @@postman, @@redis_connector.redis,
44
- @@mongo_connector.mongo, @@setup,
57
+ @@postman, @@services, @@setup,
45
58
  @@config, @@log, params)
46
59
  rescue RouteError => error
47
60
  @@response = {
@@ -98,40 +111,22 @@ module RubyPitaya
98
111
  @@postman
99
112
  end
100
113
 
101
- def redis
102
- @@redis_connector.redis
114
+ def services
115
+ @@services
103
116
  end
104
117
 
105
118
  private
106
119
 
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
120
+ def self.connect_services
121
+ @@services.all_services.map(&:connect)
118
122
  end
119
123
 
120
- def initialize_mongo
121
- @@mongo_connector ||= nil
122
-
123
- if @@mongo_connector.nil?
124
- @@core_setup ||= Setup.new
125
- mongo_address = @@core_setup['rubypitaya.mongo.url']
126
- mongo_user = @@core_setup['rubypitaya.mongo.user']
127
- mongo_password = @@core_setup['rubypitaya.mongo.pass']
128
- mongo_database_name = @@core_setup['rubypitaya.mongo.database']
129
- @@mongo_connector = MongoConnector.new(mongo_address, mongo_user,
130
- mongo_password, mongo_database_name)
131
- @@mongo_connector.connect
132
- end
124
+ def self.disconnect_services
125
+ @@services.all_services.map(&:disconnect)
126
+ end
133
127
 
134
- @@mongo_connector.mongo.collections.map(&:drop)
128
+ def self.clear_all_services_data
129
+ @@services.all_services.map(&:clear_all_data)
135
130
  end
136
131
  end
137
132
  end
@@ -8,7 +8,16 @@ require 'rubypitaya/core/spec-helpers/handler_spec_helper'
8
8
  RSpec.configure do |config|
9
9
  config.include RubyPitaya::HandlerSpecHelper
10
10
 
11
+ config.before(:suite) do
12
+ RubyPitaya::HandlerSpecHelper.initialize_before_suite
13
+ end
14
+
15
+ config.after(:suite) do
16
+ RubyPitaya::HandlerSpecHelper.finalize_after_suite
17
+ end
18
+
11
19
  config.before(:each) do
20
+ RubyPitaya::HandlerSpecHelper.update_before_each
12
21
  ActiveRecord::Base.descendants.each { |c| c.delete_all unless c == ActiveRecord::SchemaMigration }
13
22
  end
14
23
 
@@ -2,13 +2,18 @@ module RubyPitaya
2
2
 
3
3
  class SetupSpecHelper
4
4
 
5
- def initialize
5
+ def initialize(default_setup = {})
6
+ @empty_hash = {}
6
7
  @setup_mock = {}
8
+ @default_setup = default_setup
7
9
  end
8
10
 
9
11
  def [](key)
10
12
  split_key = key.split('.')
11
- @setup_mock.dig(*split_key)
13
+ result = @setup_mock.dig(*split_key)
14
+ return result unless result.nil?
15
+
16
+ @default_setup.dig(*split_key)
12
17
  end
13
18
 
14
19
  def auto_reload
@@ -25,7 +30,14 @@ module RubyPitaya
25
30
  end
26
31
 
27
32
  def fetch(*args)
28
- @setup_mock.fetch(*args)
33
+ result = self[args[0]]
34
+ return result unless result.nil?
35
+
36
+ @empty_hash.fetch(*args)
37
+ end
38
+
39
+ def clear
40
+ @setup_mock = {}
29
41
  end
30
42
 
31
43
  private
@@ -35,7 +47,13 @@ module RubyPitaya
35
47
  end
36
48
 
37
49
  def merge_recursively(a, b)
38
- a.merge(b) {|key, a_item, b_item| merge_recursively(a_item, b_item) }
50
+ a.merge(b) do |key, a_item, b_item|
51
+ if a_item.is_a?(Hash) && b_item.is_a?(Hash)
52
+ merge_recursively(a_item, b_item)
53
+ else
54
+ a[key] = b_item
55
+ end
56
+ end
39
57
  end
40
58
  end
41
59
  end
@@ -1,3 +1,3 @@
1
1
  module RubyPitaya
2
- VERSION = '3.4.2'
2
+ VERSION = '3.7.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.7.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-05 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"
@@ -331,9 +332,12 @@ files:
331
332
  - "./lib/rubypitaya/app-template/plugins.yaml"
332
333
  - "./lib/rubypitaya/app-template/spec/hello_world_handler_spec.rb"
333
334
  - "./lib/rubypitaya/app-template/spec/player_handler_spec.rb"
335
+ - "./lib/rubypitaya/app-template/spec/redis_service_spec.rb"
334
336
  - "./lib/rubypitaya/app-template/spec/spec_helper.rb"
335
337
  - "./lib/rubypitaya/core/app/migrations/0000000001_create_user_migration.rb"
336
338
  - "./lib/rubypitaya/core/app/models/user.rb"
339
+ - "./lib/rubypitaya/core/app/services/mongo_service.rb"
340
+ - "./lib/rubypitaya/core/app/services/redis_service.rb"
337
341
  - "./lib/rubypitaya/core/application_files_importer.rb"
338
342
  - "./lib/rubypitaya/core/config.rb"
339
343
  - "./lib/rubypitaya/core/config_core.rb"
@@ -350,16 +354,16 @@ files:
350
354
  - "./lib/rubypitaya/core/initializer_content.rb"
351
355
  - "./lib/rubypitaya/core/instance_holder.rb"
352
356
  - "./lib/rubypitaya/core/main.rb"
353
- - "./lib/rubypitaya/core/mongo_connector.rb"
354
357
  - "./lib/rubypitaya/core/nats_connector.rb"
355
358
  - "./lib/rubypitaya/core/parameters.rb"
356
359
  - "./lib/rubypitaya/core/path.rb"
357
360
  - "./lib/rubypitaya/core/postman.rb"
358
361
  - "./lib/rubypitaya/core/protos/nats_connector.proto"
359
362
  - "./lib/rubypitaya/core/protos/nats_connector_pb.rb"
360
- - "./lib/rubypitaya/core/redis_connector.rb"
361
363
  - "./lib/rubypitaya/core/route_error.rb"
362
364
  - "./lib/rubypitaya/core/routes_base.rb"
365
+ - "./lib/rubypitaya/core/service_base.rb"
366
+ - "./lib/rubypitaya/core/service_holder.rb"
363
367
  - "./lib/rubypitaya/core/session.rb"
364
368
  - "./lib/rubypitaya/core/setup.rb"
365
369
  - "./lib/rubypitaya/core/spec-helpers/config_spec_helper.rb"
@@ -1,25 +0,0 @@
1
- require 'mongo'
2
-
3
- module RubyPitaya
4
-
5
- class MongoConnector
6
-
7
- def initialize(mongo_address, mongo_user, mongo_password, mongo_database_name)
8
- @mongo_address = mongo_address
9
- @mongo_user = mongo_user
10
- @mongo_password = mongo_password
11
- @mongo_database_name = mongo_database_name
12
- end
13
-
14
- def connect
15
- @mongo_client = Mongo::Client.new([@mongo_address],
16
- user: @mongo_user,
17
- password: @mongo_password,
18
- database: @mongo_database_name)
19
- end
20
-
21
- def mongo
22
- @mongo_client
23
- end
24
- end
25
- end
@@ -1,25 +0,0 @@
1
- require 'redis'
2
-
3
- module RubyPitaya
4
-
5
- class RedisConnector
6
-
7
- attr_reader :redis
8
-
9
- def initialize(redis_address)
10
- @redis = nil
11
- @redis_address = redis_address
12
- end
13
-
14
- def connect
15
- @redis = Redis.new(
16
- url: @redis_address,
17
- :reconnect_attempts => 10,
18
- :reconnect_delay => 1.5,
19
- :reconnect_delay_max => 2.0,
20
- )
21
-
22
- @redis.ping
23
- end
24
- end
25
- end