openfoodfacts 0.2.2 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 236ff1efe128490b9d482a5146be10fbe6907bd6
4
- data.tar.gz: 24ab028750cca3d08ff539aed262c1bbb94b6f8b
3
+ metadata.gz: fd868f38cbc5e9fd890a62c71a25f0676e665be1
4
+ data.tar.gz: 0c4ff491f6376b0cd0336894925ee6abc05f6ac6
5
5
  SHA512:
6
- metadata.gz: f26f4ce2748bb094cd6c162427e99bd47754e5cecd962125c94b464382f3113f38d1ecefa919d678dbfa19cbb95df394ee38d4ec97c5b2f1697b5478247ed952
7
- data.tar.gz: d918fdc1c6585a50837c257f3b4b58fe5b06d0519a51a6ccda06c70c1dadd3fe3b6d5758d4a05a28bffa50f7d5b76efe6b050edffabd672d687036e6c8e00848
6
+ metadata.gz: d1791ac7c859f1e4c09150a726f32d79c74425da186f599dea4acc9daa6e4641b0455a5192554a7db417bb869a81374dce9bd4ee998be5020b0b58849bad0174
7
+ data.tar.gz: 72adbba81b6941d695b866dfdeef6f9dd8fb7a0facefc6b84c6b99f9f2fd91d314fec9cf2437d41b0b839e0dc0a86eb8b7fb9ea6a5e4fda5dbc3b394cc1194fe
data/README.md CHANGED
@@ -2,8 +2,8 @@
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/openfoodfacts.svg)](http://badge.fury.io/rb/openfoodfacts)
4
4
  [![Build Status](https://travis-ci.org/openfoodfacts/openfoodfacts-ruby.svg?branch=master)](https://travis-ci.org/openfoodfacts/openfoodfacts-ruby.svg?branch=master)
5
- [![Dependency Status](https://gemnasium.com/nicolasleger/openfoodfacts-ruby.svg)](https://gemnasium.com/nicolasleger/openfoodfacts-ruby)
6
- [![Code Climate](https://codeclimate.com/github/nicolasleger/openfoodfacts-ruby/badges/gpa.svg)](https://codeclimate.com/github/nicolasleger/openfoodfacts-ruby)
5
+ [![Dependency Status](https://gemnasium.com/openfoodfacts/openfoodfacts-ruby.svg)](https://gemnasium.com/openfoodfacts/openfoodfacts-ruby)
6
+ [![Code Climate](https://codeclimate.com/github/openfoodfacts/openfoodfacts-ruby/badges/gpa.svg)](https://codeclimate.com/github/openfoodfacts/openfoodfacts-ruby)
7
7
  [![Coverage Status](https://coveralls.io/repos/nicolasleger/openfoodfacts-ruby/badge.svg)](https://coveralls.io/r/nicolasleger/openfoodfacts-ruby)
8
8
 
9
9
  API Wrapper for [OpenFoodFacts](https://openfoodfacts.org/), the open database about food.
@@ -24,6 +24,32 @@ Or install it yourself as:
24
24
 
25
25
  $ gem install openfoodfacts
26
26
 
27
+ ## Models
28
+
29
+ All data is available for World, French, UK and US version for now. You should update the gem page URLs mapping for others.
30
+
31
+ - Additive
32
+ - Brand
33
+ - Category
34
+ - City
35
+ - Contributor
36
+ - Country
37
+ - Faq
38
+ - IngredientThatMayBeFromPalmOil
39
+ - Label
40
+ - Locale
41
+ - ManufacturingPlace
42
+ - Origin
43
+ - PackagerCode
44
+ - Packaging
45
+ - Press
46
+ - Product
47
+ - ProductState
48
+ - PurchasePlace
49
+ - Store
50
+ - Trace
51
+ - User
52
+
27
53
  ## Usage
28
54
 
29
55
  ```ruby
data/lib/openfoodfacts.rb CHANGED
@@ -1,6 +1,23 @@
1
+ require_relative 'openfoodfacts/additive'
2
+ require_relative 'openfoodfacts/brand'
3
+ require_relative 'openfoodfacts/category'
4
+ require_relative 'openfoodfacts/city'
5
+ require_relative 'openfoodfacts/contributor'
6
+ require_relative 'openfoodfacts/country'
7
+ require_relative 'openfoodfacts/faq'
8
+ require_relative 'openfoodfacts/ingredient_that_may_be_from_palm_oil'
9
+ require_relative 'openfoodfacts/label'
1
10
  require_relative 'openfoodfacts/locale'
11
+ require_relative 'openfoodfacts/manufacturing_place'
12
+ require_relative 'openfoodfacts/origin'
13
+ require_relative 'openfoodfacts/packager_code'
14
+ require_relative 'openfoodfacts/packaging'
15
+ require_relative 'openfoodfacts/press'
2
16
  require_relative 'openfoodfacts/product'
3
17
  require_relative 'openfoodfacts/product_state'
18
+ require_relative 'openfoodfacts/purchase_place'
19
+ require_relative 'openfoodfacts/store'
20
+ require_relative 'openfoodfacts/trace'
4
21
  require_relative 'openfoodfacts/user'
5
22
  require_relative 'openfoodfacts/version'
6
23
 
@@ -0,0 +1,51 @@
1
+ require 'hashie'
2
+
3
+ module Openfoodfacts
4
+ class Additive < Hashie::Mash
5
+
6
+ # TODO: Add more locales
7
+ LOCALE_PATHS = {
8
+ 'fr' => 'additifs',
9
+ 'uk' => 'additives',
10
+ 'us' => 'additives',
11
+ 'world' => 'additives'
12
+ }
13
+
14
+ class << self
15
+
16
+ # Get additives
17
+ #
18
+ def all(locale: Openfoodfacts::DEFAULT_LOCALE)
19
+ if path = LOCALE_PATHS[locale]
20
+ page_url = "http://#{locale}.openfoodfacts.org/#{path}"
21
+
22
+ Product.tags_from_page(self, page_url) do |tag|
23
+ columns = tag.css('td')
24
+
25
+ link = tag.css('a').first
26
+ attributes = {
27
+ "name" => link.text.strip,
28
+ "url" => URI.join(page_url, link.attr('href')).to_s,
29
+ "products_count" => columns[1].text.to_i,
30
+ }
31
+
32
+ riskiness = columns[2].attr('class')
33
+ if riskiness
34
+ attributes["riskiness"] = riskiness[/level_(\d+)/, 1].to_i
35
+ end
36
+
37
+ new(attributes)
38
+ end
39
+ end
40
+ end
41
+
42
+ end
43
+
44
+ # Get products with additive
45
+ #
46
+ def products(page: -1)
47
+ Product.from_website_page(url, page: page, products_count: products_count) if url
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,33 @@
1
+ require 'hashie'
2
+
3
+ module Openfoodfacts
4
+ class Allergen < Hashie::Mash
5
+
6
+ # TODO: Add more locales
7
+ LOCALE_PATHS = {
8
+ 'fr' => 'allergenes',
9
+ 'uk' => 'allergens',
10
+ 'us' => 'allergens',
11
+ 'world' => 'allergens'
12
+ }
13
+
14
+ class << self
15
+
16
+ # Get allergens
17
+ #
18
+ def all(locale: Openfoodfacts::DEFAULT_LOCALE)
19
+ if path = LOCALE_PATHS[locale]
20
+ Product.tags_from_page(self, "http://#{locale}.openfoodfacts.org/#{path}")
21
+ end
22
+ end
23
+
24
+ end
25
+
26
+ # Get products with allergen
27
+ #
28
+ def products(page: -1)
29
+ Product.from_website_page(url, page: page, products_count: products_count) if url
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,33 @@
1
+ require 'hashie'
2
+
3
+ module Openfoodfacts
4
+ class Brand < Hashie::Mash
5
+
6
+ # TODO: Add more locales
7
+ LOCALE_PATHS = {
8
+ 'fr' => 'marques',
9
+ 'uk' => 'brands',
10
+ 'us' => 'brands',
11
+ 'world' => 'brands'
12
+ }
13
+
14
+ class << self
15
+
16
+ # Get product brands
17
+ #
18
+ def all(locale: Openfoodfacts::DEFAULT_LOCALE)
19
+ if path = LOCALE_PATHS[locale]
20
+ Product.tags_from_page(self, "http://#{locale}.openfoodfacts.org/#{path}")
21
+ end
22
+ end
23
+
24
+ end
25
+
26
+ # Get products with brand
27
+ #
28
+ def products(page: -1)
29
+ Product.from_website_page(url, page: page, products_count: products_count) if url
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,33 @@
1
+ require 'hashie'
2
+
3
+ module Openfoodfacts
4
+ class Category < Hashie::Mash
5
+
6
+ # TODO: Add more locales
7
+ LOCALE_PATHS = {
8
+ 'fr' => 'categories',
9
+ 'uk' => 'categories',
10
+ 'us' => 'categories',
11
+ 'world' => 'categories'
12
+ }
13
+
14
+ class << self
15
+
16
+ # Get categories
17
+ #
18
+ def all(locale: Openfoodfacts::DEFAULT_LOCALE)
19
+ if path = LOCALE_PATHS[locale]
20
+ Product.tags_from_page(self, "http://#{locale}.openfoodfacts.org/#{path}")
21
+ end
22
+ end
23
+
24
+ end
25
+
26
+ # Get products with category
27
+ #
28
+ def products(page: -1)
29
+ Product.from_website_page(url, page: page, products_count: products_count) if url
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,33 @@
1
+ require 'hashie'
2
+
3
+ module Openfoodfacts
4
+ class City < Hashie::Mash
5
+
6
+ # TODO: Add more locales
7
+ LOCALE_PATHS = {
8
+ 'fr' => 'communes',
9
+ 'uk' => 'cities',
10
+ 'us' => 'cities',
11
+ 'world' => 'cities'
12
+ }
13
+
14
+ class << self
15
+
16
+ # Get cities
17
+ #
18
+ def all(locale: Openfoodfacts::DEFAULT_LOCALE)
19
+ if path = LOCALE_PATHS[locale]
20
+ Product.tags_from_page(self, "http://#{locale}.openfoodfacts.org/#{path}")
21
+ end
22
+ end
23
+
24
+ end
25
+
26
+ # Get products with city
27
+ #
28
+ def products(page: -1)
29
+ Product.from_website_page(url, page: page, products_count: products_count) if url
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,33 @@
1
+ require 'hashie'
2
+
3
+ module Openfoodfacts
4
+ class Contributor < Hashie::Mash
5
+
6
+ # TODO: Add more locales
7
+ LOCALE_PATHS = {
8
+ 'fr' => 'contributeurs',
9
+ 'uk' => 'contributors',
10
+ 'us' => 'contributors',
11
+ 'world' => 'contributors'
12
+ }
13
+
14
+ class << self
15
+
16
+ # Get contributors
17
+ #
18
+ def all(locale: Openfoodfacts::DEFAULT_LOCALE)
19
+ if path = LOCALE_PATHS[locale]
20
+ Product.tags_from_page(self, "http://#{locale}.openfoodfacts.org/#{path}")
21
+ end
22
+ end
23
+
24
+ end
25
+
26
+ # Get products for contributor
27
+ #
28
+ def products(page: -1)
29
+ Product.from_website_page(url, page: page, products_count: products_count) if url
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,33 @@
1
+ require 'hashie'
2
+
3
+ module Openfoodfacts
4
+ class Country < Hashie::Mash
5
+
6
+ # TODO: Add more locales
7
+ LOCALE_PATHS = {
8
+ 'fr' => 'pays',
9
+ 'uk' => 'countries',
10
+ 'us' => 'countries',
11
+ 'world' => 'countries'
12
+ }
13
+
14
+ class << self
15
+
16
+ # Get countries
17
+ #
18
+ def all(locale: Openfoodfacts::DEFAULT_LOCALE)
19
+ if path = LOCALE_PATHS[locale]
20
+ Product.tags_from_page(self, "http://#{locale}.openfoodfacts.org/#{path}")
21
+ end
22
+ end
23
+
24
+ end
25
+
26
+ # Get products with country
27
+ #
28
+ def products(page: -1)
29
+ Product.from_website_page(url, page: page, products_count: products_count) if url
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,50 @@
1
+ require 'hashie'
2
+ require 'nokogiri'
3
+ require 'open-uri'
4
+
5
+ module Openfoodfacts
6
+ class Faq < Hashie::Mash
7
+
8
+ # TODO: Add more locales
9
+ LOCALE_PATHS = {
10
+ 'fr' => 'questions-frequentes',
11
+ 'uk' => 'faq',
12
+ 'us' => 'faq',
13
+ 'world' => 'faq'
14
+ }
15
+
16
+ class << self
17
+ def items(locale: Openfoodfacts::DEFAULT_LOCALE)
18
+ if path = LOCALE_PATHS[locale]
19
+ html = open("http://#{locale}.openfoodfacts.org/#{path}").read
20
+ dom = Nokogiri::HTML.fragment(html)
21
+
22
+ titles = dom.css('#main_column h2')
23
+ titles.each_with_index.map do |item, index|
24
+ paragraphs = []
25
+
26
+ element = item.next_sibling
27
+ while !element.nil? && element.node_name != 'h2'
28
+ if element.node_name == 'p'
29
+ paragraphs.push(element)
30
+ end
31
+
32
+ element = element.next_sibling
33
+ end
34
+
35
+ if index == titles.length - 1
36
+ paragraphs = paragraphs[0..-3]
37
+ end
38
+
39
+ new({
40
+ "question" => item.text.strip,
41
+ "answer" => paragraphs.map { |paragraph| paragraph.text.strip.gsub(/\r?\n/, ' ') }.join("\n\n"),
42
+ "answer_html" => paragraphs.map(&:to_html).join
43
+ })
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,33 @@
1
+ require 'hashie'
2
+
3
+ module Openfoodfacts
4
+ class IngredientThatMayBeFromPalmOil < Hashie::Mash
5
+
6
+ # TODO: Add more locales
7
+ LOCALE_PATHS = {
8
+ 'fr' => 'ingredients-pouvant-etre-issus-de-l-huile-de-palme',
9
+ 'uk' => 'ingredients-that-may-be-from-palm-oil',
10
+ 'us' => 'ingredients-that-may-be-from-palm-oil',
11
+ 'world' => 'ingredients-that-may-be-from-palm-oil'
12
+ }
13
+
14
+ class << self
15
+
16
+ # Get ingredients that may be from palm oil
17
+ #
18
+ def all(locale: Openfoodfacts::DEFAULT_LOCALE)
19
+ if path = LOCALE_PATHS[locale]
20
+ Product.tags_from_page(self, "http://#{locale}.openfoodfacts.org/#{path}")
21
+ end
22
+ end
23
+
24
+ end
25
+
26
+ # Get products with ingredient that may be from palm oil
27
+ #
28
+ def products(page: -1)
29
+ Product.from_website_page(url, page: page, products_count: products_count) if url
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,33 @@
1
+ require 'hashie'
2
+
3
+ module Openfoodfacts
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
+ }
13
+
14
+ class << self
15
+
16
+ # Get labels
17
+ #
18
+ def all(locale: Openfoodfacts::DEFAULT_LOCALE)
19
+ if path = LOCALE_PATHS[locale]
20
+ Product.tags_from_page(self, "http://#{locale}.openfoodfacts.org/#{path}")
21
+ end
22
+ end
23
+
24
+ end
25
+
26
+ # Get products with label
27
+ #
28
+ def products(page: -1)
29
+ Product.from_website_page(url, page: page, products_count: products_count) if url
30
+ end
31
+
32
+ end
33
+ end