Harbor Documentation

Attributes

  • router [R] (Not documented)
  • environment [R] (Not documented)
  • services [R] (Not documented)

Public Class Methods

new(services, *args)

      # File lib/harbor/application.rb, line 33
33:     def initialize(services, *args)
34:       unless services.is_a?(Harbor::Container)
35:         raise ArgumentError.new("Harbor::Application#services must be a Harbor::Container")
36:       end
37: 
38:       @services = services
39: 
40:       @router = (!args.empty? && !args[0].is_a?(String) && args[0].respond_to?(:match)) ? args.shift : self.class.routes(@services)
41:       @environment = args.last || "development"
42:     end

routes(services)

Routes are defined in this method. Note that Harbor does not define any default routes, so you must reimplement this method in your application.

      # File lib/harbor/application.rb, line 27
27:     def self.routes(services)
28:       raise NotImplementedError.new("Your application must redefine #{self}::routes.")
29:     end

Public Instance Methods

call(env)

Request entry point called by Rack. It creates a request and response object based on the incoming request environment, checks for public files, and dispatches the request.

It returns a rack response hash.

      # File lib/harbor/application.rb, line 51
51:     def call(env)
52:       env["APP_ENVIRONMENT"] = environment
53:       request = Request.new(self, env)
54:       response = Response.new(request)
55: 
56:       catch(:abort_request) do
57:         if file = find_public_file(request.path_info[1..-1])
58:           response.cache(nil, ::File.mtime(file), 86400) do
59:             response.stream_file(file)
60:           end
61: 
62:           return response.to_a
63:         end
64: 
65:         handler = @router.match(request)
66: 
67:         dispatch_request(handler, request, response)
68:       end
69: 
70:       response.to_a
71:     end

default_layout()

       # File lib/harbor/application.rb, line 153
153:     def default_layout
154:       warn "Harbor::Application#default_layout has been deprecated. See Harbor::Layouts."
155:     end

dispatch_request(handler, request, response)

Request dispatch function, which handles 404’s, exceptions, and logs requests.

      # File lib/harbor/application.rb, line 77
77:     def dispatch_request(handler, request, response)
78:       dispatch_request_event = Events::DispatchRequestEvent.new(request, response)
79:       raise_event2(:request_dispatch, dispatch_request_event)
80: 
81:       return handle_not_found(request, response) unless handler
82:       
83:       handler.call(request, response)
84:     rescue StandardError, LoadError, SyntaxError => e
85:       handle_exception(e, request, response)
86:     ensure
87:       raise_event2(:request_complete, dispatch_request_event.complete!)
88:     end

handle_exception(exception, request, response)

Method used to nicely handle uncaught exceptions.

Logs full error messages to the configured ‘error’ logger.

By default, it will render “We’re sorry, but something went wrong.“

To use a custom 500 message, create a view “exceptions/500.html.erb“, and optionally create a view “layouts/exception.html.erb“ to style it.

       # File lib/harbor/application.rb, line 124
124:     def handle_exception(exception, request, response)
125:       response.flush
126:       response.status = 500
127: 
128:       if environment == "development"
129:         response.content_type = "text/html"
130:         response.puts(Rack::ShowExceptions.new(nil).pretty(request.env, exception))
131:       else
132:         response.layout = "layouts/exception" if Harbor::View.exists?("layouts/exception")
133: 
134:         if Harbor::View.exists?("exceptions/500.html.erb")
135:           response.render "exceptions/500.html.erb", :exception => exception
136:         else
137:           response.puts "We're sorry, but something went wrong."
138:         end
139:       end
140: 
141:       raise_event2(:exception, ApplicationExceptionEvent.new(request, response, exception))
142: 
143:       nil
144:     end

handle_not_found(request, response)

Method used to nicely handle cases where no routes or public files match the incoming request.

By default, it will render “The page you requested could not be found”.

To use a custom 404 message, create a view “exceptions/404.html.erb“, and optionally create a view “layouts/exception.html.erb“ to style it.

       # File lib/harbor/application.rb, line 99
 99:     def handle_not_found(request, response)      
100:       response.flush
101:       response.status = 404
102: 
103:       response.layout = "layouts/exception" if Harbor::View.exists?("layouts/exception")
104: 
105:       if Harbor::View.exists?("exceptions/404.html.erb")
106:         response.render "exceptions/404.html.erb"
107:       else
108:         response.puts "The page you requested could not be found"
109:       end
110: 
111:       raise_event2(:not_found, Events::NotFoundEvent.new(request, response))
112:     end