Class: Scrapes::Cache
- Inherits:
-
Object
- Object
- Scrapes::Cache
- Defined in:
- lib/scrapes/cache.rb
Overview
Cache web pages
Instance Attribute Summary collapse
-
#directory ⇒ Object
Set the directory to use for caching.
-
#enabled ⇒ Object
Enable/disable caching.
-
#uri_translator ⇒ Object
Set to a proc that given an URI, translates it to a file system name default is to MD5 the URL.
Instance Method Summary collapse
-
#check(uri) ⇒ Object
Checks the cache to see if there is a match for the given URI.
-
#initialize ⇒ Cache
constructor
A new instance of Cache.
-
#update(uri, data) ⇒ Object
Updates the cache by placing the data for the give URI on the file system.
-
#uri_to_md5(uri) ⇒ Object
helper method to translate a URL to a MD5.
-
#without_cache ⇒ Object
Disables caching while the given block is active.
Constructor Details
#initialize ⇒ Cache
Returns a new instance of Cache.
46 47 48 49 50 |
# File 'lib/scrapes/cache.rb', line 46 def initialize @directory = File.('cache') @enabled = false @uri_translator = nil end |
Instance Attribute Details
#directory ⇒ Object
Set the directory to use for caching
38 39 40 |
# File 'lib/scrapes/cache.rb', line 38 def directory @directory end |
#enabled ⇒ Object
Enable/disable caching
34 35 36 |
# File 'lib/scrapes/cache.rb', line 34 def enabled @enabled end |
#uri_translator ⇒ Object
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_cache ⇒ Object
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 |