Class: Plumnailer::CachingFetcher
- Defined in:
- lib/plumnailer/caching_fetcher.rb
Overview
Fetch the contents of a url and cache result on filesystem.
Constant Summary
Constants inherited from Fetcher
Instance Attribute Summary collapse
-
#cache_dir ⇒ Object
Returns the value of attribute cache_dir.
Attributes inherited from Fetcher
Instance Method Summary collapse
-
#fetch(url, orig_url = nil, redirect_count = 0) ⇒ Object
Fetch the contents of a url and cache result on filesystem.
-
#initialize(cache_dir) ⇒ CachingFetcher
constructor
A new instance of CachingFetcher.
Constructor Details
#initialize(cache_dir) ⇒ CachingFetcher
Returns a new instance of CachingFetcher.
11 12 13 14 |
# File 'lib/plumnailer/caching_fetcher.rb', line 11 def initialize(cache_dir) @cache_dir = cache_dir FileUtils.mkdir_p cache_dir end |
Instance Attribute Details
#cache_dir ⇒ Object
Returns the value of attribute cache_dir.
29 30 31 |
# File 'lib/plumnailer/caching_fetcher.rb', line 29 def cache_dir @cache_dir end |
Instance Method Details
#fetch(url, orig_url = nil, redirect_count = 0) ⇒ Object
Fetch the contents of a url and cache result on filesystem.
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/plumnailer/caching_fetcher.rb', line 17 def fetch(url, orig_url=nil, redirect_count=0) cache_file = File.join(cache_dir, CGI.escape(url.to_s)) if File.exists?(cache_file) open(cache_file) { |f| f.read } else data = super open(cache_file, 'w') { |f| f.write(data) } data end end |