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.



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

def initialize(path)
  initialize_path(path)
end

Instance Method Details

#get(resource, request_body) ⇒ Object



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

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
  present_response(resource, request_body, cache_container)
end

#invalidateObject



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

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

#set(resource, request_body, response) ⇒ Object



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

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