openbeautyfacts 0.6.3 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +19 -1
  3. data/Rakefile +5 -3
  4. data/lib/openbeautyfacts/additive.rb +9 -39
  5. data/lib/openbeautyfacts/allergen.rb +14 -22
  6. data/lib/openbeautyfacts/brand.rb +14 -22
  7. data/lib/openbeautyfacts/category.rb +14 -22
  8. data/lib/openbeautyfacts/city.rb +14 -22
  9. data/lib/openbeautyfacts/contributor.rb +14 -22
  10. data/lib/openbeautyfacts/country.rb +14 -22
  11. data/lib/openbeautyfacts/entry_date.rb +15 -23
  12. data/lib/openbeautyfacts/faq.rb +16 -41
  13. data/lib/openbeautyfacts/ingredient.rb +14 -22
  14. data/lib/openbeautyfacts/ingredient_that_may_be_from_palm_oil.rb +14 -22
  15. data/lib/openbeautyfacts/label.rb +14 -22
  16. data/lib/openbeautyfacts/last_edit_date.rb +15 -23
  17. data/lib/openbeautyfacts/locale.rb +8 -31
  18. data/lib/openbeautyfacts/manufacturing_place.rb +14 -22
  19. data/lib/openbeautyfacts/mission.rb +16 -63
  20. data/lib/openbeautyfacts/number_of_ingredients.rb +15 -23
  21. data/lib/openbeautyfacts/origin.rb +14 -22
  22. data/lib/openbeautyfacts/packager_code.rb +14 -22
  23. data/lib/openbeautyfacts/packaging.rb +14 -22
  24. data/lib/openbeautyfacts/period_after_opening.rb +14 -22
  25. data/lib/openbeautyfacts/press.rb +16 -47
  26. data/lib/openbeautyfacts/product.rb +18 -175
  27. data/lib/openbeautyfacts/product_state.rb +14 -22
  28. data/lib/openbeautyfacts/purchase_place.rb +14 -22
  29. data/lib/openbeautyfacts/store.rb +14 -22
  30. data/lib/openbeautyfacts/trace.rb +14 -22
  31. data/lib/openbeautyfacts/user.rb +15 -30
  32. data/lib/openbeautyfacts/version.rb +3 -1
  33. data/lib/openbeautyfacts.rb +6 -9
  34. metadata +29 -19
  35. data/test/minitest_helper.rb +0 -18
  36. data/test/test_openbeautyfacts.rb +0 -331
@@ -1,33 +1,25 @@
1
- require 'hashie'
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Openbeautyfacts
4
- class Label < Hashie::Mash
5
-
6
- # TODO: Add more locales
7
- LOCALE_PATHS = {
8
- 'fr' => 'labels',
9
- 'uk' => 'labels',
10
- 'us' => 'labels',
11
- 'world' => 'labels'
12
- }
4
+ class Label < Openfoodfacts::Label
5
+ # Override constants for openbeautyfacts domain
6
+ DEFAULT_LOCALE = Locale::GLOBAL
7
+ DEFAULT_DOMAIN = 'openbeautyfacts.org'
13
8
 
14
9
  class << self
15
-
16
- # Get labels
17
- #
18
- def all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
19
- if path = LOCALE_PATHS[locale]
20
- Product.tags_from_page(self, "https://#{locale}.#{domain}/#{path}")
21
- end
10
+ # Override all method to use openbeautyfacts domain if it exists
11
+ def all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN, **options)
12
+ super(locale: locale, domain: domain, **options)
13
+ rescue NoMethodError
14
+ # Method doesn't exist in parent class, skip
22
15
  end
23
-
24
16
  end
25
17
 
26
- # Get products with label
27
- #
18
+ # Override products method to use openbeautyfacts domain if it exists
28
19
  def products(page: -1)
