Class: Selenium::WebDriver::Support::CDPClientGenerator
- Inherits:
-
Object
- Object
- Selenium::WebDriver::Support::CDPClientGenerator
- Defined in:
- lib/selenium/webdriver/support/cdp_client_generator.rb
Constant Summary collapse
- TEMPLATE_PATH =
Input JSON files are generated from PDL tasks.
File.('cdp/domain.rb.erb', __dir__)
- RESERVED_KEYWORDS =
%w[end].freeze
Instance Method Summary collapse
- #call(output_dir:, version:, browser_protocol_path: nil, js_protocol_path: nil, loader_path: nil) ⇒ Object
- #kwargs(parameters) ⇒ Object
- #process_domain(domain) ⇒ Object
- #remove_empty_lines(string) ⇒ Object
- #require_file ⇒ Object
- #snake_case(string) ⇒ Object
Instance Method Details
#call(output_dir:, version:, browser_protocol_path: nil, js_protocol_path: nil, loader_path: nil) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/selenium/webdriver/support/cdp_client_generator.rb', line 33 def call(output_dir:, version:, browser_protocol_path: nil, js_protocol_path: nil, loader_path: nil, **) @template = ERB.new(File.read(TEMPLATE_PATH)) @output_dir = output_dir @loader_path = loader_path || "#{@output_dir}.rb" @version = version browser_protocol_path ||= File.('cdp/browser_protocol.json', __dir__) js_protocol_path ||= File.('cdp/js_protocol.json', __dir__) 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) browser_protocol[:domains].each { |domain| process_domain(domain) } js_protocol[:domains].each { |domain| process_domain(domain) } require_file end |
#kwargs(parameters) ⇒ Object
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/selenium/webdriver/support/cdp_client_generator.rb', line 70 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) ⇒ Object
52 53 54 55 56 |
# File 'lib/selenium/webdriver/support/cdp_client_generator.rb', line 52 def process_domain(domain) result = @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 |
#remove_empty_lines(string) ⇒ Object
81 82 83 |
# File 'lib/selenium/webdriver/support/cdp_client_generator.rb', line 81 def remove_empty_lines(string) string.split("\n").grep_v(/^\s+$/).join("\n") end |
#require_file ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/selenium/webdriver/support/cdp_client_generator.rb', line 85 def require_file # rubocop:disable Lint/InterpolationCheck dynamic_location = '#{File.dirname(File.absolute_path(__FILE__))}' # rubocop:enable Lint/InterpolationCheck require_all = "Dir.glob(\"#{dynamic_location}/#{@version}/*\", &method(:require))" File.write(@loader_path, require_all) end |
#snake_case(string) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/selenium/webdriver/support/cdp_client_generator.rb', line 58 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 |