ehbrs-tools 0.11.0 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/lib/ehbrs/google.rb +9 -0
  3. data/lib/ehbrs/google/translate.rb +11 -0
  4. data/lib/ehbrs/google/translate/session.rb +61 -0
  5. data/lib/ehbrs/runner/google.rb +19 -0
  6. data/lib/ehbrs/runner/google/translate.rb +92 -0
  7. data/lib/ehbrs/tools/version.rb +1 -1
  8. data/vendor/aranha-selenium/Gemfile +5 -0
  9. data/vendor/aranha-selenium/aranha-selenium.gemspec +19 -0
  10. data/vendor/aranha-selenium/lib/aranha/selenium.rb +8 -0
  11. data/vendor/aranha-selenium/lib/aranha/selenium/driver_factory.rb +47 -0
  12. data/vendor/aranha-selenium/lib/aranha/selenium/driver_factory/base.rb +76 -0
  13. data/vendor/aranha-selenium/lib/aranha/selenium/driver_factory/chrome.rb +28 -0
  14. data/vendor/aranha-selenium/lib/aranha/selenium/driver_factory/firefox.rb +54 -0
  15. data/vendor/aranha-selenium/lib/aranha/selenium/driver_factory/firefox_auto_download_mime_types +685 -0
  16. data/vendor/aranha-selenium/lib/aranha/selenium/session.rb +78 -0
  17. data/vendor/aranha-selenium/lib/aranha/selenium/version.rb +7 -0
  18. data/vendor/aranha-selenium/spec/rubocop_check_spec.rb +7 -0
  19. data/vendor/aranha-selenium/spec/spec_helper.rb +100 -0
  20. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/common_concern.rb +68 -0
  21. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/configs.rb +4 -12
  22. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/console/configs.rb +1 -1
  23. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/console/docopt_runner/_class_methods.rb +1 -1
  24. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/console/speaker.rb +1 -1
  25. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/listable/list.rb +4 -0
  26. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/listable/lists.rb +10 -12
  27. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/listable/symbol_list.rb +19 -0
  28. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/module/common_concern.rb +10 -0
  29. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/module/patch.rb +1 -1
  30. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/module/require_sub.rb +2 -2
  31. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/patches/object/if_present.rb +1 -1
  32. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/paths_hash.rb +1 -3
  33. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/require_sub.rb +45 -2
  34. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/version.rb +1 -1
  35. data/vendor/eac_ruby_utils/lib/eac_ruby_utils/yaml.rb +85 -2
  36. data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/common_concern_spec.rb +42 -0
  37. data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/common_constructor_spec.rb +1 -0
  38. data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/console/docopt_runner_spec.rb +5 -5
  39. data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/console/speaker_spec.rb +1 -1
  40. data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/envs/executable_spec.rb +4 -0
  41. data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/fs/temp/temp_spec.rb +1 -1
  42. data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/listable_spec.rb +32 -0
  43. data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/options_consumer_spec.rb +1 -1
  44. data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/patches/hash/sym_keys_hash_spec.rb +1 -1
  45. data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/patches/object/if_present_spec.rb +1 -1
  46. data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/patches/object/template_spec.rb +1 -1
  47. data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/patches/pathname/basename_sub_spec.rb +4 -4
  48. data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/require_sub_spec.rb +19 -0
  49. data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/require_sub_spec/stubbed_module_a.rb +6 -0
  50. data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/require_sub_spec/stubbed_not_module.rb +6 -0
  51. data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/templates/searcher_spec.rb +1 -1
  52. data/vendor/eac_ruby_utils/spec/lib/eac_ruby_utils/yaml_spec.rb +75 -0
  53. data/vendor/eac_ruby_utils/spec/locales/pt-BR.yml +17 -0
  54. metadata +26 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6f3e4b8b8c20622926deed970635cf35570dc1171177cd99ee276a78a40d916e
4
- data.tar.gz: 62f890aa3e488464d788a1f7ea5a055ba99e22ca1e48427fdf6ff30643c4290c
3
+ metadata.gz: 01424b2d17105c7694679a4deb6dda7d0ab4b98f6ee0b32ed5d183bfdac33583
4
+ data.tar.gz: 271b08e6e6cf9a3868e06d1a13520030e6f1c55d167b86781f1250e92540dee7
5
5
  SHA512:
