class R2OAS::Schema::Editor

Constants

ALERT_TEXT
TMP_FILE_NAME

Attributes

edited_schema[RW]
unit_paths_file_path[RW]

Public Class Methods

new(before_schema_data, options) click to toggle source
Calls superclass method R2OAS::Base::new
# File lib/r2-oas/schema/editor.rb, line 26
def initialize(before_schema_data, options)
  super(options)
  @editor = swagger.editor
  @before_schema_data = before_schema_data
  @schema_doc_from_local = YAML.load_file(doc_save_file_path).to_yaml
end

Public Instance Methods

start() click to toggle source
# File lib/r2-oas/schema/editor.rb, line 33
def start
  EM.run do
    container.start
    open_browser_and_set_schema
    ensure_save_tmp_schema_file
    signal_trap('INT')
    signal_trap('TERM')
  end
end

Private Instance Methods

container() click to toggle source
# File lib/r2-oas/schema/editor.rb, line 133
def container
  @container ||= Docker::Container.create(
    'Image' => image,
    'ExposedPorts' => { exposed_port => {} },
    'HostConfig' => {
      'PortBindings' => {
        exposed_port => [{ 'HostPort' => port }]
      }
    }
  )
end
ensure_save_tmp_schema_file() click to toggle source

MEMO TargetRubyVersion is 2.7 and there is a warning Because it is necessary to support from ruby2.3 series where begin cannot be omitted rubocop:disable Style/RedundantBegin

# File lib/r2-oas/schema/editor.rb, line 79
def ensure_save_tmp_schema_file
  EM.add_periodic_timer(interval_to_save_edited_tmp_schema) do
    m = Mutex.new
    return nil unless @browser.exists?

    m.synchronize do
      begin
        save_after_fetch_local_strage
      rescue Selenium::WebDriver::Error::UnexpectedAlertOpenError
        alert = @browser.driver.switch_to.alert
        if alert.text.eql?(ALERT_TEXT)
          alert.accept && save_after_fetch_local_strage
        end
      end
    end
  end
end
fetch_edited_schema_from_browser() click to toggle source
# File lib/r2-oas/schema/editor.rb, line 104
def fetch_edited_schema_from_browser
  @after_schema_data = @browser.driver.local_storage[storage_key] if @browser.exists?
end
open_browser_and_set_schema() click to toggle source
# File lib/r2-oas/schema/editor.rb, line 112
def open_browser_and_set_schema
  capabilities = { 'chromeOptions' => { 'w3c' => false } }
  @browser ||= Watir::Browser.new(:chrome, capabilities)
  @browser.goto(url)
  if wait_for_loaded
    # MEMO:
    # Because it may not be updated
    # Make sure that the launched local storage is updated reliably
    Watir::Wait.until do
      old_storage = @browser.driver.local_storage[storage_key].dup
      @browser.driver.local_storage[storage_key] = new_storage = @schema_doc_from_local
      old_storage != new_storage
    end
    @browser.refresh
  end
end
process_after_close_browser() click to toggle source
# File lib/r2-oas/schema/editor.rb, line 65
def process_after_close_browser
  fetch_edited_schema_from_browser

  options = { type: :edited }
  save_edited_schema
  conv_after_schema_data = YAML.load(@after_schema_data)
  analyzer = Analyzer.new(@before_schema_data, conv_after_schema_data, options)
  analyzer.analyze_docs
end
save_after_fetch_local_strage() click to toggle source

rubocop:enable Style/RedundantBegin

# File lib/r2-oas/schema/editor.rb, line 98
def save_after_fetch_local_strage
  @after_schema_data = @browser.driver.local_storage[storage_key] || @after_schema_data
  save_edited_schema
  puts "\nwait for signal trap ..."
end
save_edited_schema() click to toggle source
# File lib/r2-oas/schema/editor.rb, line 108
def save_edited_schema
  File.write(doc_save_file_path, @after_schema_data)
end
signal_trap(command) click to toggle source
# File lib/r2-oas/schema/editor.rb, line 48
def signal_trap(command)
  Signal.trap(command) do
    if @browser.exists?
      process_after_close_browser
      container.stop
      container.remove
      logger.info "container id: #{container.id} removed"
    else
      process_after_close_browser
      container.remove
      logger.info "container id: #{container.id} removed"
    end

    EM.stop
  end
end
wait_for_loaded() click to toggle source
# File lib/r2-oas/schema/editor.rb, line 129
def wait_for_loaded
  Watir::Wait.until { @browser.body.present? }
end