Class: WebDump

Inherits:
Object
  • Object
show all
Defined in:
lib/web_dump.rb,
lib/web_dump/version.rb

Overview

it’s only a version number

Defined Under Namespace

Modules: Version

Constant Summary collapse

DEFAULT_ATTRS =

default attributes

{
  :base_dir => '~/web_dumps',
  :file_ext => '.html'
}

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ WebDump

initializes a WebDump object. options should be a hash with options for an UriPathname object that will be internally created. Default UriPathnames options and additionally:

:base_dir => directory where everything will be stored (def. '~/web_dumps')
:file_ext => extension that will be appended to filenames (def. '.html')


29
30
31
32
33
34
35
36
# File 'lib/web_dump.rb', line 29

def initialize(options = {})

  attributes = DEFAULT_ATTRS.merge options if options.is_a? Hash
  attributes.each { |k,v| instance_eval("@#{k}='#{v}'") if DEFAULT_ATTRS.keys.include?(k) }

  @up=UriPathname.new attributes  # any valid option passed will be delivered

end

Instance Method Details

#read_pathname(pathname) ⇒ Object

returns the stored content corresponding to file pathname. In case there isn’t any file it returns nil.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/web_dump.rb', line 68

def read_pathname(pathname)
  content = nil
  arr = @up.parse pathname
  complete_pathname = File.expand_path(pathname)
  extension = arr[2]
  case extension
  when /.gz/
    File.open(complete_pathname, 'r') do |f|
      gz = Zlib::GzipReader.new(f)
      content = gz.read
      gz.close
    end
  else # others as is
    File.open(complete_pathname, 'r') do |f|
      content = f.read
    end
  end
  content
end

#read_uri(uri, filext = nil) ⇒ Object

returns the stored content corresponding to uri URI. In case there isn’t any file it returns nil.



90
91
92
93
94
# File 'lib/web_dump.rb', line 90

def read_uri(uri, filext=nil)
  filext = @file_ext unless filext
  pathname = @up.uri_to_pathname(uri,nil,filext)
  read_pathname(pathname)
end

#save(uri, content, extension = nil) ⇒ Object

saves the content (String) into a file named after UriPathname#uri_to_pathname(uri). If extension is nil initialize :file_ext option will be used:

'anything'+'.gz' ->  gzipped (less storage requirements)
other ->  as is

returns a String containing the complete pathname of the file if OK else nil



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/web_dump.rb', line 44

def save(uri, content, extension = nil)
  extension = @file_ext unless extension
  pathname = @up.uri_to_pathname(uri,nil,extension)
  return nil unless pathname
  mkdir_if_not_exists(File.dirname(pathname))
  num_bytes = nil
  case extension
  when /\.gz$/  # ...gz
    File.open(pathname, 'w') do |f|
      gz = Zlib::GzipWriter.new(f)
      # gz.comment="#dumped with web_dump #{Version::STRING}: #{uri}" # no sembla fer res
      num_bytes = gz.write content
      gz.close
    end
  else # any other
    File.open(pathname, 'w') do |f|
      num_bytes = f.write(content)
    end
  end
  num_bytes ? pathname : nil
end