Class: FormatParser::RemoteIO

Inherits:
Object
  • Object
show all
Defined in:
lib/remote_io.rb

Overview

Acts as a wrapper for turning a given URL into an IO object you can read from and seek in.

Defined Under Namespace

Classes: IntermittentFailure, InvalidRequest, RedirectLimitReached, UpstreamError

Instance Method Summary collapse

Constructor Details

#initialize(uri, headers: {}) ⇒ RemoteIO

Returns a new instance of RemoteIO.

Parameters:

  • uri (String, URI::Generic)

    the remote URL to obtain

  • headers (Hash) (defaults to: {})

    (optional) the HTTP headers to be used in the HTTP request



36
37
38
39
40
41
# File 'lib/remote_io.rb', line 36

def initialize(uri, headers: {})
  @headers = headers
  @uri = URI(uri)
  @pos = 0
  @remote_size = false
end

Instance Method Details

#posObject

Emulates IO#pos



50
51
52
# File 'lib/remote_io.rb', line 50

def pos
  @pos
end

#read(n_bytes) ⇒ String

Emulates IO#read, but requires the number of bytes to read The read will be limited to the size of the remote resource relative to the current offset in the IO, so if you are at offset 0 in the IO of size 10, doing a ‘read(20)` will only return you 10 bytes of result, and not raise any exceptions.

Parameters:

  • n_bytes (Fixnum, nil)

    how many bytes to read, or ‘nil` to read all the way to the end

Returns:



70
71
72
73
74
75
76
77
78
# File 'lib/remote_io.rb', line 70

def read(n_bytes)
  http_range = (@pos..(@pos + n_bytes - 1))
  maybe_size, maybe_body = Measurometer.instrument('format_parser.remote_io.read') { request_range(http_range) }
  if maybe_size && maybe_body
    @remote_size = maybe_size
    @pos += maybe_body.bytesize
    maybe_body.force_encoding(Encoding::ASCII_8BIT)
  end
end

#seek(offset) ⇒ Object

Emulates IO#seek



44
45
46
47
# File 'lib/remote_io.rb', line 44

def seek(offset)
  @pos = offset
  0 # always return 0
end

#sizeInteger

Emulates IO#size.

Returns:

  • (Integer)

    the size of the remote resource



57
58
59
60
# File 'lib/remote_io.rb', line 57

def size
  raise 'Remote size not yet obtained, need to perform at least one read() to retrieve it' unless @remote_size
  @remote_size
end