Sha256: ab7916ac80acd3f737f0431eadd74ff661acdca076d7f6b7fcb5d28f3ce9c099

Contents?: true

Size: 1.72 KB

Versions: 8

Compression:

Stored size: 1.72 KB

Contents

require 'json'
require 'rack'
require 'cypress_dev/configuration'
require 'cypress_dev/command_executor'

module CypressDev
  # Middleware to handle cypress commands and eval
  class Middleware
    def initialize(app, command_executor = CommandExecutor, file = ::File)
      @app = app
      @command_executor = command_executor
      @file = file
    end

    def call(env)
      request = Rack::Request.new(env)
      if request.path.start_with?('/__cypress__/command')
        configuration.tagged_logged { handle_command(request) }
      else
        @app.call(env)
      end
    end

    private

    def configuration
      CypressDev.configuration
    end

    def logger
      configuration.logger
    end

    Command = Struct.new(:name, :options, :cypress_folder) do
      # @return [Array<Cypress::Middleware::Command>]
      def self.from_body(body, configuration)
        if body.is_a?(Array)
          command_params = body
        else
          command_params = [body]
        end
        command_params.map do |params|
          new(params.fetch('name'), params['options'], configuration.cypress_folder)
        end
      end

      def file_path
        "#{cypress_folder}/app_commands/#{name}.rb"
      end
    end

    def handle_command(req)
      body = JSON.parse(req.body.read)
      logger.info "handle_command: #{body}"
      commands = Command.from_body(body, configuration)
      missing_command = commands.find {|command| !@file.exists?(command.file_path) }
      if missing_command.nil?
        commands.each { |command| @command_executor.load(command.file_path, command.options) }
        [201, {}, ['success']]
      else
        [404, {}, ["could not find command file: #{missing_command.file_path}"]]
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
cypress-on-rails-1.3.0 lib/cypress_dev/middleware.rb
cypress-on-rails-1.2.1 lib/cypress_dev/middleware.rb
cypress-on-rails-1.2.0 lib/cypress_dev/middleware.rb
cypress-on-rails-1.1.1 lib/cypress_dev/middleware.rb
cypress-on-rails-1.1.0 lib/cypress_dev/middleware.rb
cypress-on-rails-1.0.1 lib/cypress_dev/middleware.rb
cypress-on-rails-1.0.0 lib/cypress_dev/middleware.rb
cypress-on-rails-1.0.0.beta2 lib/cypress_dev/middleware.rb