Class: RubyPitaya::HandlerRouter

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

Instance Method Summary collapse

Constructor Details

#initializeHandlerRouter

Returns a new instance of HandlerRouter.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rubypitaya/core/handler_router.rb', line 9

def initialize()
  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 Method Details

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



57
58
59
60
61
62
63
64
65
66
67
68
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
# File 'lib/rubypitaya/core/handler_router.rb', line 57

def call(handler_name, action_name, session, postman, redis, setup, config,
         bll, log, params)
  unless @handler_name_map.include?(handler_name)
    return {
      code: StatusCodes::CODE_HANDLER_NOT_FOUND,
      msg: "Handler #{handler_name} not founded"
    }
  end

  unless @handler_name_map[handler_name].methods.include?(action_name.to_sym)
    return {
      code: StatusCodes::CODE_ACTION_NOT_FOUND,
      msg: "Handler #{handler_name} action #{action_name} not founded"
    }
  end

  handler = @handler_name_map[handler_name]

  handler.bll = bll
  handler.log = log
  handler.redis = redis
  handler.setup = setup
  handler.config = config
  handler.params = params
  handler.session = session
  handler.postman = postman

  if !handler.class.authenticated_action_name?(action_name)
    handler.send(action_name)
  else
    if session.authenticated?
      handler.send(action_name)
    else
      return {
        code: StatusCodes::CODE_NOT_AUTHENTICATED,
        msg: 'Not authenticated',
      }
    end
  end
end

#import_handler_classesObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rubypitaya/core/handler_router.rb', line 39

def import_handler_classes
  handler_classes = ObjectSpace.each_object(HandlerBase.singleton_class).select do |klass|
    klass != HandlerBase
  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



35
36
37
# File 'lib/rubypitaya/core/handler_router.rb', line 35

def import_handler_files(handler_folder_path)
  Gem.find_files("#{handler_folder_path}/*.rb").each { |path| require path }
end

#import_routes_classObject



27
28
29
30
31
32
33
# File 'lib/rubypitaya/core/handler_router.rb', line 27

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



23
24
25
# File 'lib/rubypitaya/core/handler_router.rb', line 23

def import_routes_file(routes_path)
  require routes_path
end