Class: Openfoodfacts::Press

Inherits:
Hashie::Mash
  • Object
show all
Defined in:
lib/openfoodfacts/press.rb

Constant Summary collapse

LOCALE_PATHS =

TODO: Add more locales

{
  'fr' => 'revue-de-presse-fr'
}
LOCALE_DATE_FORMATS =
{
  'fr' => '%d/%m/%Y'
}

Class Method Summary collapse

Class Method Details

.items(locale: 'fr', domain: DEFAULT_DOMAIN) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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