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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/webservice/base.rb', line 38

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.



16
17
18
19
20
21
22
# File 'lib/webservice/base.rb', line 16

def error(code, body=nil)
  code = 500
  body = code.to_str if code.respond_to? :to_str

  response.body = body unless body.nil?
  halt code
end

#headers(hash = nil) ⇒ Object

Set multiple response headers with Hash.



30
31
32
33
# File 'lib/webservice/base.rb', line 30

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.



25
26
27
# File 'lib/webservice/base.rb', line 25

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