rubypitaya 3.5.0 → 3.8.0

Sign up to get free protection for your applications and to get access to all the features.
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/Rakefile +6 -5
  5. data/lib/rubypitaya/app-template/app/app_initializer.rb +8 -6
  6. data/lib/rubypitaya/app-template/app/constants/status_codes.rb +1 -0
  7. data/lib/rubypitaya/app-template/app/handlers/error_handler.rb +1 -1
  8. data/lib/rubypitaya/app-template/app/handlers/hello_world_handler.rb +13 -2
  9. data/lib/rubypitaya/app-template/app/handlers/player_handler.rb +10 -9
  10. data/lib/rubypitaya/app-template/app/setup/rubypitaya.yml +0 -1
  11. data/lib/rubypitaya/app-template/features/hello_world.feature +24 -0
  12. data/lib/rubypitaya/app-template/features/step_definitions/rubypitaya_steps.rb +11 -0
  13. data/lib/rubypitaya/app-template/features/support/env.rb +8 -2
  14. data/lib/rubypitaya/app-template/setup/development/database +3 -0
  15. data/lib/rubypitaya/app-template/setup/development/main +4 -0
  16. data/lib/rubypitaya/app-template/setup/test/database +3 -0
  17. data/lib/rubypitaya/app-template/setup/test/main +4 -0
  18. data/lib/rubypitaya/app-template/spec/player_handler_spec.rb +6 -6
  19. data/lib/rubypitaya/app-template/spec/redis_service_spec.rb +19 -0
  20. data/lib/rubypitaya/core/app/services/mongo_service.rb +35 -0
  21. data/lib/rubypitaya/core/{redis_service.rb → app/services/redis_service.rb} +0 -2
  22. data/lib/rubypitaya/core/database_config.rb +9 -9
  23. data/lib/rubypitaya/core/database_connector.rb +6 -4
  24. data/lib/rubypitaya/core/handler_base.rb +1 -2
  25. data/lib/rubypitaya/core/handler_router.rb +3 -3
  26. data/lib/rubypitaya/core/helpers/setup_helper.rb +2 -3
  27. data/lib/rubypitaya/core/http_routes.rb +0 -1
  28. data/lib/rubypitaya/core/initializer_content.rb +2 -3
  29. data/lib/rubypitaya/core/main.rb +4 -19
  30. data/lib/rubypitaya/core/session.rb +8 -0
  31. data/lib/rubypitaya/core/setup.rb +4 -0
  32. data/lib/rubypitaya/core/spec-helpers/config_spec_helper.rb +11 -1
  33. data/lib/rubypitaya/core/spec-helpers/handler_spec_helper.rb +33 -25
  34. data/lib/rubypitaya/core/spec-helpers/rubypitaya_spec_helper.rb +9 -0
  35. data/lib/rubypitaya/core/spec-helpers/setup_spec_helper.rb +22 -4
  36. data/lib/rubypitaya/version.rb +1 -1
  37. metadata +9 -4
  38. data/lib/rubypitaya/core/mongo_connector.rb +0 -25
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bea764ce3cb4595677dc5ff2b0af555d0fa0db47500e0ae28a4431275ff0b3d0
4
- data.tar.gz: b4d3ac9a4b66605396f1fd27fb83618e8897f5284e6fc9b97426b0ece545ef10
3
+ metadata.gz: 12a31604406460247ee5d2596f52358a59f6b60f3b2765f4262b78525004cbd5
4
+ data.tar.gz: 74b634075206caaddae86a7726e2f7fb0c5e6e7e5e1ce0c4a4c7e1f615d7a2e7
5
5
  SHA512:
6
- metadata.gz: 38152d121b929079ba9315832bfac315771bb640c7cabbd7c66fdd286662706c54f9a0412d575bc2024e3aa929aae950c679c6189969ecfe454e41d333e38fb7
7
- data.tar.gz: c3b9b060a58f659a32fbff7baa950eade738672d426e1aaadb295df58bed56cbd5ddde2b09f1ce3a8caca47c4b1ce85e086b10598798c858fb45fdc0483604cb
6
+ metadata.gz: 673c27cb2dfea753dc3d81e95b04424d838c533dafbd262730a7c82a278ad35c9355aaf17a72e2156e9919de1987d47dadfb6728e31958f6ff6d3343acf9f175
7
+ data.tar.gz: d5e2813b687030005f55ef15acdca2f06c2f22075be9fb0db54f05d50333a7fc92e8ef478fa350a6bdab4bc78c7e830e3d55896c3c821ba9e3f6283f3e71075f
@@ -1,6 +1,6 @@
1
1
  source "https://rubygems.org"
