Class: Lux::Application

Inherits:
Object show all
Includes:
ClassCallbacks, Routes, Shared
Defined in:
lib/lux/application/lib/nav.rb,
lib/lux/application/lib/render.rb,
lib/lux/application/lib/routes.rb,
lib/lux/application/lib/shared.rb,
lib/lux/application/application.rb,
lib/lux/application/lib/magic_routes.rb

Defined Under Namespace

Modules: Render, Routes, Shared Classes: Nav

Instance Method Summary collapse

Methods included from Shared

#body?, #current, #nav, #params, #redirect_to, #request, #response, #session, #user

Methods included from Routes

#call, #map, #match, #root, #subdomain, #test?

Constructor Details

#initialize(env, opts = {}) ⇒ Application

Returns a new instance of Application.



16
17
18
# File 'lib/lux/application/application.rb', line 16

def initialize env, opts={}
  Lux::Current.new env, opts
end

Instance Method Details

#favicon(path) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/lux/application/application.rb', line 99

def favicon path
  cpath = request.path.downcase

  if !response.body? && cpath.start_with?('/favicon') || cpath.start_with?('/apple-touch-icon')
    response.max_age = 600 if response.max_age.to_i == 0

    icon = Lux.root.join(path)
    if icon.exist?
      response.send_file(icon, inline: true)
    else
      Lux.error.not_found '%s not found' % path
    end
  end
end

#mount(opts) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/lux/application/application.rb', line 83

def mount opts
  target = opts.keys.first
  value  = opts.values.first

  return unless request.path.to_s.starts_with?(value)

  call target.call current.env
end

#renderObject

to get root page body Lux.app.new(‘/’).render.body



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/lux/application/application.rb', line 69

def render
  out  = @response_render ||= render_base
  body = out[2].join('')
  body = JSON.parse body if out[1]['content-type'].index('/json')

  {
    body:    body,
    time:    out[1]['x-lux-speed'],
    status:  out[0],
    session: current.session.hash,
    headers: out[1]
  }.to_hwia
end

#render_baseObject

main render called by Lux.call



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/lux/application/application.rb', line 21

def render_base
  if Lux.env.reload_code? && Lux.env.web?
    Lux.config.on_reload_code.call
  end

  request_method = request.request_method

  Lux.log ''
  Lux.log { [request_method.white, request.url].join(' ') }

  if request.post?
    Lux.log { request.params.to_h.to_jsonp }
  end

  if request_method == 'OPTIONS'
    return [204, {
      'allow' => Lux.config[:request_options] || 'OPTIONS, GET, HEAD, POST',
      'cache-control' => 'max-age=604800',
    }, ['']]
  end

  catch :done do
    if Lux.config.serve_static_files
      Lux::Response::File.deliver_from_current
    end

    resolve_routes unless response.body?
  end

  catch :done do
    Lux.error.not_found unless response.body?
  end

  response.render
rescue => error
  if respond_to?(:rescue_from)
    catch :done do
      rescue_from error
    end

    response.render
  else
    raise error
  end
end

#render_errorObject



92
93
94
95
96
97
# File 'lib/lux/application/application.rb', line 92

def render_error
  err = Lux.current.error ||= $!
  Lux.info "Unhandled error (define render_error in routes): [#{err.class}] #{err.message}"
  Lux.error.log err
  raise err
end

#resolve_routesObject

internall call to resolve the routes



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/lux/application/application.rb', line 115

def resolve_routes
  @magic = MagicRoutes.new self

  run_callback :before, nav.path
  run_callback :routes, nav.path

  unless response.body?
    Lux.error.not_found 'Document not found'
  end

  run_callback :after, nav.path
end