class Openbeautyfacts::Press

Constants

LOCALE_DATE_FORMATS
LOCALE_PATHS

TODO: Add more locales

Public Class Methods

items(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN) click to toggle source
# File lib/openbeautyfacts/press.rb, line 25
def items(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
  if path = LOCALE_PATHS[locale]
    html = URI.open("https://#{locale}.#{domain}/#{path}").read
    dom = Nokogiri::HTML.fragment(html)

    titles = dom.css('#main_column li')
    titles.each_with_index.map do |item, index|
      data = item.inner_html.split(' - ')

      link = Nokogiri::HTML.fragment(data.first).css('a')
      attributes = {
        "title" => link.text.strip,
        "url" => link.attr('href').value
      }

      last = Nokogiri::HTML.fragment(data.last)
      if date_format = LOCALE_DATE_FORMATS[locale] and date = last.text.strip[/\d+\/\d+\/\d+\z/, 0]
        attributes["date"] = DateTime.strptime(date, date_format)
      end

      if data.length >= 3
        attributes["source"] = Nokogiri::HTML.fragment(data[-2]).text.strip
      end

      new(attributes)
    end
  end
end