Skip to content

Commit

Permalink
Merge branch 'net-http-persistent' of https://github.com/b4d/sparql-c…
Browse files Browse the repository at this point in the history
  • Loading branch information
artob committed Feb 2, 2011
2 parents 22b7755 + 419f9c8 commit d828251
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Gem::Specification.new do |gem|
gem.requirements = []
gem.add_runtime_dependency 'json_pure', '>= 1.4.2' # included in Ruby 1.9.2
gem.add_runtime_dependency 'rdf', '~> 0.3.0'
gem.add_runtime_dependency 'net-http-persistent', '~> 1.4.1'
gem.add_development_dependency 'yard' , '>= 0.6.0'
gem.add_development_dependency 'rspec', '>= 2.1.0'
gem.add_development_dependency 'rdf-spec', '~> 0.3.0'
Expand Down
24 changes: 11 additions & 13 deletions lib/sparql/client.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'net/http'
require 'net/http/persistent'
require 'rdf' # @see http://rubygems.org/gems/rdf
require 'rdf/ntriples'

Expand Down Expand Up @@ -33,6 +33,7 @@ def initialize(url, options = {}, &block)
@url, @options = RDF::URI.new(url.to_s), options
#@headers = {'Accept' => "#{RESULT_JSON}, #{RESULT_XML}, text/plain"}
@headers = {'Accept' => [RESULT_JSON, RESULT_XML, RDF::Format.content_types.collect { |k,v| k.to_s }].join(', ')}
@http = http_klass(@url.scheme)

if block_given?
case block.arity
Expand Down Expand Up @@ -270,11 +271,9 @@ def http_klass(scheme)
when "https"
proxy_uri = URI.parse(ENV['https_proxy']) unless ENV['https_proxy'].nil?
end
return Net::HTTP if proxy_uri.nil?

proxy_host, proxy_port = proxy_uri.host, proxy_uri.port
proxy_user, proxy_pass = proxy_uri.userinfo.split(/:/) if proxy_uri.userinfo
Net::HTTP::Proxy(proxy_host, proxy_port, proxy_user, proxy_pass)
klass = Net::HTTP::Persistent.new(self.class.to_s, proxy_uri)
klass.keep_alive = 120 # increase to 2 minutes
klass
end

##
Expand All @@ -289,13 +288,12 @@ def get(query, headers = {}, &block)
url = self.url.dup
url.query_values = {:query => query.to_s}

http_klass(url.scheme).start(url.host, url.port) do |http|
response = http.get(url.path + "?#{url.query}", @headers.merge(headers))
if block_given?
block.call(response)
else
response
end
request = Net::HTTP::Get.new(url.request_uri, @headers.merge(headers))
response = @http.request url, request
if block_given?
block.call(response)
else
response
end
end
end # Client
Expand Down

0 comments on commit d828251

Please sign in to comment.