Module: Webservice::Helpers
- Included in:
- Metal
- Defined in:
- lib/webservice/metal.rb
Instance Method Summary collapse
-
#content_type(type = nil) ⇒ Object
(simple) content_type helper - all “hard-coded” for now; always uses utf-8 too.
-
#error(code, body = nil) ⇒ Object
Halt processing and return the error status provided.
-
#headers(hash = nil) ⇒ Object
Set multiple response headers with Hash.
-
#not_found(body = nil) ⇒ Object
Halt processing and return a 404 Not Found.
-
#redirect(uri, status = 302) ⇒ Object
(also: #redirect_to)
Note: 302 == Found, 301 == Moved Permanently.
-
#send_file(path) ⇒ Object
simple send file (e.g. for images/binary blobs, etc.) helper.
Instance Method Details
#content_type(type = nil) ⇒ Object
(simple) content_type helper - all “hard-coded” for now; always uses utf-8 too
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/webservice/metal.rb', line 63 def content_type( type=nil ) return response[ CONTENT_TYPE ] unless type if type.to_sym == :json response[ CONTENT_TYPE ] = 'application/json; charset=utf-8' elsif type.to_sym == :js || type.to_sym == :javascript response[ CONTENT_TYPE ] = 'application/javascript; charset=utf-8' ## use 'text/javascript; charset=utf-8' -- why? why not?? ## note: ietf recommends application/javascript elsif type.to_sym == :csv || type.to_sym == :text || type.to_sym == :txt response[ CONTENT_TYPE ] = 'text/plain; charset=utf-8' elsif type.to_sym == :html || type.to_sym == :htm response[ CONTENT_TYPE ] = 'text/html; charset=utf-8' else ### unknown type; do nothing - sorry; issue warning - why? why not?? end end |
#error(code, body = nil) ⇒ Object
Halt processing and return the error status provided.
32 33 34 35 |
# File 'lib/webservice/metal.rb', line 32 def error( code, body=nil ) response.body = body unless body.nil? halt code end |
#headers(hash = nil) ⇒ Object
Set multiple response headers with Hash.
55 56 57 58 |
# File 'lib/webservice/metal.rb', line 55 def headers( hash=nil ) response.headers.merge! hash if hash response.headers end |
#not_found(body = nil) ⇒ Object
Halt processing and return a 404 Not Found.
38 39 40 |
# File 'lib/webservice/metal.rb', line 38 def not_found( body=nil ) error 404, body end |
#redirect(uri, status = 302) ⇒ Object Also known as: redirect_to
Note: 302 == Found, 301 == Moved Permanently
43 44 45 46 47 48 49 50 |
# File 'lib/webservice/metal.rb', line 43 def redirect( uri, status=302 ) ## Note: 302 == Found, 301 == Moved Permanently ## ## todo/fix: add/prepepand SCRIPT_NAME if NOT empty - why? why not?? ## without SCRIPT_NAME redirect will not work with (non-root) mounted apps halt status, { LOCATION => uri } end |
#send_file(path) ⇒ Object
simple send file (e.g. for images/binary blobs, etc.) helper
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/webservice/metal.rb', line 83 def send_file( path ) ## puts "send_file path=>#{path}<" ## puts "HTTP_IF_MODIFIED_SINCE:" ## puts request.get_header('HTTP_IF_MODIFIED_SINCE') last_modified = File.mtime(path).httpdate ## puts "last_modified:" ## puts last_modified ## HTTP 304 => Not Modified halt 304 if request.get_header('HTTP_IF_MODIFIED_SINCE') == last_modified headers[ LAST_MODIFIED ] = last_modified bytes = File.open( path, 'rb' ) { |f| f.read } ## puts "encoding:" ## puts bytes.encoding ## puts "size:" ## puts bytes.size extname = File.extname( path ) ## puts "extname:" ## puts extname ## puts "headers (before):" ## pp headers if extname == '.png' headers[ CONTENT_TYPE ] = 'image/png' else ## fallback to application/octet-stream headers[ CONTENT_TYPE ] = 'application/octet-stream' end headers[ CONTENT_LENGTH ] = bytes.size.to_s ## note: do NOT forget to use to_s (requires string!) ## puts "headers (after):" ## pp headers halt 200, bytes end |