Class: Scrapes::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/scrapes/cache.rb

Overview

Cache web pages

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



46
47
48
49
50
# File 'lib/scrapes/cache.rb', line 46

def initialize
  @directory = File.expand_path('cache')
  @enabled = false
  @uri_translator = nil
end

Instance Attribute Details

#directoryObject

Set the directory to use for caching



38
39
40
# File 'lib/scrapes/cache.rb', line 38

def directory
  @directory
end

#enabledObject

Enable/disable caching



34
35
36
# File 'lib/scrapes/cache.rb', line 34

def enabled
  @enabled
end

#uri_translatorObject

Set to a proc that given an URI, translates it to a file system name default is to MD5 the URL



43
44
45
# File 'lib/scrapes/cache.rb', line 43

def uri_translator
  @uri_translator
end

Instance Method Details

#check(uri) ⇒ Object

Checks the cache to see if there is a match for the given URI



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/scrapes/cache.rb', line 63

def check (uri)
  return nil unless @enabled

  # FIXME check some time limits around this
  cache_name = translate_uri(uri)
  
  file = File.join(@directory, cache_name)
  File.exist?(file) and File.open(file) do |f|
    return OpenStruct.new(:body => f.read, :cache_file => file)
  end
end

#update(uri, data) ⇒ Object

Updates the cache by placing the data for the give URI on the file system.



77
78
79
80
81
82
83
84
85
86
# File 'lib/scrapes/cache.rb', line 77

def update (uri, data)
  return nil unless @enabled

  cache_name = translate_uri(uri)
  mkdir # FIXME include cache_name to build all necessary directories 

  File.open(File.join(@directory, cache_name), 'w') do |f|
    f << data
  end
end

#uri_to_md5(uri) ⇒ Object

helper method to translate a URL to a MD5



90
91
92
# File 'lib/scrapes/cache.rb', line 90

def uri_to_md5 (uri)
  Digest::MD5.hexdigest(uri.to_s)
end

#without_cacheObject

Disables caching while the given block is active.



54
55
56
57
58
59
# File 'lib/scrapes/cache.rb', line 54

def without_cache
  state = @enabled
  @enabled = false
  yield if block_given?
  @enabled = state
end