Class: CommonwealthVlrEngine::Streaming::FileBody

Inherits:
Object
  • Object
show all
Defined in:
lib/commonwealth-vlr-engine/streaming.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, headers) ⇒ FileBody

Returns a new instance of FileBody.



22
23
24
25
# File 'lib/commonwealth-vlr-engine/streaming.rb', line 22

def initialize(uri, headers)
  @uri = uri
  @headers = headers
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



21
22
23
# File 'lib/commonwealth-vlr-engine/streaming.rb', line 21

def headers
  @headers
end

#uriObject (readonly)

Returns the value of attribute uri.



21
22
23
# File 'lib/commonwealth-vlr-engine/streaming.rb', line 21

def uri
  @uri
end

Instance Method Details

#each(no_of_requests_limit = 3, &block) ⇒ Object

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/commonwealth-vlr-engine/streaming.rb', line 27

def each(no_of_requests_limit = 3, &block)
  raise ArgumentError, 'HTTP redirect too deep' if no_of_requests_limit.zero?
  Net::HTTP.start(uri.host, uri.port, use_ssl: (uri.scheme == 'https')) do |http|
    request = Net::HTTP::Get.new uri, headers
    http.request request do |response|
      case response
        when Net::HTTPSuccess
          response.read_body do |chunk|
            yield chunk
          end
        when Net::HTTPRedirection
          no_of_requests_limit -= 1
          @uri = URI(response["location"])
          each(no_of_requests_limit, &block)
        else
          raise "Couldn't get data from Fedora (#{uri}). Response: #{response.code}"
      end
    end
  end
end