From c34e458c2d5afdea24a65f1663af91ba4a14175c Mon Sep 17 00:00:00 2001 From: Justin Coyne Date: Tue, 3 Mar 2015 21:19:32 -0600 Subject: [PATCH] Add a mechanism to override the HTTP verb Marmotta 3.3.0 requires GET for DELETE requests, but can accept POST for INSERT This enables rdf-marmotta to override less of #request. See https://github.com/jcoyne/rdf-marmotta/commit/9b1cf6289644578e8fe9bf098a86b56e9dc4a055 --- lib/sparql/client.rb | 13 ++++++++++--- spec/client_spec.rb | 8 ++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/sparql/client.rb b/lib/sparql/client.rb index 0f05212a..8be4a19c 100644 --- a/lib/sparql/client.rb +++ b/lib/sparql/client.rb @@ -659,8 +659,6 @@ def http_klass(scheme) # @return [Net::HTTPResponse] # @see http://www.w3.org/TR/sparql11-protocol/#query-operation def request(query, headers = {}, &block) - method = (self.options[:method] || DEFAULT_METHOD).to_sym - # Make sure an appropriate Accept header is present headers['Accept'] ||= if (query.respond_to?(:expects_statements?) ? query.expects_statements? : @@ -670,7 +668,7 @@ def request(query, headers = {}, &block) RESULT_ALL end - request = send("make_#{method}_request", query, headers) + request = send("make_#{method(query)}_request", query, headers) request.basic_auth(url.user, url.password) if url.user && !url.user.empty? @@ -686,6 +684,15 @@ def request(query, headers = {}, &block) raise ServerError, "Infinite redirect at #{url}. Redirected more than 10 times." end + ## + # Return the HTTP verb for posting this request. + # this is useful if you need to override the HTTP verb based on the request being made. + # (e.g. Marmotta 3.3.0 requires GET for DELETE requests, but can accept POST for INSERT) + def method(query) + (options[:method] || DEFAULT_METHOD).to_sym + end + + ## # Constructs an HTTP GET request according to the SPARQL Protocol. # diff --git a/spec/client_spec.rb b/spec/client_spec.rb index 96924356..fe71cf9d 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -85,6 +85,14 @@ def response(header) client.query(query) end + it "should enable overriding the http method" do + stub_request(:get, "http://data.linkedmdb.org/sparql?query=DESCRIBE%20?kb%20WHERE%20%7B%20?kb%20%3Chttp://data.linkedmdb.org/resource/movie/actor_name%3E%20%22Kevin%20Bacon%22%20.%20%7D"). + to_return(:status => 200, :body => "", :headers => {}) + allow(subject).to receive(:method).with(query).and_return(:get) + expect(subject).to receive(:make_get_request).and_call_original + subject.query(query) + end + it "should support international characters in response body" do client = SPARQL::Client.new('http://dbpedia.org/sparql') json = {