class Openfoodfacts::Press

Constants

LOCALE_DATE_FORMATS
LOCALE_PATHS

TODO: Add more locales

Public Class Methods

items(locale: 'fr', domain: DEFAULT_DOMAIN) click to toggle source
# File lib/openfoodfacts/press.rb, line 19
def items(locale: 'fr', domain: DEFAULT_DOMAIN)
  if path = LOCALE_PATHS[locale]
    date_format = LOCALE_DATE_FORMATS[locale]

    html = URI.open("https://#{locale}.#{domain}/#{path}").read
    dom = Nokogiri::HTML.fragment(html)

    titles = dom.css('#press_table tbody tr')
    titles.each_with_index.map do |item, index|
      colums = item.css('td')

      link = colums[1].css('a')
      attributes = {
        "type" => colums[0].text,
        "title" => colums[1].text.strip,
        "url" => link && link.attr('href') && link.attr('href').value,
        "source" => colums[2].text.strip,
        "date" => (DateTime.strptime(colums[3].text, date_format) rescue nil)
      }

      new(attributes)
    end
  end
end