Class: EightyLegs::EightyFormat

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

Defined Under Namespace

Classes: FileFormatError

Instance Method Summary collapse

Constructor Details

#initialize(filename_or_io) ⇒ EightyFormat

Returns a new instance of EightyFormat.



3
4
5
6
7
8
9
10
11
12
# File 'lib/eighty_legs/eighty_format.rb', line 3

def initialize(filename_or_io)
  if filename_or_io.is_a?(String)
    initialize_with_filename(filename_or_io)
  elsif filename_or_io.respond_to?(:read)
    initialize_with_io(filename_or_io)
  else
    raise TypeError.new(filename_or_io.class)
  end
  check_for_classid_and_version()
end

Instance Method Details

#each(&blk) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/eighty_legs/eighty_format.rb', line 25

def each(&blk)
  while not @io.eof?
    url_size = @io.read(4).unpack("i").first
    url = @io.read(url_size)
    data_size = @io.read(4).unpack("i").first
    data = @io.read(data_size)
    blk.call(Entry.new(url, data))
  end
  @io.rewind
  @io.read(8)
end

#initialize_with_filename(filename) ⇒ Object



14
15
16
# File 'lib/eighty_legs/eighty_format.rb', line 14

def initialize_with_filename(filename)
  initialize_with_io(File.open(filename))
end

#initialize_with_io(io) ⇒ Object



18
19
20
21
22
23
# File 'lib/eighty_legs/eighty_format.rb', line 18

def initialize_with_io(io)
  @io = Zlib::GzipReader.new(io)
rescue Zlib::GzipFile::Error
  @io = io
  @io.rewind
end