Class: Datahen::Client::BackblazeContent

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/datahen/client/backblaze_content.rb

Instance Method Summary collapse

Instance Method Details

#get_content(url) ⇒ Object



9
10
11
# File 'lib/datahen/client/backblaze_content.rb', line 9

def get_content(url)
  self.class.get(url, format: :plain).response.body
end

#get_gunzipped_content(url) ⇒ Object



13
14
15
16
# File 'lib/datahen/client/backblaze_content.rb', line 13

def get_gunzipped_content(url)
  # Zlib.gunzip(get_content(url))
  gunzip(get_content(url))
end

#gunzip(string) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/datahen/client/backblaze_content.rb', line 18

def gunzip(string)
  sio = StringIO.new(string)
  gz = Zlib::GzipReader.new(sio, encoding: Encoding::ASCII_8BIT)
  _content = ""
  begin
    _content = gz.read
  rescue => e
    # if unexpected eof error, then readchar until error, and ignore it
    if e.to_s == 'unexpected end of file'
      # heavily improve content read recovery by using "String#<<",
      #  reading all "good" lines and then concat the remaining chars
      begin
        gz.each_line{|line| _content << line}
      rescue => e
        begin
          _content << gz.readchar while !gz.eof
        rescue => e
          puts "Ignored Zlib error: #{e.to_s}"
        end
      end
    else
      raise e
    end
  end

  return _content
ensure
  gz.close if gz.respond_to?(:close)
end