summaryrefslogtreecommitdiff
path: root/lib/overwrite.rb
diff options
context:
space:
mode:
authorChristoph Helma <helma@in-silico.ch>2013-03-26 10:56:04 +0100
committerChristoph Helma <helma@in-silico.ch>2013-03-26 10:56:04 +0100
commita54db46684680d98311631804eca367cc949a715 (patch)
tree283b8c5f256e8605131cbfeae2217a77d0288ca7 /lib/overwrite.rb
parent4ba2cc9849473f97baf75195bb36c5057f1c58d4 (diff)
code cleanup and refactoring.
Diffstat (limited to 'lib/overwrite.rb')
-rw-r--r--lib/overwrite.rb68
1 files changed, 64 insertions, 4 deletions
diff --git a/lib/overwrite.rb b/lib/overwrite.rb
index c7bb312..d27434b 100644
--- a/lib/overwrite.rb
+++ b/lib/overwrite.rb
@@ -1,3 +1,4 @@
+require "base64"
class Object
# An object is blank if it's false, empty, or a whitespace string.
# For example, "", " ", +nil+, [], and {} are all blank.
@@ -17,6 +18,7 @@ module Enumerable
end
class String
+
def underscore
self.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
@@ -24,6 +26,44 @@ class String
tr("-", "_").
downcase
end
+
+ # encloses URI in text with with link tag
+ # @return [String] new text with marked links
+ def link_urls
+ self.gsub(/(?i)http(s?):\/\/[^\r\n\s']*/, '<a href="\0">\0</a>')
+ end
+
+ # produces a html page for making web services browser friendly
+ # format of text (=string params) is preserved (e.g. line breaks)
+ # urls are marked as links
+ #
+ # @param [String] text this is the actual content,
+ # @param [optional,String] related_links info on related resources
+ # @param [optional,String] description general info
+ # @param [optional,Array] post_command, infos for the post operation, object defined below
+ # @return [String] html page
+ def to_html(related_links=nil, description=nil, png_image=nil )
+
+ # TODO add title as parameter
+ title = nil #$sinatra.to($sinatra.request.env['PATH_INFO'], :full) if $sinatra
+ html = "<html>"
+ html << "<title>"+title+"</title>" if title
+ #html += "<img src=\""+OT_LOGO+"\"><\/img><body>"
+
+ html << "<h3>Description</h3><pre><p>"+description.link_urls+"</p></pre>" if description
+ 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 \#B9DCFF\">"
+ html << "<img src=\"data:image/png;base64,#{Base64.encode64(png_image)}\">\n" if png_image
+ html << self.link_urls
+ html << "</p></pre></body></html>"
+ html
+ end
+
+ def uri?
+ URI.valid?(self)
+ end
+
end
module URI
@@ -84,11 +124,11 @@ class File
end
end
-# overwrite backtick operator to catch system errors
module Kernel
- # Override raises an error if _cmd_ returns a non-zero exit status.
- # Returns stdout if _cmd_ succeeds. Note that these are simply concatenated; STDERR is not inline.
+ # overwrite backtick operator to catch system errors
+ # Override raises an error if _cmd_ returns a non-zero exit status. CH: I do not understand this comment
+ # Returns stdout if _cmd_ succeeds. Note that these are simply concatenated; STDERR is not inline. CH: I do not understand this comment
def ` cmd
stdout, stderr = ''
status = Open4::popen4(cmd) do |pid, stdin_stream, stdout_stream, stderr_stream|
@@ -101,6 +141,27 @@ module Kernel
internal_server_error $!.message
end
+ def wait_for_task uri
+ if URI.task?(uri)
+ t = OpenTox::Task.new uri
+ t.wait
+ unless t.completed?
+ begin # handle known (i.e. OpenTox) errors
+ error = OpenTox::RestClientWrapper.known_errors.select{|error| error[:code] == t.code}.first
+ error ? error_method = error[:method] : error_method = :internal_server_error
+ report = t.error_report
+ report ? error_message = report[RDF::OT.message] : error_message = $!.message
+ Object.send(error_method,error_message,t.uri)
+ rescue
+ internal_server_error "#{$!.message}\n#{$!.backtrace}", t.uri
+ end
+ end
+ uri = t.resultURI
+ end
+ uri
+ end
+
+
end
@@ -126,6 +187,5 @@ class Array
return self.uniq.size == 1
end
-
end