Class: Etna::Controller
- Inherits:
-
Object
- Object
- Etna::Controller
- Defined in:
- lib/etna/controller.rb
Constant Summary collapse
- VIEW_PATH =
methods for returning a view
:VIEW_PATH
Instance Method Summary collapse
- #config_hosts ⇒ Object
- #erb_partial(name) ⇒ Object
- #erb_view(name) ⇒ Object
- #handle_error(e) ⇒ Object
-
#initialize(request, action = nil) ⇒ Controller
constructor
A new instance of Controller.
- #log(line) ⇒ Object
- #require_params(*params) ⇒ Object (also: #require_param)
- #response(&block) ⇒ Object
- #route_path(name, params = {}) ⇒ Object
- #route_url(name, params = {}) ⇒ Object
- #send_email(to_name, to_email, subject, content) ⇒ Object
- #try_stream(content_type, &block) ⇒ Object
- #view(name) ⇒ Object
Constructor Details
#initialize(request, action = nil) ⇒ Controller
Returns a new instance of Controller.
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/etna/controller.rb', line 6 def initialize(request, action = nil) @request = request @action = action @response = Rack::Response.new @params = @request.env['rack.request.params'] @errors = [] @server = @request.env['etna.server'] @logger = @request.env['etna.logger'] @user = @request.env['etna.user'] @request_id = @request.env['etna.request_id'] @hmac = @request.env['etna.hmac'] end |
Instance Method Details
#config_hosts ⇒ Object
135 136 137 138 139 |
# File 'lib/etna/controller.rb', line 135 def config_hosts [:janus, :magma, :timur, :metis, :vulcan, :polyphemus, :gnomon].map do |host| [ :"#{host}_host", @server.send(:application).config(host)&.dig(:host) ] end.to_h.compact end |
#erb_partial(name) ⇒ Object
124 125 126 127 |
# File 'lib/etna/controller.rb', line 124 def erb_partial(name) txt = File.read("#{self.class::VIEW_PATH}/#{name}.html.erb") ERB.new(txt).result(binding) end |
#erb_view(name) ⇒ Object
129 130 131 132 133 |
# File 'lib/etna/controller.rb', line 129 def erb_view(name) @response['Content-Type'] = 'text/html' @response.write(erb_partial(name)) @response.finish end |
#handle_error(e) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/etna/controller.rb', line 23 def handle_error(e) case e when Etna::Error Rollbar.error(e) @logger.error(request_msg("Exiting with #{e.status}, #{e.}")) return failure(e.status, error: e.) else Rollbar.error(e) @logger.error(request_msg('Caught unspecified error')) @logger.error(request_msg(e.)) e.backtrace.each do |trace| @logger.error(request_msg(trace)) end return failure(500, error: 'Server error.') end end |
#log(line) ⇒ Object
19 20 21 |
# File 'lib/etna/controller.rb', line 19 def log(line) @logger.warn(request_msg(line)) end |
#require_params(*params) ⇒ Object Also known as: require_param
98 99 100 101 |
# File 'lib/etna/controller.rb', line 98 def require_params(*params) missing_params = params.reject{|p| @params.key?(p) } raise Etna::BadRequest, "Missing param #{missing_params.join(', ')}" unless missing_params.empty? end |
#response(&block) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/etna/controller.rb', line 40 def response(&block) return instance_eval(&block) if block_given? return send(@action) if @action [501, {}, ['This controller is not implemented.']] rescue Exception => e error = e ensure log_request if !@request.env['etna.dont_log'] || error return handle_error(error) if error end |
#route_path(name, params = {}) ⇒ Object
104 105 106 |
# File 'lib/etna/controller.rb', line 104 def route_path(name, params={}) @server.class.route_path(@request, name, params) end |
#route_url(name, params = {}) ⇒ Object
108 109 110 111 112 |
# File 'lib/etna/controller.rb', line 108 def route_url(name, params={}) path = route_path(name,params) return nil unless path @request.scheme + '://' + @request.host + path end |
#send_email(to_name, to_email, subject, content) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/etna/controller.rb', line 80 def send_email(to_name, to_email, subject, content) = <<MESSAGE_END From: Data Library <noreply@janus> To: #{to_name} <#{to_email}> Subject: #{subject} #{content} MESSAGE_END unless @server.send(:application).test? Net::SMTP.start('smtp.ucsf.edu') do |smtp| smtp. , 'noreply@janus', to_email end end rescue => e @logger.log_error(e) end |
#try_stream(content_type, &block) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/etna/controller.rb', line 52 def try_stream(content_type, &block) if @request.env['rack.hijack?'] @request.env['rack.hijack'].call stream = @request.env['rack.hijack_io'] headers = [ "HTTP/1.1 200 OK", "Content-Type: #{content_type}" ] stream.write(headers.map { |header| header + "\r\n" }.join) stream.write("\r\n") stream.flush Thread.new do block.call(stream) ensure stream.close end # IO is now streaming and will be processed by above thread. @response.close else @response['Content-Type'] = content_type block.call(@response) @response.finish end end |
#view(name) ⇒ Object
117 118 119 120 121 122 |
# File 'lib/etna/controller.rb', line 117 def view(name) txt = File.read("#{self.class::VIEW_PATH}/#{name}.html") @response['Content-Type'] = 'text/html' @response.write(txt) @response.finish end |