Class: WebCache
- Inherits:
-
Object
- Object
- WebCache
- Defined in:
- lib/webcache/version.rb,
lib/webcache/response.rb,
lib/webcache/web_cache.rb
Defined Under Namespace
Classes: Response
Constant Summary collapse
- VERSION =
"0.2.3"
Instance Attribute Summary collapse
-
#dir ⇒ Object
Returns the value of attribute dir.
-
#last_error ⇒ Object
readonly
Returns the value of attribute last_error.
-
#life ⇒ Object
Returns the value of attribute life.
Instance Method Summary collapse
- #cached?(url) ⇒ Boolean
- #disable ⇒ Object
- #enable ⇒ Object
- #enabled? ⇒ Boolean
- #get(url) ⇒ Object
-
#initialize(dir = 'cache', life = 3600) ⇒ WebCache
constructor
A new instance of WebCache.
Constructor Details
#initialize(dir = 'cache', life = 3600) ⇒ WebCache
Returns a new instance of WebCache.
10 11 12 13 14 |
# File 'lib/webcache/web_cache.rb', line 10 def initialize(dir='cache', life=3600) @dir = dir @life = life @enabled = true end |
Instance Attribute Details
#dir ⇒ Object
Returns the value of attribute dir.
8 9 10 |
# File 'lib/webcache/web_cache.rb', line 8 def dir @dir end |
#last_error ⇒ Object (readonly)
Returns the value of attribute last_error.
7 8 9 |
# File 'lib/webcache/web_cache.rb', line 7 def last_error @last_error end |
#life ⇒ Object
Returns the value of attribute life.
8 9 10 |
# File 'lib/webcache/web_cache.rb', line 8 def life @life end |
Instance Method Details
#cached?(url) ⇒ Boolean
30 31 32 33 |
# File 'lib/webcache/web_cache.rb', line 30 def cached?(url) path = get_path url File.exist?(path) and !old?(path) end |
#disable ⇒ Object
43 44 45 |
# File 'lib/webcache/web_cache.rb', line 43 def disable @enabled = false end |
#enable ⇒ Object
39 40 41 |
# File 'lib/webcache/web_cache.rb', line 39 def enable @enabled = true end |
#enabled? ⇒ Boolean
35 36 37 |
# File 'lib/webcache/web_cache.rb', line 35 def enabled? @enabled end |
#get(url) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/webcache/web_cache.rb', line 16 def get(url) return http_get url unless enabled? path = get_path url FileUtils.rm path if old? path return load_file_content(path) if File.exist? path response = http_get(url) save_file_content(path, response) unless !response || response.error response end |