123456789_123456789_123456789_123456789_123456789_

Class: Selenium::DevTools::Support::CDPClientGenerator

Relationships & Source Files
Inherits: Object
Defined in: rb/lib/selenium/devtools/support/cdp_client_generator.rb

Constant Summary

Instance Method Summary

Instance Method Details

#call(output_dir:, version:, **opts)

Raises:

  • (ArgumentError)
[ GitHub ]

  
# File 'rb/lib/selenium/devtools/support/cdp_client_generator.rb', line 35

def call(output_dir:, version:, **opts)
  @domain_template = ERB.new(File.read(DOMAIN_TEMPLATE_PATH))
  @loader_template = ERB.new(File.read(LOADER_TEMPLATE_PATH))
  @output_dir = output_dir
  @loader_path = opts.delete(:loader_path) || "#{@output_dir}.rb"
  @version = version

  browser_protocol_path = opts.delete(:browser_protocol_path) do
    File.expand_path('cdp/browser_protocol.json', DIR)
  end
  js_protocol_path = opts.delete(:js_protocol_path) { File.expand_path('cdp/js_protocol.json', DIR) }

  raise ArgumentError, "Invalid arguments: #{opts.keys}" unless opts.empty?

  browser_protocol = JSON.parse(File.read(browser_protocol_path), symbolize_names: true)
  js_protocol = JSON.parse(File.read(js_protocol_path), symbolize_names: true)

  FileUtils.mkdir_p(@output_dir)

  all_domains = browser_protocol[:domains] + js_protocol[:domains]
  all_domains.each { |domain| process_domain(domain) }
  process_loader(all_domains)
end

#kwargs(parameters)

[ GitHub ]

  
# File 'rb/lib/selenium/devtools/support/cdp_client_generator.rb', line 77

def kwargs(parameters)
  parameters = parameters.map do |parameter|
    if parameter[:optional]
      "#{snake_case(parameter[:name])}: nil"
    else
      "#{snake_case(parameter[:name])}:"
    end
  end
  parameters.join(', ')
end

#process_domain(domain)

[ GitHub ]

  
# File 'rb/lib/selenium/devtools/support/cdp_client_generator.rb', line 59

def process_domain(domain)
  result = @domain_template.result_with_hash(domain: domain, version: @version.upcase, h: self)
  filename = File.join(@output_dir, "#{snake_case(domain[:domain])}.rb")
  File.write(filename, remove_empty_lines(result))
end

#process_loader(domains)

[ GitHub ]

  
# File 'rb/lib/selenium/devtools/support/cdp_client_generator.rb', line 92

def process_loader(domains)
  result = @loader_template.result_with_hash(domains: domains, version: @version.upcase, h: self)
  File.write(@loader_path, remove_empty_lines(result))
end

#remove_empty_lines(string)

[ GitHub ]

  
# File 'rb/lib/selenium/devtools/support/cdp_client_generator.rb', line 88

def remove_empty_lines(string)
  string.split("\n").grep_v(/^\s+$/).join("\n")
end

#snake_case(string)

[ GitHub ]

  
# File 'rb/lib/selenium/devtools/support/cdp_client_generator.rb', line 65

def snake_case(string)
  name = string.gsub('JavaScript', 'Javascript')
               .gsub(/([A-Z]+)([A-Z][a-z]{2,})/, '\1_\2')
               .gsub(/([a-z\d])([A-Z])/, '\1_\2')
               .downcase
  # Certain CDP parameters conflict with Ruby keywords
  # so we prefix the name with underscore.
  name = "_#{name}" if RESERVED_KEYWORDS.include?(name)

  name
end