summaryrefslogtreecommitdiff
path: root/webapp/sinatra.rb
blob: d40633f65fb71a2ac9958c2b343b2f2444b03f39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
=begin
* Name: sinatra.rb
* Description: Helper code for sinatra
* Author: Andreas Maunz <andreas@maunz.de>
* Date: 10/2012
=end

module OpenTox
  class Application < Service

    # Get url_for support
    helpers Sinatra::UrlForHelper

    # Put any code here that should be executed immediately before 
    # request is processed
    before {
      $logger.debug "Request: " + request.path
      # fix IE
      request.env['HTTP_ACCEPT'] += ";text/html" if request.env["HTTP_USER_AGENT"]=~/MSIE/
      request.env['HTTP_ACCEPT']=request.params["media"] if request.params["media"]
    }

    # Conveniently accessible from anywhere within the Application class,
    # it negotiates the appropriate output format based on object class
    # and requested MIME type.
    # @param [Object] an object
    # @return [String] object serialization
    def format_output (obj)

      if obj.class == String

        case @accept
          when /text\/html/
            content_type "text/html"
            OpenTox.text_to_html obj
          else
            content_type 'text/uri-list'
            obj
        end

      else
  
        case @accept
          when "application/rdf+xml"
            content_type "application/rdf+xml"
            obj.to_rdfxml
          when /text\/html/
            content_type "text/html"
            OpenTox.text_to_html obj.to_turtle
          else
            content_type "text/turtle"
            obj.to_turtle
        end
  
      end
    end

  end
end