6
- metadata.gz: b5c33b1f44b74bd9b3a1b89739dcb4cb1b013ce99aadec47d0ccfc6fe6d430d265749141d4ddddf73d498771582586146652a00c9de19b2da5a8968b86da578e
7
- data.tar.gz: f8cd84b153932edb1a1ce1b20ca7df3dc707899a6d2d801b6d771d24ee3eb3cdc0a718ccaaf850f68ef86a9e132d50b296210c2dcd15eacc9b673fcc79300ce2
6
+ metadata.gz: bd53a0486dfec03c0d827ee8f759c5f28a490c93cb2320ea623f8a01ec5f9bdc106dd955f55114d098236b0418d96f62ab47cc2df8248b01da66bd87b3db52d8
7
+ data.tar.gz: 31dc82bd83f87cb8ea9ab4f2e8d2eac8ceba2477bf2d200156b3c01ada3794ddb673bb1d9581fc72ed9da97c3771393b701ae65f1c3c0447b9aeb344afb11d6a
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+
5
+ module Ehbrs
6
+ module Google
7
+ require_sub __FILE__
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+
5
+ module Ehbrs
6
+ module Google
7
+ module Translate
8
+ require_sub __FILE__
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'aranha/selenium/session'
4
+
5
+ module Ehbrs
6
+ module Google
7
+ module Translate
8
+ class Session
9
+ START_URL = 'https://translate.google.com.br/#view=home&op=docs&sl=en&tl=pt'
10
+ CLOSE_POPUP_CSS = '.dismiss-button'
11
+ FILE_INPUT_XPATH = '//*[@id = "tlid-file-input"]'
12
+ SUBMIT_XPATH = '//form[@action="//translate.googleusercontent.com/translate_f"]' \
13
+ '//input[@type="submit"]'
14
+ RESULT_XPATHS = [
15
+ '//script[contains(@src, "https://translate.google.com/translate_a")]',
16
+ '/html/body/pre'
17
+ ].freeze
18
+
19
+ attr_reader :sub
20
+
21
+ def initialize
22
+ @sub = ::Aranha::Selenium::Session.new
23
+ end
24
+
25
+ def translate(source_document_path)
26
+ go_to_upload_form
27
+ input_file(source_document_path)
28
+ close_changes_alert_popup
29
+ click_on_translate_button
30
+ wait_for_load_translation
31
+ sub.current_source
32
+ end
33
+
34
+ private
35
+
36
+ def go_to_upload_form
37
+ sub.navigate.to START_URL
38
+ sub.wait_for_element(xpath: FILE_INPUT_XPATH)
39
+ end
40
+
41
+ def input_file(source_document_path)
42
+ sub.find_element(xpath: FILE_INPUT_XPATH).send_keys(source_document_path)
43
+ end
44
+
45
+ def click_on_translate_button
46
+ sub.wait_for_click(xpath: SUBMIT_XPATH)
47
+ end
48
+
49
+ def close_changes_alert_popup
50
+ sub.wait_for_click(css: CLOSE_POPUP_CSS)
51
+ end
52
+
53
+ def wait_for_load_translation
54
+ sub.wait.until do
55
+ RESULT_XPATHS.any? { |xpath| sub.find_elements(xpath: xpath).size.positive? }
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_cli/default_runner'
4
+ require 'eac_ruby_utils/core_ext'
5
+ require 'eac_ruby_utils/console/docopt_runner'
6
+
7
+ module Ehbrs
8
+ class Runner < ::EacRubyUtils::Console::DocoptRunner
9
+ class Google < ::EacRubyUtils::Console::DocoptRunner
10
+ require_sub __FILE__
11
+ include ::EacCli::DefaultRunner
12
+
13
+ runner_definition do
14
+ desc 'Utilidades Google.'
15
+ subcommands
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_cli/default_runner'
4
+ require 'eac_ruby_utils/core_ext'
5
+ require 'eac_ruby_utils/console/docopt_runner'
6
+ require 'ehbrs/google/translate/session'
7
+
8
+ module Ehbrs
9
+ class Runner < ::EacRubyUtils::Console::DocoptRunner
10
+ class Google < ::EacRubyUtils::Console::DocoptRunner
11
+ class Translate < ::EacRubyUtils::Console::DocoptRunner
12
+ include ::EacCli::DefaultRunner
13
+
14
+ runner_definition do
15
+ desc 'Traduz um documento com Google Translate.'
16
+ arg_opt '-o', '--output-file', 'Salva saída em <output-file>.'
17
+ bool_opt '-w', '--overwrite', 'Permite sobreescrever arquivo de saída.'
18
+ pos_arg 'source-file'
19
+ end
20
+
21
+ def run
22
+ start_banner
23
+ validate
24
+ if output_to_stdout?
25
+ out(translated_content)
26
+ else
27
+ ::File.write(output_file, translated_content)
28
+ end
29
+ success 'Concluído'
30
+ end
31
+
32
+ def start_banner
33
+ infov 'Source file', source_file
34
+ infov 'Target file', output_file
35
+ end
36
+
37
+ def translated_content_uncached
38
+ session.translate(source_file)
39
+ end
40
+
41
+ def source_file
42
+ options.fetch('<source-file>')
43
+ end
44
+
45
+ def output_file
46
+ options.fetch('--output-file') || default_output_file
47
+ end
48
+
49
+ def default_output_file
50
+ ::File.join(
51
+ ::File.dirname(source_file),
52
+ ::File.basename(source_file, '.*') + '_translated.html'
53
+ )
54
+ end
55
+
56
+ def session_uncached
57
+ ::Ehbrs::Google::Translate::Session.new
58
+ end
59
+
60
+ def validate
61
+ validate_source_file
62
+ validate_output_file
63
+ validate_output_content
64
+ end
65
+
66
+ def validate_source_file
67
+ return if ::File.exist?(source_file)
68
+
69
+ fatal_error "Source file \"#{source_file}\" does not exist"
70
+ end
71
+
72
+ def validate_output_file
73
+ return if output_to_stdout?
74
+ return unless ::File.exist?(output_file)
75
+ return if options.fetch('--overwrite')
76
+
77
+ fatal_error "Output file \"#{output_file}\" already exists"
78
+ end
79
+
80
+ def validate_output_content
81
+ return if translated_content.present?
82
+
83
+ fatal_error 'Output content is empty'
84
+ end
85
+
86
+ def output_to_stdout?
87
+ output_file == '-'
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Ehbrs
4
4
  module Tools
