lib/activerdf_sparql-1.3.6/lib/activerdf_sparql/sparql.rb
def execute_sparql_query(qs, header=nil, select_clauses=nil, &block)
    header = header(nil) if header.nil?
    
    #    puts select_clauses
    # querying sparql endpoint
    require 'timeout'
    response = ''
    url =''
    begin 
      case @request_method
        when :get
        # encoding query string in URL
        if @url.index('?') == nil
          url = "#@url?query=#{CGI.escape(qs)}"        
        else
          url = "#@url&query=#{CGI.escape(qs)}"          
        end
        #puts url
        $activerdflog.debug "GET #{url}"        
        timeout(@timeout) do          
          #             puts url
          open(url, header) do |f|            
            response = f.read   
            #                          puts response
          end
        end
        when :post
        puts 'Via POST'
        $activerdflog.debug "POST #@url with #{qs}"
        t = URI.parse(@url)
       # puts t
        t =  t.query
       # puts t
        if t != nil
         
          param = CGI::parse(t)
          param["query"]=qs
          
          response = Net::HTTP.post_form(URI.parse(@url),param).body
         
        else
          response = Net::HTTP.post_form(URI.parse(@url),{'query'=>qs}).body
        end
        
        
        
      end
      #      puts response
    rescue Timeout::Error
      raise ActiveRdfError, "timeout on SPARQL endpoint. <br><br><b>URI accessed:</b> " + url
      return "timeout"
    rescue OpenURI::HTTPError => e
      raise ActiveRdfError, "could not query SPARQL endpoint, server said: #{e} . <br><br><b>URI accessed:</b> " + url
      return []
      #    rescue Errno::ECONNREFUSED
    rescue Exception => e
      puts e.backtrace
      raise ActiveRdfError, "connection refused on SPARQL endpoint #@url"
      return []
    end
    # puts response
    #    results =  preprocess(results) if @preprocess
    #    return results if @raw  
    # we parse content depending on the result format    
    results = case @result_format
      when :json 
      parse_json(response)
      when :xml, :sparql_xml
      parse_xml(response,select_clauses)
    end
    
    if block_given?
      results.each do |*clauses|
        yield(*clauses)
      end
    else      
      results
    end
  end
# do the real work of executing the sparql query
do the real work of executing the sparql query
do the real work of executing the sparql query
def execute_sparql_query(qs, header=nil, select_clauses=nil, &block)
    header = header(nil) if header.nil?
    require 'timeout'
    response = ''
    url =''
    begin 
      case @request_method
        when :get
        if @url.index('?') == nil
          url = "#@url?query=#{CGI.escape(qs)}"        
        else
          url = "#@url&query=#{CGI.escape(qs)}"          
        end
        $activerdflog.debug "GET #{url}"        
        timeout(@timeout) do          
          open(url, header) do |f|            
            response = f.read   
          end
        end
        when :post
        puts 'Via POST'
        $activerdflog.debug "POST #@url with #{qs}"
        t = URI.parse(@url)
        t =  t.query
        if t != nil
          param = CGI::parse(t)
          param["query"]=qs
          response = Net::HTTP.post_form(URI.parse(@url),param).body
        else
          response = Net::HTTP.post_form(URI.parse(@url),{'query'=>qs}).body
        end
      end
    rescue Timeout::Error
      raise ActiveRdfError, "timeout on SPARQL endpoint. <br><br><b>URI accessed:</b> " + url
      return "timeout"
    rescue OpenURI::HTTPError => e
      raise ActiveRdfError, "could not query SPARQL endpoint, server said: #{e} . <br><br><b>URI accessed:</b> " + url
      return []
    rescue Exception => e
      puts e.backtrace
      raise ActiveRdfError, "connection refused on SPARQL endpoint #@url"
      return []
    end
    results = case @result_format
      when :json 
      parse_json(response)
      when :xml, :sparql_xml
      parse_xml(response,select_clauses)
    end
    if block_given?
      results.each do |*clauses|
        yield(*clauses)
      end
    else      
      results
    end
  end
def execute_sparql_query ( qs , header = nil , select_clauses = nil , & block ) header = header ( nil ) if header . nil? require 'timeout' response = '' url = '' begin case @request_method when :get if @url . index ( '?' ) == nil url = "#@url?query=#{CGI.escape(qs)}" else url = "#@url&query=#{CGI.escape(qs)}" end $activerdflog . debug "GET #{url}" timeout ( @timeout ) do open ( url , header ) do | f | response = f . read end end when :post puts 'Via POST' $activerdflog . debug "POST #@url with #{qs}" t = URI . parse ( @url ) t = t . query if t != nil param = CGI :: parse ( t ) param [ "query" ] = qs response = Net :: HTTP . post_form ( URI . parse ( @url ) , param ) . body else response = Net :: HTTP . post_form ( URI . parse ( @url ) , { 'query' => qs } ) . body end end rescue Timeout :: Error raise ActiveRdfError , "timeout on SPARQL endpoint. <br><br><b>URI accessed:</b> " + url return "timeout" rescue OpenURI :: HTTPError => e raise ActiveRdfError , "could not query SPARQL endpoint, server said: #{e} . <br><br><b>URI accessed:</b> " + url return [ ] rescue Exception => e puts e . backtrace raise ActiveRdfError , "connection refused on SPARQL endpoint #@url" return [ ] end results = case @result_format when :json parse_json ( response ) when :xml , :sparql_xml parse_xml ( response , select_clauses ) end if block_given? results . each do | * clauses | yield ( * clauses ) end else results end end
do the real work of executing the sparql query
do the real work of executing the sparql query
#    puts select_clauses # querying sparql endpoint # encoding query string in URL #puts url #             puts url #                          puts response # puts t # puts t #      puts response #    rescue Errno::ECONNREFUSED # puts response #    results =  preprocess(results) if @preprocess #    return results if @raw   # we parse content depending on the result format    
qs
header
select_clauses
block