From 218e3a45f2405689534531a4658d3c747d6064e3 Mon Sep 17 00:00:00 2001 From: MUTOgen Date: Fri, 27 Sep 2024 18:02:07 +0200 Subject: [PATCH] Update middlewares --- .../vcr/insert_eject_middleware.rb | 9 +++--- ...se_middleware.rb => middleware_helpers.rb} | 28 ++++++------------- .../vcr/use_cassette_middleware.rb | 9 ++---- 3 files changed, 17 insertions(+), 29 deletions(-) rename lib/cypress_on_rails/vcr/{base_middleware.rb => middleware_helpers.rb} (55%) diff --git a/lib/cypress_on_rails/vcr/insert_eject_middleware.rb b/lib/cypress_on_rails/vcr/insert_eject_middleware.rb index 33d20e2..9d16c74 100644 --- a/lib/cypress_on_rails/vcr/insert_eject_middleware.rb +++ b/lib/cypress_on_rails/vcr/insert_eject_middleware.rb @@ -1,12 +1,13 @@ -require_relative 'base_middleware' +require_relative 'middleware_helpers' module CypressOnRails module Vcr # Middleware to handle vcr with insert/eject endpoints - class InsertEjectMiddleware < BaseMiddleware + class InsertEjectMiddleware + include MiddlewareHelpers + def initialize(app, vcr = nil) - @app = app - @vcr = vcr + super(app, vcr) @first_call = false end diff --git a/lib/cypress_on_rails/vcr/base_middleware.rb b/lib/cypress_on_rails/vcr/middleware_helpers.rb similarity index 55% rename from lib/cypress_on_rails/vcr/base_middleware.rb rename to lib/cypress_on_rails/vcr/middleware_helpers.rb index 5d2d082..817f930 100644 --- a/lib/cypress_on_rails/vcr/base_middleware.rb +++ b/lib/cypress_on_rails/vcr/middleware_helpers.rb @@ -1,25 +1,24 @@ -require 'json' -require 'rack' require 'cypress_on_rails/middleware_config' module CypressOnRails module Vcr - # Base abstract Middleware - class BaseMiddleware + # Provides helper methods for VCR middlewares + module MiddlewareHelpers include MiddlewareConfig - def initialize(**_args) - raise_not_implemented - end - - def call(_env) - raise_not_implemented + def initialize(app, vcr = nil) + @app = app + @vcr = vcr end def vcr @vcr ||= configure_vcr end + def cassette_library_dir + "#{configuration.install_folder}/fixtures/vcr_cassettes" + end + private def configure_vcr @@ -29,15 +28,6 @@ def configure_vcr end VCR end - - def cassette_library_dir - "#{configuration.install_folder}/fixtures/vcr_cassettes" - end - - def raise_not_implemented - raise NotImplementedError, - 'BaseMiddleware can not be initialized directly, use InsertEjectMiddleware or UseCassetteMiddleware' - end end end end diff --git a/lib/cypress_on_rails/vcr/use_cassette_middleware.rb b/lib/cypress_on_rails/vcr/use_cassette_middleware.rb index 79d0f48..6fda66b 100644 --- a/lib/cypress_on_rails/vcr/use_cassette_middleware.rb +++ b/lib/cypress_on_rails/vcr/use_cassette_middleware.rb @@ -1,13 +1,10 @@ -require_relative 'base_middleware' +require_relative 'middleware_helpers' module CypressOnRails module Vcr # Middleware to handle vcr with use_cassette - class UseCassetteMiddleware < BaseMiddleware - def initialize(app, vcr = nil) - @app = app - @vcr = vcr - end + class UseCassetteMiddleware + include MiddlewareHelpers def call(env) return @app.call(env) if should_not_use_vcr?