Class: Openfoodfacts::Press
- Inherits:
-
Hashie::Mash
- Object
- Hashie::Mash
- Openfoodfacts::Press
- Defined in:
- lib/openfoodfacts/press.rb
Constant Summary collapse
- LOCALE_PATHS =
TODO: Add more locales
{ 'fr' => 'presse', 'uk' => 'press', 'us' => 'press', 'world' => 'press' }
- LOCALE_DATE_FORMATS =
{ 'fr' => '%d/%m/%Y', 'uk' => '%d/%m/%Y', 'us' => '%d/%m/%Y', 'world' => '%d/%m/%Y' }
Class Method Summary collapse
Class Method Details
.items(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/openfoodfacts/press.rb', line 25 def items(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN) if path = LOCALE_PATHS[locale] html = 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 |