Class: Billy::Cache

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



10
11
12
# File 'lib/billy/cache.rb', line 10

def initialize
  reset
end

Instance Attribute Details

#scopeObject

Returns the value of attribute scope.



8
9
10
# File 'lib/billy/cache.rb', line 8

def scope
  @scope
end

Instance Method Details

#cache_file(key) ⇒ Object



97
98
99
# File 'lib/billy/cache.rb', line 97

def cache_file(key)
  File.join(Billy.config.cache_path, "#{key}.yml")
end

#cached?(method, url, body) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
# File 'lib/billy/cache.rb', line 14

def cached?(method, url, body)
  # Only log the key the first time it's looked up (in this method)
  key = key(method, url, body, true)
  !@cache[key].nil? or persisted?(key)
end

#fetch(method, url, body) ⇒ Object



24
25
26
27
# File 'lib/billy/cache.rb', line 24

def fetch(method, url, body)
  key = key(method, url, body)
  @cache[key] or fetch_from_persistence(key)
end

#fetch_from_persistence(key) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/billy/cache.rb', line 29

def fetch_from_persistence(key)
  begin
    @cache[key] = YAML.load(File.open(cache_file(key))) if persisted?(key)
  rescue ArgumentError => e
    puts "Could not parse YAML: #{e.message}"
    nil
  end
end

#format_url(url, ignore_params = false) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/billy/cache.rb', line 86

def format_url(url, ignore_params=false)
  url = URI(url)
  port_to_include = Billy.config.ignore_cache_port ? '' : ":#{url.port}"
  formatted_url = url.scheme+'://'+url.host+port_to_include+url.path
  unless ignore_params
    formatted_url += '?'+url.query if url.query
    formatted_url += '#'+url.fragment if url.fragment
  end
  formatted_url
end

#key(method, orig_url, body, log_key = false) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/billy/cache.rb', line 70

def key(method, orig_url, body, log_key = false)
  ignore_params = Billy.config.ignore_params.include?(format_url(orig_url, true))
  url = URI(format_url(orig_url, ignore_params))
  key = method+'_'+url.host+'_'+Digest::SHA1.hexdigest(scope.to_s + url.to_s)
  body_msg = ''

  if method == 'post' and !ignore_params
    body_formatted = JSONUtils::json?(body.to_s) ? JSONUtils::sort_json(body.to_s) : body.to_s
    body_msg = " with body '#{body_formatted}'"
    key += '_'+Digest::SHA1.hexdigest(body_formatted)
  end

  Billy.log(:info, "puffing-billy: CACHE KEY for '#{orig_url}#{body_msg}' is '#{key}'") if log_key
  key
end

#persisted?(key) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/billy/cache.rb', line 20

def persisted?(key)
  Billy.config.persist_cache and File.exists?(cache_file(key))
end

#resetObject



66
67
68
# File 'lib/billy/cache.rb', line 66

def reset
  @cache = {}
end

#scope_to(new_scope = nil) ⇒ Object



101
102
103
# File 'lib/billy/cache.rb', line 101

def scope_to(new_scope = nil)
  self.scope = new_scope
end

#store(method, url, request_headers, body, response_headers, status, content) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/billy/cache.rb', line 38

def store(method, url, request_headers, body, response_headers, status, content)
  cached = {
    :scope => scope,
    :url => format_url(url),
    :body => body,
    :status => status,
    :method => method,
    :headers => response_headers,
    :content => content
  }

  cached.merge!({:request_headers => request_headers}) if Billy.config.cache_request_headers

  key = key(method, url, body)
  @cache[key] = cached

  if Billy.config.persist_cache
    Dir.mkdir(Billy.config.cache_path) unless File.exists?(Billy.config.cache_path)

    begin
      File.open(cache_file(key), 'w') do |f|
        f.write(cached.to_yaml(:Encoding => :Utf8))
      end
    rescue StandardError => e
    end
  end
end

#use_default_scopeObject



114
115
116
# File 'lib/billy/cache.rb', line 114

def use_default_scope
  scope_to nil
end

#with_scope(use_scope = nil, &block) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/billy/cache.rb', line 105

def with_scope(use_scope = nil, &block)
  raise ArgumentError, 'Expected a block but none was received.' if block.nil?
  original_scope = scope
  scope_to use_scope
  block.call()
ensure
  scope_to original_scope
end