Class: AnkiTranslator::References::GoogleTranslate

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeGoogleTranslate

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

#nameObject (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

#sessionObject

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