OT_LOGO = File.join(CONFIG[:services]["opentox-validation"],"resources/ot-logo.png") class String # 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']*/, '\0') end end module OpenTox # 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 self.text_to_html( text, subjectid=nil, related_links=nil, description=nil, post_command=nil ) # TODO add title as parameter title = nil #$sinatra.url_for($sinatra.request.env['PATH_INFO'], :full) if $sinatra html = "" html += ""+title+"" if title html += "<\/img>" if AA_SERVER user = OpenTox::Authorization.get_user(subjectid) if subjectid html += "

" unless user html += "You are currently not signed in to "+$url_provider.url_for("",:full)+ ", sign in" else html += "You are signed in as '#{user}' to "+$url_provider.url_for("",:full)+ ", sign out" end html += "

" end html += "

Description

"+description.link_urls+"

" if description html += "

Related links

"+related_links.link_urls+"

" if related_links if post_command raise "not a post command" unless post_command.is_a?(OpenTox::PostCommand) html += "

POST command

" html += post_command.to_html end html += "

Content

" if description || related_links || post_command html += "

" html += text.link_urls html += "

" html end def self.sign_in( msg=nil ) html = "Login" html += "
" html += "

" html += msg+"\n\n" if msg html += "Please sign in to "+$url_provider.url_for("",:full)+"\n\n" html += "" html += ""+ ""+ #""+ "" html += "
user:
password:

" html end class PostAttribute attr_accessor :name, :is_mandatory, :default, :description def initialize(name, is_mandatory=true, default=nil, description=nil) @name = name @is_mandatory = is_mandatory @default = default @description = description end end class PostCommand attr_accessor :attributes, :uri, :name def initialize( uri, name="Send" ) @uri = uri @name = name @attributes = [] end def to_html html = "
" html << "

" html << "" #html << "" attributes.each do |a| mandatory_string = a.is_mandatory ? "*" : "" html << "" html << "" html << "" end html << "" html << "
Mandatory params are marked with *.
"+a.name.to_s+":"+mandatory_string+""+a.description.to_s+"

" html end end end get '/sign_out/?' do response.set_cookie("subjectid",{:value=>nil}) content_type "text/html" content = "Sucessfully signed out from "+$url_provider.url_for("",:full) OpenTox.text_to_html(content) end get '/sign_in/?' do content_type "text/html" OpenTox.sign_in end post '/sign_in/?' do subjectid = OpenTox::Authorization.authenticate(params[:user], params[:password]) if (subjectid) response.set_cookie("subjectid",{:value=>subjectid}) content_type "text/html" content = "Sucessfully signed in as '"+params[:user]+"' to "+$url_provider.url_for("",:full) OpenTox.text_to_html(content,subjectid) else content_type "text/html" OpenTox.sign_in("Login failed, please try again") end end