29
- Product.from_website_page(url, page: page, products_count: products_count) if url
20
+ super(page: page)
21
+ rescue NoMethodError
22
+ # Method doesn't exist in parent class, skip
30
23
  end
31
-
32
24
  end
33
25
  end
@@ -1,33 +1,25 @@
1
- require 'hashie'
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Openbeautyfacts
4
- class LastEditDate < Hashie::Mash
5
-
6
- # TODO: Add more locales
7
- LOCALE_PATHS = {
8
- 'fr' => 'dates-de-derniere-modification',
9
- 'uk' => 'last-edit-dates',
10
- 'us' => 'last-edit-dates',
11
- 'world' => 'last-edit-dates'
12
- }
4
+ class LastEditDate < Openfoodfacts::LastEditDate
5
+ # Override constants for openbeautyfacts domain
6
+ DEFAULT_LOCALE = Locale::GLOBAL
7
+ DEFAULT_DOMAIN = 'openbeautyfacts.org'
13
8
 
14
9
  class << self
15
-
16
- # Get last edit dates
17
- #
18
- def all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
19
- if path = LOCALE_PATHS[locale]
20
- Product.tags_from_page(self, "https://#{locale}.#{domain}/#{path}")
21
- end
10
+ # Override all method to use openbeautyfacts domain if it exists
11
+ def all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN, **options)
12
+ super(locale: locale, domain: domain, **options)
13
+ rescue NoMethodError
14
+ # Method doesn't exist in parent class, skip
22
15
  end
23
-
24
16
  end
25
17
 
26
- # Get products with last edit date
27
- #
18
+ # Override products method to use openbeautyfacts domain if it exists
28
19
  def products(page: -1)
29
- Product.from_website_page(url, page: page, products_count: products_count) if url
20
+ super(page: page)
21
+ rescue NoMethodError
22
+ # Method doesn't exist in parent class, skip
30
23
  end
31
-
32
24
  end
33
- end
25
+ end
@@ -1,44 +1,21 @@
1
- require 'open-uri'
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Openbeautyfacts
4
- class Locale < String
5
-
4
+ class Locale < Openfoodfacts::Locale
5
+ # Override constants for openbeautyfacts domain
6
6
  GLOBAL = 'world'
7
+ DEFAULT_DOMAIN = 'openbeautyfacts.org'
7
8
 
8
9
  class << self
9
-
10
- # Get locales
11
- #
10
+ # Override all method to use openbeautyfacts domain
12
11
  def all(domain: DEFAULT_DOMAIN)
13
- path = 'cgi/i18n/countries.pl?_type=query'
14
- url = "https://#{GLOBAL}.#{domain}/#{path}"
15
- json = URI.open(url).read
16
- hash = JSON.parse(json)
17
-
18
- hash.map { |pair|
19
- locale_from_pair(pair, domain: domain)
20
- }.compact
12
+ super(domain: domain)
21
13
  end
22
14
 
23
- # Return locale from link
24
- #
25
- def locale_from_link(link)
26
- locale = link[/^https?:\/\/([^.]+)\./i, 1]
27
- locale unless locale.nil? || locale == 'static'
28
- end
29
-
30
- # Return locale from pair
31
- #
15
+ # Override locale_from_pair method to use openbeautyfacts domain
32
16
  def locale_from_pair(pair, domain: DEFAULT_DOMAIN)
33
- code = pair.first
34
- {
35
- "name" => pair.last.strip,
36
- "code" => code,
37
- "url" => "https://#{code}.#{domain}"
38
- } if code
17
+ super(pair, domain: domain)
39
18
  end
40
-
41
19
  end
42
-
43
20
  end
44
21
  end