2
2
 
3
- gem 'rubypitaya', '3.5.0'
3
+ gem 'rubypitaya', '3.8.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.5.0)
102
+ rubypitaya (3.8.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.5.0)
141
+ rubypitaya (= 3.8.0)
142
142
  sinatra-contrib (= 2.1.0)
143
143
 
144
144
  BUNDLED WITH
@@ -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,4 +1,5 @@
1
- require 'rubypitaya/core/redis_service'
1
+ require 'rubypitaya/core/app/services/mongo_service'
2
+ require 'rubypitaya/core/app/services/redis_service'
2
3
 
3
4
  class AppInitializer < RubyPitaya::InitializerBase
4
5
 
@@ -8,10 +9,6 @@ class AppInitializer < RubyPitaya::InitializerBase
8
9
  # - services
9
10
  # - link: https://gitlab.com/LucianoPC/ruby-pitaya/-/blob/master/lib/rubypitaya/core/service_holder.rb
10
11
  #
11
- # - mongo
12
- # - class: Mongo::Client
13
- # - link: https://docs.mongodb.com/ruby-driver/current/tutorials/quick-start/
14
- #
15
12
  # - config
16
13
  # - class: RubyPitaya::Config
17
14
  # - link: https://gitlab.com/LucianoPC/ruby-pitaya/-/blob/master/lib/rubypitaya/core/config.rb
@@ -30,15 +27,20 @@ class AppInitializer < RubyPitaya::InitializerBase
30
27
  # - methods:
31
28
  # - info
32
29
  # - log information
33
-
30
+ #
34
31
  # services:
35
32
  # - redis
36
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/
37
38
 
38
39
  def run(initializer_content)
39
40
  setup = initializer_content.setup
40
41
  services = initializer_content.services
41
42
 
43
+ services.add(:mongo, RubyPitaya::MongoService.new(setup))
42
44
  services.add(:redis, RubyPitaya::RedisService.new(setup))
43
45
 
44
46
  playerBll = PlayerBLL.new
@@ -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
@@ -9,7 +9,7 @@ class ErrorHandler < RubyPitaya::HandlerBase
9
9
  raise RubyPitaya::RouteError.new(error_code, error_message)
10
10
 
