diff options
author | mguetlein <martin.guetlein@gmail.com> | 2012-12-07 16:59:56 +0100 |
---|---|---|
committer | mguetlein <martin.guetlein@gmail.com> | 2012-12-07 16:59:56 +0100 |
commit | 7e27259d5db7349e0e29762244298c986146ca37 (patch) | |
tree | 9258561d0693f7b8586392bdfa4c444fe929e707 /lib | |
parent | 4af2fdebf685aa7ab7f5bc7c653fce49a4f1a389 (diff) |
extend html support
get request in service root now returns uri-list
accept header for html requests is fixed in before-method
fix link parsing in to_html method
add png image to to_html method
Diffstat (limited to 'lib')
-rw-r--r-- | lib/4store.rb | 9 | ||||
-rw-r--r-- | lib/utils/html.rb | 9 | ||||
-rw-r--r-- | lib/utils/shims/feature.rb | 2 |
3 files changed, 11 insertions, 9 deletions
diff --git a/lib/4store.rb b/lib/4store.rb index 3ed081d..5a2714b 100644 --- a/lib/4store.rb +++ b/lib/4store.rb @@ -6,9 +6,8 @@ module OpenTox @@content_type_formats = [ "application/rdf+xml", "text/turtle", "text/plain" ] def self.list mime_type - mime_type = "text/html" if mime_type.match(%r{\*/\*}) bad_request_error "'#{mime_type}' is not a supported mime type. Please specify one of #{@@accept_formats.join(", ")} in the Accept Header." unless @@accept_formats.include? mime_type - if mime_type =~ /uri-list/ + if mime_type =~ /(uri-list|html)/ sparql = "SELECT DISTINCT ?g WHERE {GRAPH ?g {?s <#{RDF.type}> <#{klass}>; ?p ?o. } }" else sparql = "CONSTRUCT {?s ?p ?o.} WHERE {?s <#{RDF.type}> <#{klass}>; ?p ?o. }" @@ -17,7 +16,6 @@ module OpenTox end def self.get uri, mime_type - mime_type = "text/html" if mime_type.match(%r{\*/\*}) bad_request_error "'#{mime_type}' is not a supported mime type. Please specify one of #{@@accept_formats.join(", ")} in the Accept Header." unless @@accept_formats.include? mime_type sparql = "CONSTRUCT {?s ?p ?o.} FROM <#{uri}> WHERE { ?s ?p ?o. }" rdf = query sparql, mime_type @@ -61,8 +59,9 @@ module OpenTox case mime_type when 'application/sparql-results+xml' RestClient.get(sparql_uri, :params => { :query => sparql }, :accept => mime_type).body - when "text/uri-list" - RestClient.get(sparql_uri, :params => { :query => sparql }, :accept => "text/plain").body.gsub(/"|<|>/,'').split("\n").drop(1).join("\n") + when /(uri-list|html)/ + uri_list = RestClient.get(sparql_uri, :params => { :query => sparql }, :accept => "text/plain").body.gsub(/"|<|>/,'').split("\n").drop(1).join("\n") + uri_list = OpenTox.text_to_html(uri_list) if mime_type=~/html/ else bad_request_error "#{mime_type} is not a supported mime type for SELECT statements." end diff --git a/lib/utils/html.rb b/lib/utils/html.rb index 91dfc64..d74f357 100644 --- a/lib/utils/html.rb +++ b/lib/utils/html.rb @@ -7,15 +7,17 @@ * Date: 10/2012 =end +require "base64" + # AM: needed since this gem has a nested directory structure class String # encloses URI in text with with link tag # @return [String] new text with marked links def link_urls - regex = Regexp.new '(https?:\/\/[\S]+)([>"])' - self.gsub( regex, '<a href="\1">\1</a>\2' ) + self.gsub(/(?i)http(s?):\/\/[^\r\n\s']*/, '<a href="\0">\0</a>') end + end module OpenTox @@ -29,7 +31,7 @@ module OpenTox # @param [optional,String] description general info # @param [optional,Array] post_command, infos for the post operation, object defined below # @return [String] html page - def self.text_to_html( text, subjectid=nil, related_links=nil, description=nil, post_command=nil ) + def self.text_to_html( text, subjectid=nil, related_links=nil, description=nil, post_command=nil, png_image=nil ) # TODO add title as parameter title = nil #$sinatra.url_for($sinatra.request.env['PATH_INFO'], :full) if $sinatra @@ -41,6 +43,7 @@ module OpenTox html += "<h3>Related links</h3><pre><p>"+related_links.link_urls+"</p></pre>" if related_links html += "<h3>Content</h3>" if description || related_links html += "<pre><p style=\"padding:15px; border:10px solid \#5D308A\">" + html += "<img src=\"data:image/png;base64,#{Base64.encode64(png_image)}\">\n" if png_image html += text.link_urls html += "</p></pre></body></html>" html diff --git a/lib/utils/shims/feature.rb b/lib/utils/shims/feature.rb index 297748b..f49bb39 100644 --- a/lib/utils/shims/feature.rb +++ b/lib/utils/shims/feature.rb @@ -30,7 +30,7 @@ module OpenTox metadata[RDF.type] = [] unless metadata[RDF.type] metadata[RDF.type] << RDF::OT.Feature unless metadata[RDF.type].include?(RDF::OT.Feature) metadata[RDF::DC.title] = title unless (metadata[RDF::DC.title]) - feature = feature_new = OpenTox::Feature.new File.join($feature[:uri], SecureRandom.uuid), @subjectid + feature = feature_new = OpenTox::Feature.new(File.join($feature[:uri], SecureRandom.uuid), @subjectid) feature_new.metadata = metadata sparql = "SELECT DISTINCT ?feature WHERE { ?feature <#{RDF.type}> <#{RDF::OT['feature'.capitalize]}>. ?feature <#{RDF::DC.title}> '#{title.to_s}' }" feature_uris = OpenTox::Backend::FourStore.query(sparql,"text/uri-list").split("\n") |