Module: Iev

Defined in:
lib/iev.rb,
lib/iev/db.rb,
lib/iev/version.rb,
lib/iev/db_cache.rb

Defined Under Namespace

Classes: Db, DbCache

Constant Summary collapse

VERSION =
"0.3.0".freeze

Class Method Summary collapse

Class Method Details

.get(code, lang) ⇒ String?

Scrape Electropedia for term.

if code not found then empty string,

if language not found then nil.

Parameters:

  • code (String)

    for example β€œ103-01-02”

  • lang (String)

    language code, for examle β€œen”

Returns:

  • (String, nil)

    if found than term,



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/iev.rb', line 17

def self.get(code, lang)
  url = "http://www.electropedia.org/iev/iev.nsf/"\
        "display?openform&ievref=#{code}"
  doc = Nokogiri::HTML OpenURI.open_uri(url), nil, "UTF-8"
  xpath = "//table/tr/td/div/font[.=\"#{lang}\"]/../../"\
          "following-sibling::td[2]"
  a = doc&.at(xpath)&.children&.to_xml
  a&.sub(%r{<br/>.*$}, "")
    &.sub(%r{, &lt;.*$}, "")
    &.gsub(%r{<[^>]*>}, "")&.strip
end