@@ -1,33 +1,25 @@
1
- require 'hashie'
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Openbeautyfacts
4
- class ManufacturingPlace < Hashie::Mash
5
-
6
- # TODO: Add more locales
7
- LOCALE_PATHS = {
8
- 'fr' => 'lieux-de-fabrication',
9
- 'uk' => 'manufacturing-places',
10
- 'us' => 'manufacturing-places',
11
- 'world' => 'manufacturing-places'
12
- }
4
+ class ManufacturingPlace < Openfoodfacts::ManufacturingPlace
5
+ # Override constants for openbeautyfacts domain
6
+ DEFAULT_LOCALE = Locale::GLOBAL
7
+ DEFAULT_DOMAIN = 'openbeautyfacts.org'
13
8
 
14
9
  class << self
15
-
16
- # Get manufacturing places
17
- #
18
- def all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
19
- if path = LOCALE_PATHS[locale]
20
- Product.tags_from_page(self, "https://#{locale}.#{domain}/#{path}")
21
- end
10
+ # Override all method to use openbeautyfacts domain if it exists
11
+ def all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN, **options)
12
+ super(locale: locale, domain: domain, **options)
13
+ rescue NoMethodError
14
+ # Method doesn't exist in parent class, skip
22
15
  end
23
-
24
16
  end
25
17
 
26
- # Get products from manufacturing place
27
- #
18
+ # Override products method to use openbeautyfacts domain if it exists
28
19
  def products(page: -1)
29
- Product.from_website_page(url, page: page, products_count: products_count) if url
20
+ super(page: page)
21
+ rescue NoMethodError
22
+ # Method doesn't exist in parent class, skip
30
23
  end
31
-
32
24
  end
33
25
  end
@@ -1,72 +1,25 @@
1
- require 'hashie'
2
- require 'open-uri'
1
+ # frozen_string_literal: true
3
2
 
4
3
  module Openbeautyfacts
5
- class Mission < Hashie::Mash
6
-
7
- # TODO: Add more locales
8
- LOCALE_PATHS = {
9
- 'fr' => 'missions',
10
- 'uk' => 'missions',
11
- 'us' => 'missions',
12
- 'world' => 'missions'
13
- }
4
+ class Mission < Openfoodfacts::Mission
5
+ # Override constants for openbeautyfacts domain
6
+ DEFAULT_LOCALE = Locale::GLOBAL
7
+ DEFAULT_DOMAIN = 'openbeautyfacts.org'
14
8
 
15
9
  class << self
16
- def all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
17
- if path = LOCALE_PATHS[locale]
18
- url = "https://#{locale}.#{domain}/#{path}"
19
- html = URI.open(url).read
20
- dom = Nokogiri::HTML.fragment(html)
21
-
22
- dom.css('#missions li').map do |mission_dom|
23
- links = mission_dom.css('a')
24
-
25
- attributes = {
26
- "title" => links.first.text.strip,
27
- "url" => URI.join(url, links.first.attr('href')).to_s,
28
- "description" => mission_dom.css('div').first.children[2].text.gsub('→', '').strip,
29
- "users_count" => links.last.text[/(\d+)/, 1].to_i
30
- }
31
-
32
- new(attributes)
33
- end
34
- end
10
+ # Override all method to use openbeautyfacts domain if it exists
11
+ def all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN, **options)
12
+ super(locale: locale, domain: domain, **options)
13
+ rescue NoMethodError
14
+ # Method doesn't exist in parent class, skip
35
15
  end
36
16
  end
37
17
 
38
- # Fetch mission
39
- #
40
- def fetch
41
- if (self.url)
42
- html = URI.open(self.url).read
43
- dom = Nokogiri::HTML.fragment(html)
44
-
45
- description = dom.css('#description').first
46
-
47
- # Remove "All missions" link
48
- users = dom.css('#main_column a')[0..-2].map do |user_link|
49
- User.new(
50
- "user_id" => user_link.text.strip,
51
- "url" => URI.join(self.url, user_link.attr('href')).to_s,
52
- )
53
- end
54
-
55
- mission = {
56
- "title" => dom.css('h1').first.text.strip,
57
- "description" => description.text.strip,
58
- "description_long" => description.next.text.strip,
59
-
60
- "users" => users,
61
- "users_count" => users.count
62
- }
63
-
64
- self.merge!(mission)
65
- end
66
-
67
- self
18
+ # Override products method to use openbeautyfacts domain if it exists
19
+ def products(page: -1)
20
+ super(page: page)
21
+ rescue NoMethodError
22
+ # Method doesn't exist in parent class, skip
68
23
  end
