Class: LocalCopy

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

Constant Summary collapse

VERSION =
"0.0.6"

Class Method Summary collapse

Class Method Details

.fetch(url, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/local_copy.rb', line 6

def self.fetch(url, &block)
  path = File.join(local_dir, Digest::MD5.hexdigest(url))
  unless File.exist?(path)
    dirname = File.dirname(path)
    Dir.mkdir(dirname) unless File.exist?(dirname)
    content = ''
    open(url) { |url| content << url.read }
    content = block.call(content) if block
    File.open(path, 'wb') { |file| file << content }
  end
  path
end

.flushObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/local_copy.rb', line 19

def self.flush
  deleted_files = []
  return deleted_files unless File.exist?(local_dir)
  (Dir.entries(local_dir) - %w(. ..)).each do |file|
    path = File.join(local_dir, file)
    File.delete(path)
    deleted_files << path
  end
  deleted_files
end

.local_dirObject



30
31
32
# File 'lib/local_copy.rb', line 30

def self.local_dir
  File.join('', 'tmp', 'local_copy_gem')
end