Module: Otto::Static
Overview
Static response utilities for common HTTP responses
Instance Method Summary collapse
-
#indifferent_hash ⇒ Object
Creates a Hash with indifferent access.
-
#indifferent_params(params) ⇒ Object
Enable string or symbol key access to the nested params hash.
- #not_found ⇒ Object
- #security_headers ⇒ Object
- #server_error ⇒ Object
Instance Method Details
#indifferent_hash ⇒ Object
Creates a Hash with indifferent access.
48 49 50 |
# File 'lib/otto/static.rb', line 48 def indifferent_hash Hash.new { |hash, key| hash[key.to_s] if key.is_a?(Symbol) } end |
#indifferent_params(params) ⇒ Object
Enable string or symbol key access to the nested params hash.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/otto/static.rb', line 28 def indifferent_params(params) if params.is_a?(Hash) params = indifferent_hash.merge(params) params.each do |key, value| next unless value.is_a?(Hash) || value.is_a?(Array) params[key] = indifferent_params(value) end elsif params.is_a?(Array) params.collect! do |value| if value.is_a?(Hash) || value.is_a?(Array) indifferent_params(value) else value end end end end |
#not_found ⇒ Object
14 15 16 |
# File 'lib/otto/static.rb', line 14 def not_found [404, security_headers.merge({ 'content-type' => 'text/plain' }), ['Not Found']] end |
#security_headers ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/otto/static.rb', line 18 def security_headers { 'x-frame-options' => 'DENY', 'x-content-type-options' => 'nosniff', 'x-xss-protection' => '1; mode=block', 'referrer-policy' => 'strict-origin-when-cross-origin', } end |
#server_error ⇒ Object
10 11 12 |
# File 'lib/otto/static.rb', line 10 def server_error [500, security_headers.merge({ 'content-type' => 'text/plain' }), ['Server error']] end |