Class: Typhoeus::SpecCache

Inherits:
Object show all
Defined in:
lib/typhoeus/spec_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hydra, cache_path) ⇒ SpecCache

Returns a new instance of SpecCache.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/typhoeus/spec_cache.rb', line 7

def initialize(hydra, cache_path)
  @responses = {}
  @cache_path = cache_path

  @hydra = hydra
  @hydra.cache_setter do |request|
    cache_setter_callback(request)
  end
  @hydra.cache_getter do |request|
    cache_getter_callback(request)
  end

  read_cache_fixtures!
end

Instance Attribute Details

#cache_pathObject

Returns the value of attribute cache_path.



4
5
6
# File 'lib/typhoeus/spec_cache.rb', line 4

def cache_path
  @cache_path
end

#hydraObject (readonly)

Returns the value of attribute hydra.



5
6
7
# File 'lib/typhoeus/spec_cache.rb', line 5

def hydra
  @hydra
end

#responsesObject

Returns the value of attribute responses.



3
4
5
# File 'lib/typhoeus/spec_cache.rb', line 3

def responses
  @responses
end

Instance Method Details

#cache_filesObject



82
83
84
# File 'lib/typhoeus/spec_cache.rb', line 82

def cache_files
  Dir.glob("#{cache_path}/*.cache")
end

#cache_getter_callback(request) ⇒ Object



26
27
28
# File 'lib/typhoeus/spec_cache.rb', line 26

def cache_getter_callback(request)
  responses[request.cache_key]
end

#cache_setter_callback(request) ⇒ Object



22
23
24
# File 'lib/typhoeus/spec_cache.rb', line 22

def cache_setter_callback(request)
  responses[request.cache_key] = request.response
end

#clear_cache!Object



78
79
80
# File 'lib/typhoeus/spec_cache.rb', line 78

def clear_cache!
  @responses = {}
end

#clear_hydra_callbacks!Object



68
69
70
71
72
73
74
75
76
# File 'lib/typhoeus/spec_cache.rb', line 68

def clear_hydra_callbacks!
  if hydra.respond_to?(:clear_cache_callbacks)
    hydra.clear_cache_callbacks
  else
    # you like that? do you?!?!
    hydra.instance_variable_set("@cache_setter", nil)
    hydra.instance_variable_set("@cache_getter", nil)
  end
end

#dump_cache_fixtures!Object

Dumps out any cached items to disk.



57
58
59
60
61
62
63
64
65
66
# File 'lib/typhoeus/spec_cache.rb', line 57

def dump_cache_fixtures!
  responses.each do |cache_key, response|
    path = filepath_for(cache_key)
    unless File.exist?(path)
      File.open(path, "wb") do |fp|
        fp.write(Marshal.dump(response))
      end
    end
  end
end

#filepath_for(cache_key) ⇒ Object



30
31
32
# File 'lib/typhoeus/spec_cache.rb', line 30

def filepath_for(cache_key)
  "#{cache_path}/#{cache_key}.cache"
end

#read_cache_fixtures!Object

Reads in the cache fixture files to in-memory cache.



48
49
50
51
52
53
54
# File 'lib/typhoeus/spec_cache.rb', line 48

def read_cache_fixtures!
  files = cache_files
  files.each do |file|
    cache_key = get_cache_key_from_filename(file)
    responses[cache_key] = Marshal.load(File.read(file))
  end
end

#remove_unnecessary_cache_files!Object

Removes any cache files that aren’t needed anymore



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/typhoeus/spec_cache.rb', line 35

def remove_unnecessary_cache_files!
  current_keys = cache_files.map do |file|
    get_cache_key_from_filename(file)
  end
  inmemory_keys = responses.keys

  unneeded_keys = current_keys - inmemory_keys
  unneeded_keys.each do |key|
    File.unlink(filepath_for(key))
  end
end