5
- VERSION = '0.11.0'
5
+ VERSION = '0.12.0'
6
6
  end
7
7
  end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.push File.expand_path('lib', __dir__)
4
+
5
+ require 'aranha/selenium/version'
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = 'aranha-selenium'
9
+ s.version = ::Aranha::Selenium::VERSION
10
+ s.authors = ['Esquilo Azul Company']
11
+ s.summary = 'Selenium\' utilities for Ruby.'
12
+
13
+ s.files = Dir['{lib}/**/*', 'Gemfile']
14
+
15
+ s.add_dependency 'activesupport'
16
+ s.add_dependency 'selenium-webdriver', '~> 3.142', '>= 3.142.3'
17
+
18
+ s.add_development_dependency 'eac_ruby_gem_support', '~> 0.1', '>= 0.1.1'
19
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Aranha
4
+ module Selenium
5
+ require 'aranha/selenium/driver_factory'
6
+ require 'aranha/selenium/session'
7
+ end
8
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/hash/indifferent_access'
4
+ require 'aranha/selenium/driver_factory/base'
5
+ require 'aranha/selenium/driver_factory/chrome'
6
+ require 'aranha/selenium/driver_factory/firefox'
7
+
8
+ module Aranha
9
+ module Selenium
10
+ class DriverFactory
11
+ class << self
12
+ def create_driver(options = {})
13
+ new(options).create_driver
14
+ end
15
+ end
16
+
17
+ attr_reader :options
18
+
19
+ def initialize(options)
20
+ @options = options.with_indifferent_access.freeze
21
+ end
22
+
23
+ def create_driver
24
+ driver_class.new(driver_options).build
25
+ end
26
+
27
+ def driver_name
28
+ (options[:driver] || default_driver_name).to_s
29
+ end
30
+
31
+ def driver_class
32
+ Aranha::Selenium::DriverFactory.const_get(driver_name.classify)
33
+ rescue NameError
34
+ raise "Unknown Aranha Selenium driver: \"#{driver_name}\"" \
35
+ " (Class \"Aranha::Selenium::DriverFactory::#{driver_name.classify}\" not found)"
36
+ end
37
+
38
+ def default_driver_name
39
+ :firefox
40
+ end
41
+
42
+ def driver_options
43
+ options.except(:driver)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/json'
4
+ require 'active_support/core_ext/hash/indifferent_access'
5
+ require 'selenium-webdriver'
6
+ require 'tmpdir'
7
+
8
+ module Aranha
9
+ module Selenium
10
+ class DriverFactory
11
+ class Base
12
+ DEFAULT_DOWNLOADS_DIR = ::File.join(::Dir.tmpdir, 'aranha_downloads_dir')
13
+ DEFAULT_ACCEPT_INSECURE_CERTS = false
14
+ DEFAULT_HEADLESS = false
15
+ DEFAULT_USER_AGENT = nil
16
+
17
+ class << self
18
+ attr_writer :default_accept_insecure_certs, :default_downloads_dir, :default_headless,
19
+ :default_user_agent
20
+
21
+ def default_downloads_dir
22
+ @default_downloads_dir || DEFAULT_DOWNLOADS_DIR
23
+ end
24
+
25
+ def default_accept_insecure_certs
26
+ @default_accept_insecure_certs || DEFAULT_ACCEPT_INSECURE_CERTS
27
+ end
28
+
29
+ def default_headless
30
+ @default_headless || DEFAULT_HEADLESS
31
+ end
32
+
33
+ def default_user_agent
34
+ @default_user_agent || DEFAULT_USER_AGENT
35
+ end
36
+ end
37
+
38
+ attr_reader :options
39
+
40
+ def initialize(options)
41
+ @options = options.with_indifferent_access.freeze
42
+ end
43
+
44
+ def build
45
+ raise 'Must be overrided'
46
+ end
47
+
48
+ def downloads_dir
49
+ option_value(:downloads_dir)
50
+ end
51
+
52
+ def accept_insecure_certs?
53
+ option_value(:accept_insecure_certs)
54
+ end
55
+
56
+ def headless?
57
+ option_value(:headless)
58
+ end
59
+
60
+ def user_agent
61
+ option_value(:user_agent)
62
+ end
63
+
64
+ private
65
+
66
+ def option_value(key)
67
+ if options.key?(key)
68
+ options.fetch(key)
69
+ else
70
+ ::Aranha::Selenium::DriverFactory::Base.send("default_#{key}")
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'aranha/selenium/driver_factory/base'
4
+
5
+ module Aranha
6
+ module Selenium
7
+ class DriverFactory
8
+ class Chrome < ::Aranha::Selenium::DriverFactory::Base
9
+ def build
10
+ ::Selenium::WebDriver.for :chrome, options: chrome_options
11
+ end
12
+
13
+ private
14
+
15
+ def chrome_options
16
+ r = ::Selenium::WebDriver::Chrome::Options.new
17
+ r.add_argument('--ignore-certificate-errors') if accept_insecure_certs?
18
+ r.add_argument('--headless') if headless?
19
+ r.add_argument('--disable-popup-blocking')
20
+ r.add_argument('--disable-translate')
21
+ r.add_argument("user-agent=#{user_agent}") if user_agent.present?
22
+ r.add_preference(:download, prompt_for_download: false, default_directory: downloads_dir)
23
+ r
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'aranha/selenium/driver_factory/base'
4
+
5
+ module Aranha
6
+ module Selenium
7
+ class DriverFactory
8
+ class Firefox < ::Aranha::Selenium::DriverFactory::Base
9
+ def build
10
+ ::Selenium::WebDriver.for(
11
+ :firefox,
12
+ options: ::Selenium::WebDriver::Firefox::Options.new(
13
+ profile: build_profile,
14
+ args: build_args
15
+ ),
16
+ desired_capabilities: build_capabilities
17
+ )
18
+ end
19
+
20
+ private
21
+
22
+ def build_args
23
+ r = []
24
+ r << '-headless' if headless?
25
+ r
26
+ end
27
+
28
+ def build_capabilities
29
+ if accept_insecure_certs?
30
+ ::Selenium::WebDriver::Remote::Capabilities.firefox(accept_insecure_certs: true)
31
+ else
32
+ ::Selenium::WebDriver::Remote::Capabilities.firefox
33
+ end
34
+ end
35
+
36
+ def build_profile
37
+ r = ::Selenium::WebDriver::Firefox::Profile.new
38
+ r['browser.download.dir'] = downloads_dir
39
+ r['browser.download.folderList'] = 2
40
+ r['browser.helperApps.neverAsk.saveToDisk'] = auto_download_mime_types.join(';')
41
+ r['pdfjs.disabled'] = true
42
+ r['general.useragent.override'] = user_agent if user_agent.present?
43
+ r
44
+ end
45
+
46
+ def auto_download_mime_types
47
+ ::File.read(
48
+ ::File.join(__dir__, 'firefox_auto_download_mime_types')
49
+ ).each_line.map(&:strip)
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end