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



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

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

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

Yields:



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

def self.configure
  yield config
end

.startObject



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

def self.start
  instance || new

  instance.start
end

Instance Method Details

#call(env) ⇒ Object



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
100
# File 'lib/orbit.rb', line 75

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(requested_path)

    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



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

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
  use_protection
end

#setup_builderObject



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

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

#startObject



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

def start
  builder.run self
  builder
end

#use_protectionObject



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

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_sessionObject



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

def use_session
  options = {}
  options[:secret] = config.session_secret

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