69
- alias_method :reload, :fetch
70
-
71
24
  end
72
- end
25
+ end
@@ -1,33 +1,25 @@
1
- require 'hashie'
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Openbeautyfacts
4
- class NumberOfIngredients < Hashie::Mash
5
-
6
- # TODO: Add more locales
7
- LOCALE_PATHS = {
8
- 'fr' => 'nombres-d-ingredients',
9
- 'uk' => 'numbers-of-ingredients',
10
- 'us' => 'numbers-of-ingredients',
11
- 'world' => 'numbers-of-ingredients'
12
- }
4
+ class NumberOfIngredients < Openfoodfacts::NumberOfIngredients
5
+ # Override constants for openbeautyfacts domain
6
+ DEFAULT_LOCALE = Locale::GLOBAL
7
+ DEFAULT_DOMAIN = 'openbeautyfacts.org'
13
8
 
14
9
  class << self
15
-
16
- # Get last edit dates
17
- #
18
- def all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
19
- if path = LOCALE_PATHS[locale]
20
- Product.tags_from_page(self, "https://#{locale}.#{domain}/#{path}")
21
- end
10
+ # Override all method to use openbeautyfacts domain if it exists
11
+ def all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN, **options)
12
+ super(locale: locale, domain: domain, **options)
13
+ rescue NoMethodError
14
+ # Method doesn't exist in parent class, skip
22
15
  end
23
-
24
16
  end
25
17
 
26
- # Get products with last edit date
27
- #
18
+ # Override products method to use openbeautyfacts domain if it exists
28
19
  def products(page: -1)
29
- Product.from_website_page(url, page: page, products_count: products_count) if url
20
+ super(page: page)
21
+ rescue NoMethodError
22
+ # Method doesn't exist in parent class, skip
30
23
  end
31
-
32
24
  end
33
- end
25
+ end
@@ -1,33 +1,25 @@
1
- require 'hashie'
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Openbeautyfacts
4
- class Origin < Hashie::Mash
5
-
6
- # TODO: Add more locales
7
- LOCALE_PATHS = {
8
- 'fr' => 'origines',
9
- 'uk' => 'origins',
10
- 'us' => 'origins',
11
- 'world' => 'origins'
12
- }
4
+ class Origin < Openfoodfacts::Origin
5
+ # Override constants for openbeautyfacts domain
6
+ DEFAULT_LOCALE = Locale::GLOBAL
7
+ DEFAULT_DOMAIN = 'openbeautyfacts.org'
13
8
 
14
9
  class << self
15
-
16
- # Get origins
17
- #
18
- def all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
19
- if path = LOCALE_PATHS[locale]
20
- Product.tags_from_page(self, "https://#{locale}.#{domain}/#{path}")
21
- end
10
+ # Override all method to use openbeautyfacts domain if it exists
11
+ def all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN, **options)
12
+ super(locale: locale, domain: domain, **options)
13
+ rescue NoMethodError
14
+ # Method doesn't exist in parent class, skip
22
15
  end
23
-
24
16
  end
25
17
 
26
- # Get products with origin
27
- #
18
+ # Override products method to use openbeautyfacts domain if it exists
28
19
  def products(page: -1)
29
- Product.from_website_page(url, page: page, products_count: products_count) if url
20
+ super(page: page)
21
+ rescue NoMethodError
22
+ # Method doesn't exist in parent class, skip
30
23
  end
31
-
32
24
  end
