Module: Tripod::Streaming

Defined in:
lib/tripod/streaming.rb

Class Method Summary collapse

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
# 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 = ""

  http.request_get(uri.request_uri, 'Accept' => accept) do |res|

    Rails.logger.debug "TRIPOD: response code: #{res.code}" if defined?(Rails)

    raise Tripod::Errors::BadSparqlRequest.new(res.body) if res.code.to_s != "200"

    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
  end

  response_string

end