Class: Endpointer::Cacher

Inherits:
Object
  • Object
show all
Defined in:
lib/endpointer/cacher.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Cacher

Returns a new instance of Cacher.



10
11
12
# File 'lib/endpointer/cacher.rb', line 10

def initialize(path)
  initialize_path(path)
end

Instance Method Details

#get(resource, request_body) ⇒ Object



14
15
16
17
18
19
# File 'lib/endpointer/cacher.rb', line 14

def get(resource, request_body)
  cache_key = get_cache_key(resource, request_body)
  cache_container = retrieve_cache_container(cache_key)
  raise Endpointer::Errors::CachedItemNotFoundError unless cache_container.resource == resource
  cache_container.response
end

#invalidateObject



26
27
28
29
# File 'lib/endpointer/cacher.rb', line 26

def invalidate
  FileUtils.remove_entry(@path)
  initialize_path(@path)
end

#set(resource, request_body, response) ⇒ Object



21
22
23
24
# File 'lib/endpointer/cacher.rb', line 21

def set(resource, request_body, response)
  cache_container = create_cache_container(resource, response)
  File.write(File.join(@path, get_cache_key(resource, request_body)), YAML.dump(cache_container))
end