class Endpointer::Cacher
Public Class Methods
new(path)
click to toggle source
# File lib/endpointer/cacher.rb, line 11 def initialize(path) initialize_path(path) end
Public Instance Methods
get(resource, request_body)
click to toggle source
# 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
invalidate()
click to toggle source
# File lib/endpointer/cacher.rb, line 27 def invalidate FileUtils.remove_entry(@path) initialize_path(@path) end
set(resource, request_body, response)
click to toggle source
# 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
Private Instance Methods
create_cache_container(resource, response)
click to toggle source
# File lib/endpointer/cacher.rb, line 34 def create_cache_container(resource, response) Endpointer::CacheContainer.new(resource, response, Time.now.utc) end
get_cache_key(resource, request_body)
click to toggle source
# File lib/endpointer/cacher.rb, line 46 def get_cache_key(resource, request_body) Endpointer::CacheKeyResolver.new.get_key(resource, request_body) end
initialize_path(path)
click to toggle source
# File lib/endpointer/cacher.rb, line 50 def initialize_path(path) begin @path = path Dir.mkdir(@path) unless File.exist?(@path) rescue Errno::ENOENT => e raise Endpointer::Errors::InvalidCacheDirError, e.message end end
present_response(resource, request_body, cache_container)
click to toggle source
# File lib/endpointer/cacher.rb, line 59 def present_response(resource, request_body, cache_container) Endpointer::ResponsePresenter.new.present( status: cache_container.response.code, body: cache_container.response.body, headers: cache_container.response.headers, request_body: request_body, resource: resource ) end
retrieve_cache_container(cache_key)
click to toggle source
# File lib/endpointer/cacher.rb, line 38 def retrieve_cache_container(cache_key) begin YAML.load(File.read(File.join(@path, cache_key))) rescue Errno::ENOENT => e raise Endpointer::Errors::CachedItemNotFoundError, e.message end end