Class: RubyAPI::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_api/application.rb

Overview

Application

Constant Summary collapse

APP_MODULE_NAME =
'APP'
APP_SOURCE_PATH =
'app/app'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cfg = {}) ⇒ Application

Returns a new instance of Application.



11
12
13
14
# File 'lib/ruby_api/application.rb', line 11

def initialize(cfg = {})
  @config = load_config(cfg)
  @routes = load_routes
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/ruby_api/application.rb', line 9

def config
  @config
end

#loggerObject (readonly)

Returns the value of attribute logger.



9
10
11
# File 'lib/ruby_api/application.rb', line 9

def logger
  @logger
end

#routesObject (readonly)

Returns the value of attribute routes.



9
10
11
# File 'lib/ruby_api/application.rb', line 9

def routes
  @routes
end

Instance Method Details

#app_moduleObject



48
49
50
# File 'lib/ruby_api/application.rb', line 48

def app_module
  Kernel.const_get(app_name)
end

#bootObject



31
32
33
34
35
36
37
# File 'lib/ruby_api/application.rb', line 31

def boot
  @logger = Logger.new log_path[:info]
  @logger_err = Logger.new log_path[:error]
  require boot_script if boot_script?
  require app_path
  self
end

#call(env) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/ruby_api/application.rb', line 39

def call(env)
  @env = env
  response = process Request.new(self, env, routes)
  return handle_error(response) if response.failure?
  handle_success(response)
rescue StandardError => e
  handle_exception(e)
end

#load_config(cfg) ⇒ Object



16
17
18
19
20
# File 'lib/ruby_api/application.rb', line 16

def load_config(cfg)
  require_hash_or_string(cfg, 'Invalid configuration: %s')
  cfg = YAML.load_file cfg if cfg.is_a?(String)
  Config.new(cfg)
end

#load_routesObject



22
23
24
25
26
27
28
29
# File 'lib/ruby_api/application.rb', line 22

def load_routes
  return {} unless config.key?(:routes)

  routes = config.routes
  require_hash_or_string(routes, 'Invalid routes config: %s')
  routes = YAML.load_file routes if routes.is_a?(String)
  @routes = routes.deep_symbolize_keys!
end