Class: Orbit::Application

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/orbit.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Singleton

included, #instantiate

Constructor Details

#initializeApplication

Returns a new instance of Application.



13
14
15
16
17
18
19
20
21
# File 'lib/orbit.rb', line 13

def initialize
  instantiate

  setup_builder
  load_middleware

  loader = Loaders::DirectoryLoader.load
  @reloader = loader.reloader
end

Instance Attribute Details

#builderObject (readonly)

Returns the value of attribute builder.



11
12
13
# File 'lib/orbit.rb', line 11

def builder
  @builder
end

Class Method Details

.configObject



51
52
53
# File 'lib/orbit.rb', line 51

def self.config
  @config ||= Orbit::Config.instance
end

.configure {|config| ... } ⇒ Object

Yields:



59
60
61
# File 'lib/orbit.rb', line 59

def self.configure
  yield config
end

.startObject



63
64
65
66
67
# File 'lib/orbit.rb', line 63

def self.start
  instance || new

  instance.start
end

Instance Method Details

#call(env) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/orbit.rb', line 74

def call(env)
  @reloader.reload
  @request = config.request_class.new(env)
  verb = @request.request_method
  requested_path = @request.path_info

  route = Config.router_class.match(verb, requested_path)

  if route
    intercepted = Interceptors::List.intercept_path(@request)

    return intercepted if intercepted

    route_params = route[:route].path.get_params(requested_path) || {}

    @request.params.merge!(route_params)

    begin
      route[:class].execute_action(@request, route[:action])
    rescue Exception => exception
      Config.response_class.server_error(exception, verb, requested_path)
    end
  else
    Config.response_class.not_found(verb, requested_path)
  end
end

#configObject



55
56
57
# File 'lib/orbit.rb', line 55

def config
  self.class.config
end

#load_middlewareObject



27
28
29
30
31
32
33
34
35
# File 'lib/orbit.rb', line 27

def load_middleware
  builder.use Rack::MethodOverride
  builder.use Rack::Head
  builder.use Rack::Static, :urls => Config.static_files_path
  builder.use config.rack_logger_class

  use_session(config.session_options)
  use_protection
end

#setup_builderObject



23
24
25
# File 'lib/orbit.rb', line 23

def setup_builder
  @builder = Rack::Builder.new
end

#startObject



69
70
71
72
# File 'lib/orbit.rb', line 69

def start
  builder.run self
  builder
end

#use_protectionObject



43
44
45
46
47
48
49
# File 'lib/orbit.rb', line 43

def use_protection
  options = {}
  options[:except] = Array options[:except]
  options[:except] += [:session_hijacking, :remote_token]
  options[:reaction] ||= :drop_session
  builder.use Rack::Protection, options
end

#use_session(options) ⇒ Object



37
38
39
40
41
# File 'lib/orbit.rb', line 37

def use_session(options)
  options[:secret] = config.session_secret

  builder.use Rack::Session::Cookie, options
end