class RubyAPI::Application

Application

Constants

APP_MODULE_NAME
APP_SOURCE_PATH

Attributes

config[R]
logger[R]
routes[R]

Public Class Methods

new(config) click to toggle source
# File lib/ruby_api/application.rb, line 11
def initialize(config)
  raise "Invalid config: #{config.inspect}" unless config.is_a?(Config)
  @config = config
  @routes = load_routes
end

Public Instance Methods

app_module() click to toggle source
# File lib/ruby_api/application.rb, line 43
def app_module
  Kernel.const_get(app_name)
end
boot() click to toggle source
# File lib/ruby_api/application.rb, line 26
def boot
  @logger = Logger.new log_path[:info]
  @logger_err = Logger.new log_path[:error]
  require RubyAPI.path(boot_script) if boot_script?
  require RubyAPI.path(app_path)
  self
end
call(env) click to toggle source
# File lib/ruby_api/application.rb, line 34
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_routes() click to toggle source
# File lib/ruby_api/application.rb, line 17
def load_routes
  return {} unless config.key?(:routes)

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

Private Instance Methods

app_name() click to toggle source
# File lib/ruby_api/application.rb, line 57
def app_name
  name = config.app[:name] if config.key?(:app) && config.app[:name]
  name || APP_MODULE_NAME
end
app_path() click to toggle source
# File lib/ruby_api/application.rb, line 62
def app_path
  return APP_SOURCE_PATH unless config.key?(:app)
  return APP_SOURCE_PATH unless config.app.key?(:source)
  config.app[:source]
end
boot_script() click to toggle source
# File lib/ruby_api/application.rb, line 68
def boot_script
  config.boot_script
end
boot_script?() click to toggle source
# File lib/ruby_api/application.rb, line 72
def boot_script?
  return false unless config.key?(:boot_script)
  path = RubyAPI.path(boot_script)
  return true if File.exist?(path)
  logger.warn "Boot script not found at: #{path}"
  false
end
handle_error(response) click to toggle source
# File lib/ruby_api/application.rb, line 90
def handle_error(response)
  @logger_err.error response.data
  rack_response Response::OK, response.headers, response.data
end
handle_exception(error) click to toggle source
# File lib/ruby_api/application.rb, line 95
def handle_exception(error)
  @logger_err.error error
  rack_response Response::FAIL, Response::DEFAULT_HEADERS,
                Response.exception(error).data
end
handle_success(response) click to toggle source
# File lib/ruby_api/application.rb, line 86
def handle_success(response)
  rack_response response.status, response.headers, response.data
end
log_path() click to toggle source
# File lib/ruby_api/application.rb, line 49
def log_path
  if config.key?(:log)
    { info: config.log[:info], error: config.log[:error] }
  else
    { info: $stdout, error: $stderr }
  end
end
process(request) click to toggle source
# File lib/ruby_api/application.rb, line 80
def process(request)
  operation = request.operation
  result = operation.process(self, request)
  Response.operation(result)
end
rack_response(status, headers, data) click to toggle source
# File lib/ruby_api/application.rb, line 101
def rack_response(status, headers, data)
  data = [data] unless data.is_a?(Array)
  [status, headers || {}, data]
end
require_hash_or_string(object, message = 'Invalid Hash/String: %s') click to toggle source
# File lib/ruby_api/application.rb, line 106
def require_hash_or_string(object, message = 'Invalid Hash/String: %s')
  return if object.is_a?(Hash) || object.is_a?(String)
  raise format(message, object.inspect)
end