Class: WebCache

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#dirObject

Returns the value of attribute dir.



8
9
10
# File 'lib/webcache/web_cache.rb', line 8

def dir
  @dir
end

#last_errorObject (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

#lifeObject

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

Returns:

  • (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

#disableObject



43
44
45
# File 'lib/webcache/web_cache.rb', line 43

def disable
  @enabled = false
end

#enableObject



39
40
41
# File 'lib/webcache/web_cache.rb', line 39

def enable
  @enabled = true
end

#enabled?Boolean

Returns:

  • (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