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



12
13
14
15
16
17
18
19
20
21
# File 'lib/openfoodfacts/locale.rb', line 12

def all(domain: DEFAULT_DOMAIN)
  path = 'cgi/i18n/countries.pl?_type=query'
  url = "https://#{GLOBAL}.#{domain}/#{path}"
  json = URI.open(url).read
  hash = JSON.parse(json)

  hash.map { |pair|
    locale_from_pair(pair, domain: domain)
  }.compact
end

Return locale from link



25
26
27
28
# File 'lib/openfoodfacts/locale.rb', line 25

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

.locale_from_pair(pair, domain: DEFAULT_DOMAIN) ⇒ Object

Return locale from pair



32
33
34
35
36
37
38
39
# File 'lib/openfoodfacts/locale.rb', line 32

def locale_from_pair(pair, domain: DEFAULT_DOMAIN)
  code = pair.first
  {
    "name" => pair.last.strip,
    "code" => code,
    "url" => "https://#{code}.#{domain}"
  } if code
end