Class: AnkiTranslator::References::GoogleTranslate
- Inherits:
-
Object
- Object
- AnkiTranslator::References::GoogleTranslate
- Defined in:
- lib/vocab_to_anki/references/google_translate.rb
Constant Summary collapse
- URL =
"https://translate.google.com/"
- TARGET_LANGUAGE =
"pt"
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#session ⇒ Object
readonly
Returns the value of attribute session.
Instance Method Summary collapse
- #fetch_definitions(term) ⇒ Object
-
#initialize ⇒ GoogleTranslate
constructor
A new instance of GoogleTranslate.
Constructor Details
#initialize ⇒ GoogleTranslate
Returns a new instance of GoogleTranslate.
11 12 13 14 |
# File 'lib/vocab_to_anki/references/google_translate.rb', line 11 def initialize @name = :google_translate @session = new_session end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/vocab_to_anki/references/google_translate.rb', line 9 def name @name end |
#session ⇒ Object
Returns the value of attribute session.
9 10 11 |
# File 'lib/vocab_to_anki/references/google_translate.rb', line 9 def session @session end |
Instance Method Details
#fetch_definitions(term) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/vocab_to_anki/references/google_translate.rb', line 16 def fetch_definitions(term) search(term) return unless session.has_selector?("h3", text: "Definitions of ") definitions = session.find("h3", text: "Definitions of ")&.all(:xpath, ".//..")&.first&.all("div[lang=en]") a = [] a.push parse_definitions(definitions) a.push translations rescue Selenium::WebDriver::Error::StaleElementReferenceError new_session nil end |