class Dimscan::HTTPByteEnumerator

A utility class for enumerating over the bytes of a http response body

Public Class Methods

http() click to toggle source
# File lib/dimscan/http_byte_enumerator.rb, line 9
def http
  @http ||= Net::HTTP::Persistent.new('dimscan')
end
new(url) click to toggle source
# File lib/dimscan/http_byte_enumerator.rb, line 14
def initialize(url)
  @uri = URI(url)
  fail ArgumentError, 'invalid scheme' unless @uri.scheme =~ /^https?$/
end

Public Instance Methods

each(&block) click to toggle source
# File lib/dimscan/http_byte_enumerator.rb, line 19
def each(&block)
  return to_enum(__callee__) unless block_given?
  self.class.http.request(@uri) do |response|
    fail response.body if error?(response)
    enumerate_response(response, &block)
  end
end

Protected Instance Methods

enumerate_response(response, &block) click to toggle source
# File lib/dimscan/http_byte_enumerator.rb, line 35
def enumerate_response(response, &block)
  response.read_body do |chunk|
    chunk.each_byte(&block)
  end
end
error?(response) click to toggle source
# File lib/dimscan/http_byte_enumerator.rb, line 29
def error?(response)
  [Net::HTTPError, Net::HTTPClientError].any? do |error|
    response.is_a?(error)
  end
end