Class: RubyPitaya::HandlerRouter

Inherits:
Object
  • Object
show all
Defined in:
lib/rubypitaya/core/handler_router.rb

Constant Summary collapse

INVALID_ACTION_NAMES =
['initialize', 'set_attributes']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(is_cheats_enabled) ⇒ HandlerRouter

Returns a new instance of HandlerRouter.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rubypitaya/core/handler_router.rb', line 13

def initialize(is_cheats_enabled)
  @is_cheats_enabled = is_cheats_enabled

  routes_path = Path::ROUTES_FILE_PATH
  handler_folder_paths = Path::Plugins::HANDLERS_FOLDER_PATHS + [Path::HANDLERS_FOLDER_PATH]

  import_routes_file(routes_path)

  handler_folder_paths.each do |handler_folder_path|
    import_handler_files(handler_folder_path)
  end

  import_routes_class
  import_handler_classes
end

Instance Attribute Details

#handlersObject (readonly)

Returns the value of attribute handlers.



11
12
13
# File 'lib/rubypitaya/core/handler_router.rb', line 11

def handlers
  @handlers
end

Instance Method Details

#call(handler_name, action_name, session, postman, services, setup, config, log, params) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rubypitaya/core/handler_router.rb', line 69

def call(handler_name, action_name, session, postman, services, setup,
         config, log, params)
  unless @handler_name_map.include?(handler_name)
    return {
      code: StatusCodes::CODE_HANDLER_NOT_FOUND,
      message: "Handler #{handler_name} not found"
    }
  end
  if INVALID_ACTION_NAMES.include?(action_name) ||
     !@handler_name_map[handler_name].public_methods(false).include?(action_name.to_sym)
    return {
      code: StatusCodes::CODE_ACTION_NOT_FOUND,
      message: "Handler #{handler_name} action #{action_name} not found"
    }
  end

  handler = @handler_name_map[handler_name]

  if !handler.class.authenticated_action_name?(action_name)
    handler.set_attributes(log, services, setup, config, params, session, postman)
    handler.send(action_name)
  else
    if session.authenticated?
      handler.set_attributes(log, services, setup, config, params, session, postman)
      handler.send(action_name)
    else
      return {
        code: StatusCodes::CODE_NOT_AUTHENTICATED,
        message: 'Not authenticated',
      }
    end
  end
end

#import_handler_classesObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rubypitaya/core/handler_router.rb', line 48

def import_handler_classes
  handler_classes = ObjectSpace.each_object(HandlerBase.singleton_class).select do |klass|
    class_name = klass.to_s.downcase
    is_cheat_class = class_name.end_with?('cheat') || class_name.end_with?('cheats')

    klass != HandlerBase && (@is_cheats_enabled || !is_cheat_class)
  end

  @handlers = handler_classes.map { |handler_class| handler_class.new }

  @handler_name_map = @handlers.map do |handler|
    handler_name = handler.class.to_s
    handler_name = @routes.get_new_handler_name(handler_name)
    handler_name = handler_name.split('::').last
    handler_name = handler_name[0].downcase + handler_name[1..-1]

    [handler_name, handler]
  end
  @handler_name_map = @handler_name_map.to_h
end

#import_handler_files(handler_folder_path) ⇒ Object



41
42
43
44
45
46
# File 'lib/rubypitaya/core/handler_router.rb', line 41

def import_handler_files(handler_folder_path)
  handler_files = Dir.glob("#{handler_folder_path}/*.rb")
  handler_files = handler_files.select { |a| !a[/.+_cheats.rb/] && !a[/.+_cheat.rb/] } unless @is_cheats_enabled

  handler_files.each { |path| require path }
end

#import_routes_classObject



33
34
35
36
37
38
39
# File 'lib/rubypitaya/core/handler_router.rb', line 33

def import_routes_class
  routes_classes = ObjectSpace.each_object(RoutesBase.singleton_class).select do |klass|
    klass != RoutesBase
  end

  @routes = routes_classes.first.new
end

#import_routes_file(routes_path) ⇒ Object



29
30
31
# File 'lib/rubypitaya/core/handler_router.rb', line 29

def import_routes_file(routes_path)
  require routes_path
end