46: def execute_sparql_query(qs, header=nil, &block)
47: header = header(nil) if header.nil?
48:
49:
50: url = "#@url?query=#{CGI.escape(qs)}"
51: $activerdflog.debug "executing: requesting #{url}"
52:
53:
54: response = ''
55: begin
56: open(url, header) do |f|
57: response = f.read
58: end
59: rescue OpenURI::HTTPError => e
60: raise ActiveRdfError, "could not query SPARQL endpoint, server said: #{e}"
61: return []
62: rescue Errno::ECONNREFUSED
63: raise ActiveRdfError, "connection refused on SPARQL endpoint #@url"
64: return []
65: end
66:
67:
68: results = case @result_format
69: when :json
70: parse_json(response)
71: when :xml, :sparql_xml
72: parse_xml(response)
73: end
74:
75: if block_given?
76: results.each do |*clauses|
77: yield(*clauses)
78: end
79: else
80: results
81: end
82: end