Class: SeaShanty::RequestStore
- Inherits:
-
Object
- Object
- SeaShanty::RequestStore
- Defined in:
- lib/sea_shanty/request_store.rb
Instance Method Summary collapse
- #fetch(request, &block) ⇒ Object
-
#initialize(configuration, generic_responses: configuration.generic_responses, storage_dir: configuration.storage_dir) ⇒ RequestStore
constructor
A new instance of RequestStore.
- #load_response(path, request) ⇒ Object
- #request_file_path(request) ⇒ Object
- #store(path, request, response) ⇒ Object
Constructor Details
#initialize(configuration, generic_responses: configuration.generic_responses, storage_dir: configuration.storage_dir) ⇒ RequestStore
Returns a new instance of RequestStore.
10 11 12 13 14 15 |
# File 'lib/sea_shanty/request_store.rb', line 10 def initialize(configuration, generic_responses: configuration.generic_responses, storage_dir: configuration.storage_dir) @configuration = configuration @generic_responses = generic_responses @storage_dir = Pathname.new(storage_dir) @request_serializer = RequestSerializer.new(headers_filter: configuration.request_headers_filter, body_filter: configuration.request_body_filter) end |
Instance Method Details
#fetch(request, &block) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/sea_shanty/request_store.rb', line 17 def fetch(request, &block) if configuration.bypass? raise ConfigurationError, "Bypass and readonly are both true - please set only one of them." if configuration.readonly? yield else path = request_file_path(request) if configuration.readonly? || path.exist? load_response(path, request) else response = yield store(path, request, response) unless configuration.bypass? response end end end |
#load_response(path, request) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/sea_shanty/request_store.rb', line 33 def load_response(path, request) raise UnknownRequest, "SeaShanty: Unknown request #{request.method.to_s.upcase} to #{request.url}" unless path.exist? log("Loading response for #{request.url} from #{path}") contents = YAML.safe_load(Pathname(path).read, permitted_classes: [Symbol, Time, DateTime]) Response.from_h(contents.fetch(:response)) end |
#request_file_path(request) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/sea_shanty/request_store.rb', line 48 def request_file_path(request) _, generic_file_path = generic_responses.find { |matcher, path| matcher.match?(request.url.to_s) } file_path = if generic_file_path.nil? request_serializer.file_path(request).tap { |path| log("Generated #{path} for request to #{request.url}") } else log("Found a generic response in #{generic_file_path} for request to #{request.url}") Pathname.new(generic_file_path.to_s) end storage_dir.join(file_path) end |
#store(path, request, response) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/sea_shanty/request_store.rb', line 40 def store(path, request, response) log("Storing response for #{request.url} in #{path}") path.dirname.mkpath path.open("w+") do |file| file.write(YAML.dump(serialize(request, response))) end end |