Class: HttpZip::RangeRequest

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

Overview

Class to make Range requests to a HTTP server

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ RangeRequest

Create a new RangeRequest object

Parameters:

  • url (String)

    remote file URL



11
12
13
14
15
# File 'lib/http_zip/range_request.rb', line 11

def initialize(url)
  @uri = URI(url)
  @connection = Net::HTTP.new(@uri.host, @uri.port)
  @connection.use_ssl = true if @uri.scheme == "https"
end

Instance Method Details

#get(from, to) {|chunk| ... } ⇒ Object

Request a partial object via HTTP. If a block is given, yields the response body in chunks.

Parameters:

  • from (Integer)

    start byte of the range to request. Inclusive.

  • to (Integer)

    end byte of the range to request. Exclusive.

Yields:

  • (chunk)

    yields a chunk of data to the block

Raises:

  • (ContentRangeError)

    if the server responds with anything other than 206 Partial Content



23
24
25
26
27
# File 'lib/http_zip/range_request.rb', line 23

def get(from, to, &block)
  request = Net::HTTP::Get.new(@uri)
  request["Range"] = "bytes=#{from}-#{to - 1}"
  make_request(request, &block)
end

#last(num_bytes) ⇒ Object

Request the last ‘num_bytes` bytes of the remote file via HTTP.

Parameters:

  • num_bytes (Integer)

    number of bytes to request

Raises:

  • (ContentRangeError)

    if the server responds with anything other than 206 Partial Content



33
34
35
36
37
# File 'lib/http_zip/range_request.rb', line 33

def last(num_bytes)
  request = Net::HTTP::Get.new(@uri)
  request["Range"] = "bytes=-#{num_bytes}"
  make_request(request)
end