Class: Simple::Httpd::BaseController

Inherits:
Sinatra::Base
  • Object
show all
Extended by:
Reloader, RouteDescriptions, ServiceIntegration
Defined in:
lib/simple/httpd/base_controller/x_processing.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/origin.rb,
lib/simple/httpd/base_controller/result.rb,
lib/simple/httpd/base_controller/request.rb,
lib/simple/httpd/base_controller/build_url.rb,
lib/simple/httpd/base_controller/error_handling.rb,
lib/simple/httpd/base_controller.rb,
lib/simple/httpd/base_controller.rb

Overview

Simple::SQL connection handling

Defined Under Namespace

Classes: LoginRequiredError, NotAuthorizedError, NotFoundError

Constant Summary collapse

H =
::Simple::Httpd::Helpers

Instance Attribute Summary

Attributes included from Reloader

#reloading_paths

Instance Method Summary collapse

Methods included from ServiceIntegration

delete, get, head, mount_service, post, put

Methods included from Reloader

attach, load!, reload!

Methods included from RouteDescriptions

describe_route!, route_descriptions

Instance Method Details

#dispatch!Object



12
13
14
15
16
# File 'lib/simple/httpd/base_controller.rb', line 12

def dispatch!
  self.class.reload! if ::Simple::Httpd.env == "development"

  super
end

#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:



121
122
123
# File 'lib/simple/httpd/base_controller/error_handling.rb', line 121

def (msg)
  raise LoginRequiredError, msg
end

#not_authorized!(msg) ⇒ Object

Raises:



106
107
108
# File 'lib/simple/httpd/base_controller/error_handling.rb', line 106

def not_authorized!(msg)
  raise NotAuthorizedError, msg
end

#not_found!(msg) ⇒ Object

Raises:



136
137
138
# File 'lib/simple/httpd/base_controller/error_handling.rb', line 136

def not_found!(msg)
  raise NotFoundError, msg
end

#parsed_bodyObject



2
3
4
5
6
7
8
# File 'lib/simple/httpd/base_controller/request.rb', line 2

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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/simple/httpd/base_controller/error_handling.rb', line 38

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
  options["@request"] = "#{request.request_method} #{request.path}"

  if Simple::Httpd.env == "development" || Simple::Httpd.env == "test"
    options["@headers"] = request_headers_for_debugging
    options["@backtrace"] = exc.backtrace[0, 10]
  end

  json options
end

#request_headers_for_debuggingObject



61
62
63
64
65
66
67
68
# File 'lib/simple/httpd/base_controller/error_handling.rb', line 61

def request_headers_for_debugging
  request.headers.each_with_object({}) do |(key, value), hsh|
    next if /^(Host|Version|Connection|User-Agent|Accept-Encoding)$/ =~ key
    next if key == "Accept" && value == "*/*"

    hsh[key] = value
  end
end