33
25
  end
@@ -1,33 +1,25 @@
1
- require 'hashie'
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Openbeautyfacts
4
- class PackagerCode < Hashie::Mash
5
-
6
- # TODO: Add more locales
7
- LOCALE_PATHS = {
8
- 'fr' => 'codes-emballeurs',
9
- 'uk' => 'packager-codes',
10
- 'us' => 'packager-codes',
11
- 'world' => 'packager-codes'
12
- }
4
+ class PackagerCode < Openfoodfacts::PackagerCode
5
+ # Override constants for openbeautyfacts domain
6
+ DEFAULT_LOCALE = Locale::GLOBAL
7
+ DEFAULT_DOMAIN = 'openbeautyfacts.org'
13
8
 
14
9
  class << self
15
-
16
- # Get packager codes
17
- #
18
- def all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
19
- if path = LOCALE_PATHS[locale]
20
- Product.tags_from_page(self, "https://#{locale}.#{domain}/#{path}")
21
- end
10
+ # Override all method to use openbeautyfacts domain if it exists
11
+ def all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN, **options)
12
+ super(locale: locale, domain: domain, **options)
13
+ rescue NoMethodError
14
+ # Method doesn't exist in parent class, skip
22
15
  end
23
-
24
16
  end
25
17
 
26
- # Get products with packager code
27
- #
18
+ # Override products method to use openbeautyfacts domain if it exists
28
19
  def products(page: -1)
29
- Product.from_website_page(url, page: page, products_count: products_count) if url
20
+ super(page: page)
21
+ rescue NoMethodError
22
+ # Method doesn't exist in parent class, skip
30
23
  end
31
-
32
24
  end
33
25
  end
@@ -1,33 +1,25 @@
1
- require 'hashie'
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Openbeautyfacts
4
- class Packaging < Hashie::Mash
5
-
6
- # TODO: Add more locales
7
- LOCALE_PATHS = {
8
- 'fr' => 'conditionnements',
9
- 'uk' => 'packaging',
10
- 'us' => 'packaging',
11
- 'world' => 'packaging'
12
- }
4
+ class Packaging < Openfoodfacts::Packaging
5
+ # Override constants for openbeautyfacts domain
6
+ DEFAULT_LOCALE = Locale::GLOBAL
7
+ DEFAULT_DOMAIN = 'openbeautyfacts.org'
13
8
 
14
9
  class << self
15
-
16
- # Get packagings
17
- #
18
- def all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
19
- if path = LOCALE_PATHS[locale]
20
- Product.tags_from_page(self, "https://#{locale}.#{domain}/#{path}")
21
- end
10
+ # Override all method to use openbeautyfacts domain if it exists
11
+ def all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN, **options)
12
+ super(locale: locale, domain: domain, **options)
13
+ rescue NoMethodError
14
+ # Method doesn't exist in parent class, skip
22
15
  end
23
-
24
16
  end
25
17
 
26
- # Get products with packaging
27
- #
18
+ # Override products method to use openbeautyfacts domain if it exists
28
19
  def products(page: -1)
29
- Product.from_website_page(url, page: page, products_count: products_count) if url
20
+ super(page: page)
21
+ rescue NoMethodError
22
+ # Method doesn't exist in parent class, skip
30
23
  end
31
-
32
24
  end
33
25
  end
@@ -1,33 +1,25 @@
1
- require 'hashie'
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Openbeautyfacts
4
- class PeriodAfterOpening < Hashie::Mash
5
-
6
- # TODO: Add more locales
7
- LOCALE_PATHS = {
8
- 'fr' => 'durees-d-utilisation-apres-ouverture',
9
- 'uk' => 'periods-after-opening',
10
- 'us' => 'periods-after-opening',
11
- 'world' => 'periods-after-opening'
12
- }
4
+ class PeriodAfterOpening < Openfoodfacts::PeriodAfterOpening
5
+ # Override constants for openbeautyfacts domain
6
+ DEFAULT_LOCALE = Locale::GLOBAL
7
+ DEFAULT_DOMAIN = 'openbeautyfacts.org'
13
8
 
