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 32
32:     def initialize(services, *args)
33:       unless services.is_a?(Harbor::Container)
34:         raise ArgumentError.new("Harbor::Application#services must be a Harbor::Container")
35:       end
36: 
37:       @services = services
38: 
39:       @router = (!args.empty? && !args[0].is_a?(String) && args[0].respond_to?(:match)) ? args.shift : self.class.routes(@services)
40:       @environment = args.last || "development"
41:     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 26
26:     def self.routes(services)
27:       raise NotImplementedError.new("Your application must redefine #{self}::routes.")
28:     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 50
50:     def call(env)
51:       env["APP_ENVIRONMENT"] = environment
52:       request = Request.new(self, env)
53:       response = Response.new(request)
54: 
55:       catch(:abort_request) do
56:         if file = find_public_file(request.path_info[1..-1])
57:           response.cache(nil, ::File.mtime(file), 86400) do
58:             response.stream_file(file)
59:           end
60: 
61:           return response.to_a
62:         end
63: 
64:         handler = @router.match(request)
65: 
66:         dispatch_request(handler, request, response)
67:       end
68: 
69:       response.to_a
70:     end

default_layout()

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

dispatch_request(handler, request, response)

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

      # File lib/harbor/application.rb, line 76
76:     def dispatch_request(handler, request, response)
77:       dispatch_request_event = Events::DispatchRequestEvent.new(request, response)
78:       raise_event2(:request_dispatch, dispatch_request_event)
79: 
80:       return handle_not_found(request, response) unless handler
81:       
82:       handler.call(request, response)
83:     rescue StandardError, LoadError, SyntaxError => e
84:       handle_exception(e, request, response)
85:     ensure
86:       raise_event2(:request_complete, dispatch_request_event.complete!)
87:     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 123
123:     def handle_exception(exception, request, response)
124:       response.flush
125:       response.status = 500
126: 
127:       if environment == "development"
128:         response.content_type = "text/html"
129:         response.puts(Rack::ShowExceptions.new(nil).pretty(request.env, exception))
130:       else
131:         response.layout = "layouts/exception" if Harbor::View.exists?("layouts/exception")
132: 
133:         if Harbor::View.exists?("exceptions/500.html.erb")
134:           response.render "exceptions/500.html.erb", :exception => exception
135:         else
136:           response.puts "We're sorry, but something went wrong."
137:         end
138:       end
139: 
140:       raise_event2(:exception, ApplicationExceptionEvent.new(request, response, exception))
141: 
142:       nil
143:     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 98
 98:     def handle_not_found(request, response)      
 99:       response.flush
100:       response.status = 404
101: 
102:       response.layout = "layouts/exception" if Harbor::View.exists?("layouts/exception")
103: 
104:       if Harbor::View.exists?("exceptions/404.html.erb")
105:         response.render "exceptions/404.html.erb"
106:       else
107:         response.puts "The page you requested could not be found"
108:       end
109: 
110:       raise_event2(:not_found, Events::NotFoundEvent.new(request, response))
111:     end