Class: Rhail::Helper
- Inherits:
-
Object
- Object
- Rhail::Helper
- Defined in:
- lib/rhail/helper.rb
Overview
Helper for all http response related needs.
Defined Under Namespace
Classes: FailedTokenAuth
Class Method Summary collapse
-
.params_from_json_request(req) ⇒ Object
When JSON request is expected converts it to Hash.
-
.render_html(files: ['index'], local_vars: nil, headers: {}) ⇒ Object
Renders html.
-
.render_json(response_structure: {}, status: 200, headers: {}) ⇒ Object
Renders json form hash in
response_structure
. -
.token_auth(http_auth_header, valid_token) ⇒ Object
Authenticates request based on token.
Class Method Details
.params_from_json_request(req) ⇒ Object
82 83 84 |
# File 'lib/rhail/helper.rb', line 82 def self.params_from_json_request(req) JSON.parse(req.body.read) end |
.render_html(files: ['index'], local_vars: nil, headers: {}) ⇒ Object
Renders html. It concatenates files specified in files
parameter. If local_vars
specified it also make Ruby string evaluation so var value will appera in html. If headers
provided also adds them, good for caching headers.
Example:
Rhail::Helper.render_html files: %w(head home foot), local_vars: {page: 'home'}
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/rhail/helper.rb', line 27 def self.render_html(files: ['index'], local_vars: nil, headers: {}) res = Rack::Response.new html = '' if local_vars files.each {|fn| html += eval('%Q(' + File.read("public/html/#{fn}.html") + ')') } else files.each {|fn| html += File.read("public/html/#{fn}.html") } end res['Content-Type'] = 'text/html' headers.each do |k, v| res[k] = v end if headers res.write html res.finish end |
.render_json(response_structure: {}, status: 200, headers: {}) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/rhail/helper.rb', line 58 def self.render_json(response_structure: {}, status: 200, headers: {}) res = Rack::Response.new([], status, {}) res['Content-Type'] = 'application/json' headers.each do |k, v| res[k] = v end if headers res.write response_structure.to_json res.finish end |
.token_auth(http_auth_header, valid_token) ⇒ Object
Authenticates request based on token. valid_token
source is up to developer, in generators it’s just in a config file. In real life you can use database or key-value storage and get it from there. If token is not valid renders 401 with error in json.
Example:
Rhail::Helper.token_auth(env['HTTP_AUTHORIZATION'], valid_token) do |t|
Rhail::Helper.render_json(response_structure: some_hash)
end
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/rhail/helper.rb', line 98 def self.token_auth(http_auth_header, valid_token) raise FailedTokenAuth unless http_auth_header type, token = http_auth_header.split(' ') raise FailedTokenAuth if type != 'Token' token_value = token.sub('token="', '').chop rescue raise(FailedTokenAuth) raise FailedTokenAuth if token_value != valid_token puts "Access granted" yield token_value rescue FailedTokenAuth, ::StandardError => e puts e. render_json(response_structure: {status: '401', code: 'Unauthorized', detail: 'Access denied'}, status: 401) end |