openfoodfacts 0.3.1 → 0.4.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.
- checksums.yaml +4 -4
- data/README.md +4 -4
- data/lib/openfoodfacts.rb +2 -1
- data/lib/openfoodfacts/additive.rb +4 -4
- data/lib/openfoodfacts/allergen.rb +3 -3
- data/lib/openfoodfacts/brand.rb +2 -2
- data/lib/openfoodfacts/category.rb +3 -3
- data/lib/openfoodfacts/city.rb +3 -3
- data/lib/openfoodfacts/contributor.rb +3 -3
- data/lib/openfoodfacts/country.rb +3 -3
- data/lib/openfoodfacts/faq.rb +5 -5
- data/lib/openfoodfacts/ingredient_that_may_be_from_palm_oil.rb +3 -3
- data/lib/openfoodfacts/label.rb +5 -10
- data/lib/openfoodfacts/locale.rb +2 -2
- data/lib/openfoodfacts/manufacturing_place.rb +3 -3
- data/lib/openfoodfacts/origin.rb +3 -3
- data/lib/openfoodfacts/packager_code.rb +3 -3
- data/lib/openfoodfacts/packaging.rb +3 -3
- data/lib/openfoodfacts/press.rb +7 -7
- data/lib/openfoodfacts/product.rb +17 -22
- data/lib/openfoodfacts/product_state.rb +3 -3
- data/lib/openfoodfacts/purchase_place.rb +3 -3
- data/lib/openfoodfacts/store.rb +3 -3
- data/lib/openfoodfacts/trace.rb +3 -3
- data/lib/openfoodfacts/user.rb +5 -4
- data/lib/openfoodfacts/version.rb +1 -1
- data/test/fixtures/fetch_product_3029330003533.yml +1 -1
- data/test/fixtures/index.yml +1 -1
- data/test/fixtures/login_user.yml +1 -1
- data/test/fixtures/product_3029330003533.yml +1 -1
- data/test/fixtures/product_states.yml +1 -1
- data/test/fixtures/products_for_state.yml +2 -2
- data/test/fixtures/search_Chocolat.yml +2 -2
- data/test/fixtures/search_Chocolat_1_000_000.yml +1 -1
- data/test/test_openfoodfacts.rb +8 -18
- metadata +4 -28
- data/test/fixtures/additives.yml +0 -1963
- data/test/fixtures/additives_locale.yml +0 -1963
- data/test/fixtures/brands.yml +0 -31542
- data/test/fixtures/brands_locale.yml +0 -17498
- data/test/fixtures/faq.yml +0 -1078
- data/test/fixtures/labels.yml +0 -9091
- data/test/fixtures/press.yml +0 -1284
- data/test/fixtures/product_states_locale.yml +0 -1002
- data/test/fixtures/products_for_brand.yml +0 -5481
- data/test/fixtures/products_with_additive.yml +0 -3130
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1ab380f0014effa3920df74d1ba593e4f26d50e9
|
4
|
+
data.tar.gz: 384401825b4f8fdc2e3ea18555f79f9714567f59
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 131ae9169cf6279ca8dac267d145e2c18513a2503e225c45f6de7e0ba8e16804ac7e9579a92295eea0078cbe5aa3113e59ee2a253b8ce5ca9cef2e28a775e9f2
|
7
|
+
data.tar.gz: c97be50ed729e1a64b40b28c520929df384a6fc02f6f48b364aaf1833e3609879b3c1ccdc3bc3ac61d9e1956f7c5cfb8961d390705ec4684db30cbfaccbaea84
|
data/README.md
CHANGED
@@ -1,13 +1,13 @@
|
|
1
|
-
#
|
1
|
+
# Open Food Facts
|
2
2
|
|
3
3
|
[](http://badge.fury.io/rb/openfoodfacts)
|
4
4
|
[](https://travis-ci.org/openfoodfacts/openfoodfacts-ruby)
|
5
5
|
[](https://gemnasium.com/openfoodfacts/openfoodfacts-ruby)
|
6
6
|
[](https://codeclimate.com/github/openfoodfacts/openfoodfacts-ruby)
|
7
|
-
[](https://coveralls.io/github/openfoodfacts/openfoodfacts-ruby?branch=master)
|
8
8
|
[](http://inch-ci.org/github/openfoodfacts/openfoodfacts-ruby)
|
9
9
|
|
10
|
-
API Wrapper for [
|
10
|
+
API Wrapper for [Open Food Facts](https://openfoodfacts.org/), the open database about food.
|
11
11
|
|
12
12
|
## Installation
|
13
13
|
|
@@ -79,7 +79,7 @@ end
|
|
79
79
|
# Search products
|
80
80
|
|
81
81
|
products = Openfoodfacts::Product.search("Chocolate", locale: 'world', page_size: 3)
|
82
|
-
# => [#<Openfoodfacts::Product _id="3045140105502" code="3045140105502" id="3045140105502" image_small_url="
|
82
|
+
# => [#<Openfoodfacts::Product _id="3045140105502" code="3045140105502" id="3045140105502" image_small_url="https://en.openfoodfacts.org/images/products/304/514/010/5502/front.7.100.jpg" lc="en" product_name="Milka au lait du Pays Alpin">, #<Openfoodfacts::Product _id="3046920028363" code="3046920028363" id="3046920028363" image_small_url="https://en.openfoodfacts.org/images/products/304/692/002/8363/front.5.100.jpg" lc="en" product_name="Tableta de chocolate negro \"Lindt Excellence\" 85% cacao">, #<Openfoodfacts::Product _id="3046920029759" code="3046920029759" id="3046920029759" image_small_url="https://en.openfoodfacts.org/images/products/304/692/002/9759/front.9.100.jpg" lc="en" product_name="Tableta de chocolate negro \"Lindt Excellence\" 90% cacao">]
|
83
83
|
|
84
84
|
# You might need to fetch full product data from results
|
85
85
|
|
data/lib/openfoodfacts.rb
CHANGED
@@ -15,9 +15,9 @@ module Openfoodfacts
|
|
15
15
|
|
16
16
|
# Get additives
|
17
17
|
#
|
18
|
-
def all(locale:
|
18
|
+
def all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
|
19
19
|
if path = LOCALE_PATHS[locale]
|
20
|
-
page_url = "
|
20
|
+
page_url = "https://#{locale}.#{domain}/#{path}"
|
21
21
|
|
22
22
|
Product.tags_from_page(self, page_url) do |tag|
|
23
23
|
columns = tag.css('td')
|
@@ -28,7 +28,7 @@ module Openfoodfacts
|
|
28
28
|
"url" => URI.join(page_url, link.attr('href')).to_s,
|
29
29
|
"products_count" => columns[1].text.to_i,
|
30
30
|
}
|
31
|
-
|
31
|
+
|
32
32
|
riskiness = columns[2].attr('class')
|
33
33
|
if riskiness
|
34
34
|
attributes["riskiness"] = riskiness[/level_(\d+)/, 1].to_i
|
@@ -48,4 +48,4 @@ module Openfoodfacts
|
|
48
48
|
end
|
49
49
|
|
50
50
|
end
|
51
|
-
end
|
51
|
+
end
|
@@ -15,9 +15,9 @@ module Openfoodfacts
|
|
15
15
|
|
16
16
|
# Get allergens
|
17
17
|
#
|
18
|
-
def all(locale:
|
18
|
+
def all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
|
19
19
|
if path = LOCALE_PATHS[locale]
|
20
|
-
Product.tags_from_page(self, "
|
20
|
+
Product.tags_from_page(self, "https://#{locale}.#{domain}/#{path}")
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -30,4 +30,4 @@ module Openfoodfacts
|
|
30
30
|
end
|
31
31
|
|
32
32
|
end
|
33
|
-
end
|
33
|
+
end
|
data/lib/openfoodfacts/brand.rb
CHANGED
@@ -15,9 +15,9 @@ module Openfoodfacts
|
|
15
15
|
|
16
16
|
# Get product brands
|
17
17
|
#
|
18
|
-
def all(locale:
|
18
|
+
def all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
|
19
19
|
if path = LOCALE_PATHS[locale]
|
20
|
-
Product.tags_from_page(self, "
|
20
|
+
Product.tags_from_page(self, "https://#{locale}.#{domain}/#{path}")
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -15,9 +15,9 @@ module Openfoodfacts
|
|
15
15
|
|
16
16
|
# Get categories
|
17
17
|
#
|
18
|
-
def all(locale:
|
18
|
+
def all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
|
19
19
|
if path = LOCALE_PATHS[locale]
|
20
|
-
Product.tags_from_page(self, "
|
20
|
+
Product.tags_from_page(self, "https://#{locale}.#{domain}/#{path}")
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -30,4 +30,4 @@ module Openfoodfacts
|
|
30
30
|
end
|
31
31
|
|
32
32
|
end
|
33
|
-
end
|
33
|
+
end
|
data/lib/openfoodfacts/city.rb
CHANGED
@@ -15,9 +15,9 @@ module Openfoodfacts
|
|
15
15
|
|
16
16
|
# Get cities
|
17
17
|
#
|
18
|
-
def all(locale:
|
18
|
+
def all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
|
19
19
|
if path = LOCALE_PATHS[locale]
|
20
|
-
Product.tags_from_page(self, "
|
20
|
+
Product.tags_from_page(self, "https://#{locale}.#{domain}/#{path}")
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -30,4 +30,4 @@ module Openfoodfacts
|
|
30
30
|
end
|
31
31
|
|
32
32
|
end
|
33
|
-
end
|
33
|
+
end
|
@@ -15,9 +15,9 @@ module Openfoodfacts
|
|
15
15
|
|
16
16
|
# Get contributors
|
17
17
|
#
|
18
|
-
def all(locale:
|
18
|
+
def all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
|
19
19
|
if path = LOCALE_PATHS[locale]
|
20
|
-
Product.tags_from_page(self, "
|
20
|
+
Product.tags_from_page(self, "https://#{locale}.#{domain}/#{path}")
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -30,4 +30,4 @@ module Openfoodfacts
|
|
30
30
|
end
|
31
31
|
|
32
32
|
end
|
33
|
-
end
|
33
|
+
end
|
@@ -15,9 +15,9 @@ module Openfoodfacts
|
|
15
15
|
|
16
16
|
# Get countries
|
17
17
|
#
|
18
|
-
def all(locale:
|
18
|
+
def all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
|
19
19
|
if path = LOCALE_PATHS[locale]
|
20
|
-
Product.tags_from_page(self, "
|
20
|
+
Product.tags_from_page(self, "https://#{locale}.#{domain}/#{path}")
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -30,4 +30,4 @@ module Openfoodfacts
|
|
30
30
|
end
|
31
31
|
|
32
32
|
end
|
33
|
-
end
|
33
|
+
end
|
data/lib/openfoodfacts/faq.rb
CHANGED
@@ -14,15 +14,15 @@ module Openfoodfacts
|
|
14
14
|
}
|
15
15
|
|
16
16
|
class << self
|
17
|
-
def items(locale:
|
17
|
+
def items(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
|
18
18
|
if path = LOCALE_PATHS[locale]
|
19
|
-
html = open("
|
19
|
+
html = open("https://#{locale}.#{domain}/#{path}").read
|
20
20
|
dom = Nokogiri::HTML.fragment(html)
|
21
|
-
|
21
|
+
|
22
22
|
titles = dom.css('#main_column h2')
|
23
23
|
titles.each_with_index.map do |item, index|
|
24
24
|
paragraphs = []
|
25
|
-
|
25
|
+
|
26
26
|
element = item.next_sibling
|
27
27
|
while !element.nil? && element.node_name != 'h2'
|
28
28
|
if element.node_name == 'p'
|
@@ -47,4 +47,4 @@ module Openfoodfacts
|
|
47
47
|
end
|
48
48
|
|
49
49
|
end
|
50
|
-
end
|
50
|
+
end
|
@@ -15,9 +15,9 @@ module Openfoodfacts
|
|
15
15
|
|
16
16
|
# Get ingredients that may be from palm oil
|
17
17
|
#
|
18
|
-
def all(locale:
|
18
|
+
def all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
|
19
19
|
if path = LOCALE_PATHS[locale]
|
20
|
-
Product.tags_from_page(self, "
|
20
|
+
Product.tags_from_page(self, "https://#{locale}.#{domain}/#{path}")
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -30,4 +30,4 @@ module Openfoodfacts
|
|
30
30
|
end
|
31
31
|
|
32
32
|
end
|
33
|
-
end
|
33
|
+
end
|
data/lib/openfoodfacts/label.rb
CHANGED
@@ -1,10 +1,8 @@
|
|
1
1
|
require 'hashie'
|
2
|
-
require 'json'
|
3
|
-
require 'open-uri'
|
4
2
|
|
5
3
|
module Openfoodfacts
|
6
4
|
class Label < Hashie::Mash
|
7
|
-
|
5
|
+
|
8
6
|
# TODO: Add more locales
|
9
7
|
LOCALE_PATHS = {
|
10
8
|
'fr' => 'labels',
|
@@ -17,12 +15,9 @@ module Openfoodfacts
|
|
17
15
|
|
18
16
|
# Get labels
|
19
17
|
#
|
20
|
-
def all(locale:
|
18
|
+
def all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
|
21
19
|
if path = LOCALE_PATHS[locale]
|
22
|
-
|
23
|
-
json = JSON.parse(body)
|
24
|
-
json_labels = json['tags']
|
25
|
-
json_labels.map(&method(:new))
|
20
|
+
Product.tags_from_page(self, "https://#{locale}.#{domain}/#{path}")
|
26
21
|
end
|
27
22
|
end
|
28
23
|
|
@@ -31,8 +26,8 @@ module Openfoodfacts
|
|
31
26
|
# Get products with label
|
32
27
|
#
|
33
28
|
def products(page: -1)
|
34
|
-
Product.
|
29
|
+
Product.from_website_page(url, page: page, products_count: products_count) if url
|
35
30
|
end
|
36
31
|
|
37
32
|
end
|
38
|
-
end
|
33
|
+
end
|
data/lib/openfoodfacts/locale.rb
CHANGED
@@ -15,9 +15,9 @@ module Openfoodfacts
|
|
15
15
|
|
16
16
|
# Get manufacturing places
|
17
17
|
#
|
18
|
-
def all(locale:
|
18
|
+
def all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
|
19
19
|
if path = LOCALE_PATHS[locale]
|
20
|
-
Product.tags_from_page(self, "
|
20
|
+
Product.tags_from_page(self, "https://#{locale}.#{domain}/#{path}")
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -30,4 +30,4 @@ module Openfoodfacts
|
|
30
30
|
end
|
31
31
|
|
32
32
|
end
|
33
|
-
end
|
33
|
+
end
|
data/lib/openfoodfacts/origin.rb
CHANGED
@@ -15,9 +15,9 @@ module Openfoodfacts
|
|
15
15
|
|
16
16
|
# Get origins
|
17
17
|
#
|
18
|
-
def all(locale:
|
18
|
+
def all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
|
19
19
|
if path = LOCALE_PATHS[locale]
|
20
|
-
Product.tags_from_page(self, "
|
20
|
+
Product.tags_from_page(self, "https://#{locale}.#{domain}/#{path}")
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -30,4 +30,4 @@ module Openfoodfacts
|
|
30
30
|
end
|
31
31
|
|
32
32
|
end
|
33
|
-
end
|
33
|
+
end
|
@@ -15,9 +15,9 @@ module Openfoodfacts
|
|
15
15
|
|
16
16
|
# Get packager codes
|
17
17
|
#
|
18
|
-
def all(locale:
|
18
|
+
def all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
|
19
19
|
if path = LOCALE_PATHS[locale]
|
20
|
-
Product.tags_from_page(self, "
|
20
|
+
Product.tags_from_page(self, "https://#{locale}.#{domain}/#{path}")
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -30,4 +30,4 @@ module Openfoodfacts
|
|
30
30
|
end
|
31
31
|
|
32
32
|
end
|
33
|
-
end
|
33
|
+
end
|
@@ -15,9 +15,9 @@ module Openfoodfacts
|
|
15
15
|
|
16
16
|
# Get packagings
|
17
17
|
#
|
18
|
-
def all(locale:
|
18
|
+
def all(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
|
19
19
|
if path = LOCALE_PATHS[locale]
|
20
|
-
Product.tags_from_page(self, "
|
20
|
+
Product.tags_from_page(self, "https://#{locale}.#{domain}/#{path}")
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -30,4 +30,4 @@ module Openfoodfacts
|
|
30
30
|
end
|
31
31
|
|
32
32
|
end
|
33
|
-
end
|
33
|
+
end
|
data/lib/openfoodfacts/press.rb
CHANGED
@@ -16,17 +16,17 @@ module Openfoodfacts
|
|
16
16
|
|
17
17
|
LOCALE_DATE_FORMATS = {
|
18
18
|
'fr' => '%d/%m/%Y',
|
19
|
-
'uk' => '%m/%
|
20
|
-
'us' => '%m/%
|
21
|
-
'world' => '%m/%
|
19
|
+
'uk' => '%d/%m/%Y',
|
20
|
+
'us' => '%d/%m/%Y',
|
21
|
+
'world' => '%d/%m/%Y'
|
22
22
|
}
|
23
23
|
|
24
24
|
class << self
|
25
|
-
def items(locale:
|
25
|
+
def items(locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
|
26
26
|
if path = LOCALE_PATHS[locale]
|
27
|
-
html = open("
|
27
|
+
html = open("https://#{locale}.#{domain}/#{path}").read
|
28
28
|
dom = Nokogiri::HTML.fragment(html)
|
29
|
-
|
29
|
+
|
30
30
|
titles = dom.css('#main_column li')
|
31
31
|
titles.each_with_index.map do |item, index|
|
32
32
|
data = item.inner_html.split(' - ')
|
@@ -53,4 +53,4 @@ module Openfoodfacts
|
|
53
53
|
end
|
54
54
|
|
55
55
|
end
|
56
|
-
end
|
56
|
+
end
|
@@ -16,20 +16,9 @@ module Openfoodfacts
|
|
16
16
|
|
17
17
|
class << self
|
18
18
|
|
19
|
-
# Products from API
|
20
|
-
#
|
21
|
-
def from_api(api_url, page: -1)
|
22
|
-
if url
|
23
|
-
body = open(api_url).read
|
24
|
-
json = JSON.parse(body)
|
25
|
-
json_products = json['products']
|
26
|
-
json_products.map(&method(:new))
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
19
|
# Get product
|
31
20
|
#
|
32
|
-
def get(code, locale:
|
21
|
+
def get(code, locale: DEFAULT_LOCALE)
|
33
22
|
if code
|
34
23
|
product_url = url(code, locale: locale)
|
35
24
|
json = open(product_url).read
|
@@ -42,15 +31,19 @@ module Openfoodfacts
|
|
42
31
|
|
43
32
|
# Return product API URL
|
44
33
|
#
|
45
|
-
def url(code, locale:
|
46
|
-
|
34
|
+
def url(code, locale: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
|
35
|
+
if code
|
36
|
+
path = "api/v0/produit/#{code}.json"
|
37
|
+
"https://#{locale}.#{domain}/#{path}"
|
38
|
+
end
|
47
39
|
end
|
48
40
|
|
49
41
|
# Search products
|
50
42
|
#
|
51
|
-
def search(terms, locale:
|
43
|
+
def search(terms, locale: DEFAULT_LOCALE, page: 1, page_size: 20, sort_by: 'unique_scans_n', domain: DEFAULT_DOMAIN)
|
52
44
|
terms = URI::encode(terms)
|
53
|
-
|
45
|
+
path = "cgi/search.pl?search_terms=#{terms}&jqm=1&page=#{page}&page_size=#{page_size}&sort_by=#{sort_by}"
|
46
|
+
url = "https://#{locale}.#{domain}/#{path}"
|
54
47
|
json = open(url).read
|
55
48
|
hash = JSON.parse(json)
|
56
49
|
html = hash["jqm"]
|
@@ -154,10 +147,11 @@ module Openfoodfacts
|
|
154
147
|
# User can be nil
|
155
148
|
# Tested not updatable fields: countries, ingredients_text, purchase_places, purchase_places_tag, purchase_places_tags
|
156
149
|
#
|
157
|
-
def update(user: nil)
|
150
|
+
def update(user: nil, domain: DEFAULT_DOMAIN)
|
158
151
|
if self.code && self.lc
|
159
152
|
subdomain = self.lc == 'world' ? 'world' : "world-#{self.lc}"
|
160
|
-
|
153
|
+
path = 'cgi/product_jqm.pl'
|
154
|
+
uri = URI("https://#{subdomain}.#{domain}/#{path}")
|
161
155
|
params = self.to_hash
|
162
156
|
params.merge!("user_id" => user.user_id, "password" => user.password) if user
|
163
157
|
response = Net::HTTP.post_form(uri, params)
|
@@ -172,17 +166,18 @@ module Openfoodfacts
|
|
172
166
|
|
173
167
|
# Return Product API URL
|
174
168
|
#
|
175
|
-
def url(locale:
|
169
|
+
def url(locale: DEFAULT_LOCALE)
|
176
170
|
self.class.url(self.code, locale: locale)
|
177
171
|
end
|
178
172
|
|
179
173
|
# Return Product web URL according to locale
|
180
174
|
#
|
181
|
-
def weburl(locale: nil)
|
182
|
-
locale ||= self.lc ||
|
175
|
+
def weburl(locale: nil, domain: DEFAULT_DOMAIN)
|
176
|
+
locale ||= self.lc || DEFAULT_LOCALE
|
183
177
|
|
184
178
|
if self.code && prefix = LOCALE_WEBURL_PREFIXES[locale]
|
185
|
-
"
|
179
|
+
path = "#{prefix}/#{self.code}"
|
180
|
+
"https://#{locale}.#{domain}/#{path}"
|
186
181
|
end
|
187
182
|
end
|
188
183
|
|