Class: Simple::Httpd::BaseController

Inherits:
Sinatra::Base
  • Object
show all
Extended by:
Service::ControllerAdapter
Defined in:
lib/simple/httpd/base_controller/x_processing.rb,
lib/simple/httpd/base_controller.rb,
lib/simple/httpd/base_controller/cors.rb,
lib/simple/httpd/base_controller/json.rb,
lib/simple/httpd/base_controller/debug.rb,
lib/simple/httpd/base_controller/build_url.rb,
lib/simple/httpd/base_controller/error_handling.rb

Overview

rubocop:disable Metrics/ClassLength

Defined Under Namespace

Classes: LoginRequiredError, NotAuthorizedError, NotFoundError

Constant Summary collapse

CORS_HEADERS =
{
  "access-control-allow-credentials" => "true",
  "access-control-allow-headers" => "Origin,X-Requested-With,Content-Type,Accept,Session-Id",
  "access-control-allow-methods" => "*",
  "access-control-allow-origin" => "*",
  # "access-control-expose-headers" => "X-Total-Entries,X-Total-Pages,X-Page,X-Per-Page",
  # "access-control-max-age" => "-1",
  "access-control-max-age" => "600",
  "access-control-request-headers" => "Content-Type",
  "access-control-request-method" => "GET,POST,PATCH,PUT,DELETE,OPTIONS"
}
H =
::Simple::Httpd::Helpers

Instance Method Summary collapse

Methods included from Service::ControllerAdapter

delete, get, head, mount_service, post, put

Instance Method Details

#json(result) ⇒ Object



2
3
4
5
# File 'lib/simple/httpd/base_controller/json.rb', line 2

def json(result)
  content_type :json
  generate_json(result)
end

#login_required!(msg) ⇒ Object

Raises:



97
98
99
# File 'lib/simple/httpd/base_controller/error_handling.rb', line 97

def (msg)
  raise LoginRequiredError, msg
end

#not_authorized!(msg) ⇒ Object

Raises:



88
89
90
# File 'lib/simple/httpd/base_controller/error_handling.rb', line 88

def not_authorized!(msg)
  raise NotAuthorizedError, msg
end

#not_found!(msg) ⇒ Object

Raises:



112
113
114
# File 'lib/simple/httpd/base_controller/error_handling.rb', line 112

def not_found!(msg)
  raise NotFoundError, msg
end

#parsed_bodyObject



32
33
34
35
36
37
38
# File 'lib/simple/httpd/base_controller/json.rb', line 32

def parsed_body
  return @parsed_body if defined? @parsed_body

  @parsed_body = parse_body
rescue RuntimeError => e
  raise ArgumentError, e.to_s
end

#render_error(exc, options) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/simple/httpd/base_controller/error_handling.rb', line 27

def render_error(exc, options)
  expect! options => {
    status: Integer,
    title: String,
    description: [String, nil]
  }

  status options[:status]

  options = stringify_hash(options)
  options["description"] ||= options["title"]
  options["@type"] = error_type(exc)
  options["@now"] = Time.now.to_f

  json options
end