Class: Openfoodfacts::Locale

Inherits:
String
  • Object
show all
Defined in:
lib/openfoodfacts/locale.rb

Constant Summary collapse

GLOBAL =
'world'

Class Method Summary collapse

Class Method Details

.all(domain: DEFAULT_DOMAIN) ⇒ Object

Get locales


10
11
12
13
14
15
16
17
18
# File 'lib/openfoodfacts/locale.rb', line 10

def all(domain: DEFAULT_DOMAIN)
  url = "http://#{domain}/"
  body = open(url).read
  dom = Nokogiri.parse(body)

  dom.css('ul li a').map { |locale_link|
    locale_from_link(locale_link.attr('href'))
  }.uniq.sort
end

Return locale from link


22
23
24
25
# File 'lib/openfoodfacts/locale.rb', line 22

def locale_from_link(link)
  locale = link[/^https?:\/\/([^.]+)\./i, 1]
  locale unless locale.nil? || locale == 'static'
end