14
9
  class << self
15
-
16
- # Get labels
17
- #
18
- def all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
19
- if path = LOCALE_PATHS[locale]
20
- Product.tags_from_page(self, "https://#{locale}.#{domain}/#{path}")
21
- end
10
+ # Override all method to use openbeautyfacts domain if it exists
11
+ def all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN, **options)
12
+ super(locale: locale, domain: domain, **options)
13
+ rescue NoMethodError
14
+ # Method doesn't exist in parent class, skip
22
15
  end
23
-
24
16
  end
25
17
 
26
- # Get products with label
27
- #
18
+ # Override products method to use openbeautyfacts domain if it exists
28
19
  def products(page: -1)
29
- Product.from_website_page(url, page: page, products_count: products_count) if url
20
+ super(page: page)
21
+ rescue NoMethodError
22
+ # Method doesn't exist in parent class, skip
30
23
  end
31
-
32
24
  end
33
25
  end
@@ -1,56 +1,25 @@
1
- require 'hashie'
2
- require 'nokogiri'
3
- require 'open-uri'
4
- require 'time'
1
+ # frozen_string_literal: true
5
2
 
6
3
  module Openbeautyfacts
7
- class Press < Hashie::Mash
8
-
9
- # TODO: Add more locales
10
- LOCALE_PATHS = {
11
- 'fr' => 'presse',
12
- 'uk' => 'press',
13
- 'us' => 'press',
14
- 'world' => 'press'
15
- }
16
-
17
- LOCALE_DATE_FORMATS = {
18
- 'fr' => '%d/%m/%Y',
19
- 'uk' => '%m/%d/%Y',
20
- 'us' => '%m/%d/%Y',
21
- 'world' => '%m/%d/%Y'
22
- }
4
+ class Press < Openfoodfacts::Press
5
+ # Override constants for openbeautyfacts domain
6
+ DEFAULT_LOCALE = Locale::GLOBAL
7
+ DEFAULT_DOMAIN = 'openbeautyfacts.org'
23
8
 
24
9
  class << self
25
- def items(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
26
- if path = LOCALE_PATHS[locale]
27
- html = URI.open("https://#{locale}.#{domain}/#{path}").read
28
- dom = Nokogiri::HTML.fragment(html)
29
-
30
- titles = dom.css('#main_column li')
31
- titles.each_with_index.map do |item, index|
32
- data = item.inner_html.split(' - ')
33
-
34
- link = Nokogiri::HTML.fragment(data.first).css('a')
35
- attributes = {
36
- "title" => link.text.strip,
37
- "url" => link.attr('href').value
38
- }
39
-
40
- last = Nokogiri::HTML.fragment(data.last)
41
- if date_format = LOCALE_DATE_FORMATS[locale] and date = last.text.strip[/\d+\/\d+\/\d+\z/, 0]
42
- attributes["date"] = DateTime.strptime(date, date_format)
43
- end
44
-
45
- if data.length >= 3
46
- attributes["source"] = Nokogiri::HTML.fragment(data[-2]).text.strip
47
- end
48
-
49
- new(attributes)
50
- end
51
- end
10
+ # Override all method to use openbeautyfacts domain if it exists
11
+ def all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN, **options)
12
+ super(locale: locale, domain: domain, **options)
13
+ rescue NoMethodError
14
+ # Method doesn't exist in parent class, skip
52
15
  end
53
16
  end
54
17
 
18
+ # Override products method to use openbeautyfacts domain if it exists
19
+ def products(page: -1)
20
+ super(page: page)
21
+ rescue NoMethodError
22
+ # Method doesn't exist in parent class, skip
23
+ end
55
24
  end
56
25
  end