Module: Tripod::Streaming
- Defined in:
- lib/tripod/streaming.rb
Class Method Summary collapse
-
.get_data(request_url, opts = {}) ⇒ Object
stream data from a url opts :accept => “/” :timeout_seconds = 10 :limit_bytes = nil.
Class Method Details
.get_data(request_url, opts = {}) ⇒ Object
stream data from a url opts :accept => “/”
:timeout_seconds = 10
:limit_bytes = nil
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/tripod/streaming.rb', line 11 def self.get_data(request_url, opts={}) accept = opts[:accept] || "*/*" timeout_in_seconds = opts[:timeout_seconds] || 10 limit_in_bytes = opts[:response_limit_bytes] uri = URI(request_url) http = Net::HTTP.new(uri.host, uri.port) http.read_timeout = timeout_in_seconds total_bytes = 0 response_string = "" request_start_time = Time.now if Tripod.logger.debug? begin http.request_get(uri.request_uri, 'Accept' => accept) do |res| response_duration = Time.now - request_start_time if Tripod.logger.debug? Tripod.logger.debug "TRIPOD: received response code: #{res.code} in: #{response_duration} secs" raise Tripod::Errors::BadSparqlRequest.new(res.body) if res.code.to_s != "200" stream_start_time = Time.now if Tripod.logger.debug? res.read_body do |seg| total_bytes += seg.size response_string += seg.to_s # if there's a limit, stop when we reach it raise Tripod::Errors::SparqlResponseTooLarge.new if limit_in_bytes && (total_bytes > limit_in_bytes) end if Tripod.logger.debug? stream_duration = Time.now - stream_start_time if total_request_time = Time.now - request_start_time end Tripod.logger.debug "TRIPOD: #{total_bytes} bytes streamed in: #{stream_duration} secs" Tripod.logger.debug "TRIPOD: total request time: #{total_request_time} secs" end rescue Timeout::Error => timeout raise Tripod::Errors::Timeout.new end response_string end |