module Webservice::Helpers

Public Instance Methods

content_type( type=nil ) click to toggle source

(simple) content_type helper - all “hard-coded” for now; always uses utf-8 too

# 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 ) click to toggle source

Halt processing and return the error status provided.

# File lib/webservice/metal.rb, line 32
def error( code, body=nil )
  response.body = body unless body.nil?
  halt code
end
headers( hash=nil ) click to toggle source

Set multiple response headers with Hash.

# File lib/webservice/metal.rb, line 55
def headers( hash=nil )
  response.headers.merge! hash if hash
  response.headers
end
not_found( body=nil ) click to toggle source

Halt processing and return a 404 Not Found.

# File lib/webservice/metal.rb, line 38
def not_found( body=nil )
  error 404, body
end
redirect( uri, status=302 ) click to toggle source
# 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
Also aliased as: redirect_to
redirect_to( uri, status=302 )
Alias for: redirect
send_file( path ) click to toggle source

simple send file (e.g. for images/binary blobs, etc.) helper

# 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