11
11
  response = {
12
- code: 'RP-200',
12
+ code: StatusCodes::CODE_OK,
13
13
  data: {
14
14
  message: 'Ok!'
15
15
  }
@@ -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,10 +8,6 @@ class PlayerHandler < RubyPitaya::HandlerBase
8
8
  # - [](key)
9
9
  # - info: get object by key
10
10
  #
11
- # - @mongo
12
- # - class: Mongo::Client
13
- # - link: https://docs.mongodb.com/ruby-driver/current/tutorials/quick-start/
14
- #
15
11
  # - @config
16
12
  # - info: Hash with config json files inside of 'app/config'
17
13
  # - example:
@@ -41,6 +37,14 @@ class PlayerHandler < RubyPitaya::HandlerBase
41
37
  # of the session, for example you can set an userId on
42
38
  # @session, like `@session.uid = '123'`, and then you can
43
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/
44
48
 
45
49
  non_authenticated_actions :authenticate
46
50
 
@@ -55,10 +59,7 @@ class PlayerHandler < RubyPitaya::HandlerBase
55
59
  bind_session_response = @postman.bind_session(@session)
56
60
 
57
61
  unless bind_session_response.dig(:error, :code).nil?
58
- return response = {
59
- code: RubyPitaya::StatusCodes::CODE_AUTHENTICATION_ERROR,
60
- msg: 'Error to authenticate',
61
- }
62
+ raise RubyPitaya::RouteError.new(StatusCodes::CODE_AUTHENTICATION_ERROR, 'Error to authenticate')
62
63
  end
63
64
 
64
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
@@ -0,0 +1,3 @@
1
+ #!/bin/bash
2
+
3
+ bundle exec rake db:reset
@@ -0,0 +1,4 @@
1
+ #!/bin/bash
2
+
3
+ echo "Setup database"
4
+ ./setup/development/database
@@ -0,0 +1,3 @@
1
+ #!/bin/bash
2
+
3
+ bundle exec rake db:test:setup
@@ -0,0 +1,4 @@
1
+ #!/bin/bash
2
+
3
+ echo "Setup database"
4
+ ./setup/test/database
@@ -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
@@ -5,8 +5,6 @@ module RubyPitaya
5
5
 
6
6
  class RedisService < ServiceBase
7
7
 
8
- attr_reader :redis
9
-
10
8
  def initialize(setup)
11
9
  @redis = nil
12
10
  @redis_address = setup['rubypitaya.redis.url']
@@ -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
 
@@ -21,10 +21,9 @@ module RubyPitaya
21
21
  @objects = self.class.objects
22
22
  end
23
23
 
24
- def set_attributes(log, services, mongo, setup, config, params, session, postman)
24
+ def set_attributes(log, services, setup, config, params, session, postman)
25
25
  @log = log
26
26
  @services = services
27
- @mongo = mongo
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, services, 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, services, 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, services, 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,7 +24,6 @@ module RubyPitaya
24
24
  content_type :json
25
25
 
26
26
  @log = settings.log
27
- @mongo = settings.mongo
28
27
  @setup = settings.setup
29
28
  @services = settings.services
30
29
  @config = settings.config
@@ -2,12 +2,11 @@ module RubyPitaya
2
2
 
3
3
  class InitializerContent
4
4
 
5
- attr_reader :log, :services, :mongo, :setup, :config
5
+ attr_reader :log, :services, :setup, :config
6
6
 
7
- def initialize(log, services, mongo, setup, config)
7
+ def initialize(log, services, setup, config)
8
8
  @log = log
9
9
  @services = services
10
- @mongo = mongo
11
10
  @setup = setup
12
11
  @config = config
13
12
  end
@@ -17,9 +17,7 @@ require 'rubypitaya/core/etcd_connector'
17
17
  require 'rubypitaya/core/handler_router'
18
18
  require 'rubypitaya/core/nats_connector'
19
19
  require 'rubypitaya/core/service_holder'
20
- require 'rubypitaya/core/database_config'
21
20
  require 'rubypitaya/core/instance_holder'
22
- require 'rubypitaya/core/mongo_connector'
23
21
  require 'rubypitaya/core/database_connector'
24
22
  require 'rubypitaya/core/initializer_content'
25
23
  require 'rubypitaya/core/initializer_broadcast'
@@ -64,19 +62,9 @@ module RubyPitaya
64
62
  @nats_connector = NatsConnector.new(@nats_address, @service_uuid,
65
63
  @server_name)
66
64
 
67
- @database_config = DatabaseConfig.new
68
- @database_connector = DatabaseConnector.new(@database_config, @log)
65
+ @database_connector = DatabaseConnector.new(@setup, @log)
69
66
  @database_connector.connect
70
67
 
71
- @mongo_enabled = @setup['rubypitaya.mongo.enabled']
72
- @mongo_address = @setup['rubypitaya.mongo.url']
73
- @mongo_user = @setup['rubypitaya.mongo.user']
74
- @mongo_password = @setup['rubypitaya.mongo.pass']
75
- @mongo_database_name = @setup['rubypitaya.mongo.database']
76
- @mongo_connector = MongoConnector.new(@mongo_address, @mongo_user,
77
- @mongo_password, @mongo_database_name)
78
- @mongo_connector.connect if @mongo_enabled
79
-
80
68
  @session = Session.new
81
69
  @postman = Postman.new(@nats_connector)
82
70
  @config = Config.new
@@ -88,7 +76,6 @@ module RubyPitaya
88
76
 
89
77
  @initializer_content = InitializerContent.new(@log,
90
78
  @services,
91
- @mongo_connector.mongo,
92
79
  @setup,
93
80
  @config)
94
81
  @initializer_broadcast = InitializerBroadcast.new
@@ -115,7 +102,6 @@ module RubyPitaya
115
102
  def run_http
116
103
  HttpRoutes.set :log, @log
117
104
  HttpRoutes.set :services, @services
118
- HttpRoutes.set :mongo, @mongo_connector.mongo
119
105
  HttpRoutes.set :setup, @setup
120
106
  HttpRoutes.set :config, @config
121
107
  HttpRoutes.set :objects, @objects
@@ -196,17 +182,16 @@ module RubyPitaya
196
182
 
197
183
  begin
198
184
  response = @handler_router.call(handler_name, action_name, @session,
199
- @postman, @services,
200
- @mongo_connector.mongo, @setup, @config,
185
+ @postman, @services, @setup, @config,
201
186
  @log, params)
202
187
  rescue RouteError => error
203
- @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")}"
204
189
  response = {
205
190
  code: error.code,
206
191
  message: error.message
207
192
  }
208
193
  rescue Exception => error
209
- @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")}"
210
195
  response = {
211
196
  code: StatusCodes::CODE_INTERNAL_ERROR,
212
197
  message: StatusCodes::MESSAGE_INTERNAL_ERROR,
@@ -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
@@ -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
20
  @@services = ServiceHolder.new
21
21
 
22
- @@response = {}
23
-
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
29
  @@services,
32
- @@mongo_connector.mongo,
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, @@services,
44
- @@mongo_connector.mongo, @@setup,
57
+ @@postman, @@services, @@setup,
45
58
  @@config, @@log, params)
46
59
  rescue RouteError => error
47
60
  @@response = {
@@ -104,21 +117,16 @@ module RubyPitaya
104
117
 
105
118
  private
106
119
 
107
- def initialize_mongo
108
- @@mongo_connector ||= nil
109
-
110
- if @@mongo_connector.nil?
111
- @@core_setup ||= Setup.new
112
- mongo_address = @@core_setup['rubypitaya.mongo.url']
113
- mongo_user = @@core_setup['rubypitaya.mongo.user']
114
- mongo_password = @@core_setup['rubypitaya.mongo.pass']
115
- mongo_database_name = @@core_setup['rubypitaya.mongo.database']
116
- @@mongo_connector = MongoConnector.new(mongo_address, mongo_user,
117
- mongo_password, mongo_database_name)
118
- @@mongo_connector.connect
119
- end
120
-
121
- @@mongo_connector.mongo.collections.map(&:drop)
120
+ def self.connect_services
121
+ @@services.all_services.map(&:connect)
122
+ end
123
+
124
+ def self.disconnect_services
125
+ @@services.all_services.map(&:disconnect)
126
+ end
127
+
128
+ def self.clear_all_services_data
129
+ @@services.all_services.map(&:clear_all_data)
122
130
  end
123
131
  end
124
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.5.0'
2
+ VERSION = '3.8.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.5.0
4
+ version: 3.8.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-03-01 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
@@ -330,11 +330,18 @@ files:
330
330
  - "./lib/rubypitaya/app-template/kubernetes/statefulset-postgres.yaml"
331
331
  - "./lib/rubypitaya/app-template/kubernetes/statefulset-redis.yaml"
332
332
  - "./lib/rubypitaya/app-template/plugins.yaml"
333
+ - "./lib/rubypitaya/app-template/setup/development/database"
334
+ - "./lib/rubypitaya/app-template/setup/development/main"
335
+ - "./lib/rubypitaya/app-template/setup/test/database"
336
+ - "./lib/rubypitaya/app-template/setup/test/main"
333
337
  - "./lib/rubypitaya/app-template/spec/hello_world_handler_spec.rb"
334
338
  - "./lib/rubypitaya/app-template/spec/player_handler_spec.rb"
339
+ - "./lib/rubypitaya/app-template/spec/redis_service_spec.rb"
335
340
  - "./lib/rubypitaya/app-template/spec/spec_helper.rb"
336
341
  - "./lib/rubypitaya/core/app/migrations/0000000001_create_user_migration.rb"
337
342
  - "./lib/rubypitaya/core/app/models/user.rb"
343
+ - "./lib/rubypitaya/core/app/services/mongo_service.rb"
344
+ - "./lib/rubypitaya/core/app/services/redis_service.rb"
338
345
  - "./lib/rubypitaya/core/application_files_importer.rb"
339
346
  - "./lib/rubypitaya/core/config.rb"
340
347
  - "./lib/rubypitaya/core/config_core.rb"
@@ -351,14 +358,12 @@ files:
351
358
  - "./lib/rubypitaya/core/initializer_content.rb"
352
359
  - "./lib/rubypitaya/core/instance_holder.rb"
353
360
  - "./lib/rubypitaya/core/main.rb"
354
- - "./lib/rubypitaya/core/mongo_connector.rb"
355
361
  - "./lib/rubypitaya/core/nats_connector.rb"
356
362
  - "./lib/rubypitaya/core/parameters.rb"
357
363
  - "./lib/rubypitaya/core/path.rb"
358
364
  - "./lib/rubypitaya/core/postman.rb"
359
365
  - "./lib/rubypitaya/core/protos/nats_connector.proto"
360
366
  - "./lib/rubypitaya/core/protos/nats_connector_pb.rb"
361
- - "./lib/rubypitaya/core/redis_service.rb"
362
367
  - "./lib/rubypitaya/core/route_error.rb"
363
368
  - "./lib/rubypitaya/core/routes_base.rb"
364
369
  - "./lib/rubypitaya/core/service_base.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