Sha256: 7748a044f4349fe1526a5d3efce00428888042328db3049633b16153000364d4

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal:true

require 'docker'
require 'em/pure_ruby'
require 'watir'
require 'forwardable'

# Scope Rails
module R2OAS
  module Schema
    class UI < Base
      extend Forwardable

      alias swagger_json doc_save_file_path

      def initialize(options = {})
        super
        @ui = swagger.ui
      end

      def start
        EM.run do
          container.start
          open_browser
          puts "\nwait for single trap ..."
          signal_trap('INT')
          signal_trap('TERM')
        end
      end

      private

      attr_accessor :unit_paths_file_path
      def_delegators :@ui, :image, :port, :url, :exposed_port, :volume

      def signal_trap(command)
        Signal.trap(command) do
          container.stop
          container.remove
          logger.info "container id: #{container.id} removed"

          EM.stop
        end
      end

      def open_browser
        @browser ||= Watir::Browser.new
        @browser.goto(url)
        wait_for_loaded
      end

      def wait_for_loaded
        Watir::Wait.until { @browser.body.present? }
      end

      # [Reference]
      # https://www.tomduffield.com/files/presentations/the-nitty-gritty-of-the-docker-api.pdf
      def container
        @container ||= Docker::Container.create(
          'Image' => image,
          'ExposedPorts' => { exposed_port => {} },
          'HostConfig' => {
            'PortBindings' => {
              exposed_port => [{ 'HostPort' => port }]
            },
            'Binds' => ["#{swagger_json}:#{volume}"]
          },
          'Volumes' => { volume => {} }
        )
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
r2-oas-0.5.0 lib/r2-oas/schema/ui.rb