Module: Webservice::Helpers

Included in:
Base
Defined in:
lib/webservice/base.rb

Instance Method Summary collapse

Instance Method Details

#content_type(type = nil) ⇒ Object

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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/webservice/base.rb', line 58

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/base.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.



50
51
52
53
# File 'lib/webservice/base.rb', line 50

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/base.rb', line 38

def not_found( body=nil )
  error 404, body
end

#redirect_to(uri, status = 302) ⇒ Object Also known as: redirect

Note: 302 == Found, 301 == Moved Permanently



43
44
45
# File 'lib/webservice/base.rb', line 43

def redirect_to( uri, status=302 )    ## Note: 302 == Found, 301 == Moved Permanently
  halt status, { LOCATION => uri }
end

#send_file(path) ⇒ Object

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



78
79
80
81
82
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
# File 'lib/webservice/base.rb', line 78

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