Class: Openbeautyfacts::Additive

Inherits:
Hashie::Mash
  • Object
show all
Defined in:
lib/openbeautyfacts/additive.rb

Constant Summary collapse

LOCALE_PATHS =

TODO: Add more locales

{
  'fr' => 'additifs',
  'uk' => 'additives',
  'us' => 'additives',
  'world' => 'additives'
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN) ⇒ Object

Get additives


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/openbeautyfacts/additive.rb', line 18

def all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
  if path = LOCALE_PATHS[locale]
    page_url = "http://#{locale}.#{domain}/#{path}"

    Product.tags_from_page(self, page_url) do |tag|
      columns = tag.css('td')

      link = tag.css('a').first
      attributes = {
        "name" => link.text.strip,
        "url" => URI.join(page_url, link.attr('href')).to_s,
        "products_count" => columns[1].text.to_i,
      }

      riskiness = columns[3].attr('class')
      if riskiness
        attributes["riskiness"] = riskiness[/level_(\d+)/, 1].to_i
      end

      new(attributes)
    end
  end
end

Instance Method Details

#products(page: -1)) ⇒ Object

Get products with additive


46
47
48
# File 'lib/openbeautyfacts/additive.rb', line 46

def products(page: -1)
  Product.from_website_page(url, page: page, products_count: products_count) if url
end