openbeautyfacts 0.1.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 (48) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +21 -0
  3. data/README.md +111 -0
  4. data/Rakefile +19 -0
  5. data/lib/openbeautyfacts.rb +60 -0
  6. data/lib/openbeautyfacts/additive.rb +51 -0
  7. data/lib/openbeautyfacts/allergen.rb +33 -0
  8. data/lib/openbeautyfacts/brand.rb +33 -0
  9. data/lib/openbeautyfacts/category.rb +33 -0
  10. data/lib/openbeautyfacts/city.rb +33 -0
  11. data/lib/openbeautyfacts/contributor.rb +33 -0
  12. data/lib/openbeautyfacts/country.rb +33 -0
  13. data/lib/openbeautyfacts/faq.rb +50 -0
  14. data/lib/openbeautyfacts/ingredient_that_may_be_from_palm_oil.rb +33 -0
  15. data/lib/openbeautyfacts/label.rb +33 -0
  16. data/lib/openbeautyfacts/locale.rb +30 -0
  17. data/lib/openbeautyfacts/manufacturing_place.rb +33 -0
  18. data/lib/openbeautyfacts/origin.rb +33 -0
  19. data/lib/openbeautyfacts/packager_code.rb +33 -0
  20. data/lib/openbeautyfacts/packaging.rb +33 -0
  21. data/lib/openbeautyfacts/press.rb +56 -0
  22. data/lib/openbeautyfacts/product.rb +185 -0
  23. data/lib/openbeautyfacts/product_state.rb +33 -0
  24. data/lib/openbeautyfacts/purchase_place.rb +33 -0
  25. data/lib/openbeautyfacts/store.rb +33 -0
  26. data/lib/openbeautyfacts/trace.rb +33 -0
  27. data/lib/openbeautyfacts/user.rb +41 -0
  28. data/lib/openbeautyfacts/version.rb +3 -0
  29. data/test/fixtures/additives.yml +1422 -0
  30. data/test/fixtures/additives_locale.yml +1422 -0
  31. data/test/fixtures/brands.yml +2144 -0
  32. data/test/fixtures/brands_locale.yml +2024 -0
  33. data/test/fixtures/faq.yml +1031 -0
  34. data/test/fixtures/fetch_product_3600550362626.yml +104 -0
  35. data/test/fixtures/index.yml +169 -0
  36. data/test/fixtures/login_user.yml +40 -0
  37. data/test/fixtures/press.yml +986 -0
  38. data/test/fixtures/product_3600550362626.yml +104 -0
  39. data/test/fixtures/product_states.yml +936 -0
  40. data/test/fixtures/product_states_locale.yml +976 -0
  41. data/test/fixtures/products_for_brand.yml +1707 -0
  42. data/test/fixtures/products_for_state.yml +2260 -0
  43. data/test/fixtures/products_with_additive.yml +1723 -0
  44. data/test/fixtures/search_doux.yml +538 -0
  45. data/test/fixtures/search_doux_1_000_000.yml +257 -0
  46. data/test/minitest_helper.rb +12 -0
  47. data/test/test_openbeautyfacts.rb +193 -0
  48. metadata +188 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dc1875dd0d4f35000da26600c17317afe76eb644
4
+ data.tar.gz: 04cf3b96a42e49e6aecab11762630c8bbe68c002
5
+ SHA512:
6
+ metadata.gz: 10ea3c62fed84fc3f5a9a0d1f1676da2999342095f8b65c2e1cbf2e408473cf04af7a5b221e5dcc443b2ed618239d0b8dd960a3b220abbe8804a275251d55de3
7
+ data.tar.gz: b35cc9a9329c61020d69eed0a55b5b57a9af3a4241cbe5e1d886fa227c6ce77ffc920822a6dd209dd300e0f8e5a8e545a0cf2c90c4667455ae21d0669b264943
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Nicolas Leger
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,111 @@
1
+ # Open Beauty Facts
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/openbeautyfacts.svg)](http://badge.fury.io/rb/openbeautyfacts)
4
+ [![Build Status](https://travis-ci.org/openbeautyfacts/openbeautyfacts-ruby.svg?branch=master)](https://travis-ci.org/openbeautyfacts/openbeautyfacts-ruby)
5
+ [![Dependency Status](https://gemnasium.com/openbeautyfacts/openbeautyfacts-ruby.svg)](https://gemnasium.com/openbeautyfacts/openbeautyfacts-ruby)
6
+ [![Code Climate](https://codeclimate.com/github/openbeautyfacts/openbeautyfacts-ruby/badges/gpa.svg)](https://codeclimate.com/github/openbeautyfacts/openbeautyfacts-ruby)
7
+ [![Coverage Status](https://coveralls.io/repos/github/openfoodfacts/openbeautyfacts-ruby/badge.svg?branch=master)](https://coveralls.io/github/openbeautyfacts/openbeautyfacts-ruby?branch=master)
8
+ [![Documentation](http://inch-ci.org/github/openbeautyfacts/openbeautyfacts-ruby.svg?branch=master)](http://inch-ci.org/github/openbeautyfacts/openbeautyfacts-ruby)
9
+
10
+ API Wrapper for [Open Beauty Facts](http://openbeautyfacts.org/), the open database about beauty products.
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'openbeautyfacts'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install openbeautyfacts
27
+
28
+ ## Models
29
+
30
+ All data is available for World, French, UK and US version for now. You should update the gem page URLs mapping for others.
31
+
32
+ - Additive
33
+ - Brand
34
+ - Category
35
+ - City
36
+ - Contributor
37
+ - Country
38
+ - Faq
39
+ - IngredientThatMayBeFromPalmOil
40
+ - Label
41
+ - Locale
42
+ - ManufacturingPlace
43
+ - Origin
44
+ - PackagerCode
45
+ - Packaging
46
+ - Press
47
+ - Product
48
+ - ProductState
49
+ - PurchasePlace
50
+ - Store
51
+ - Trace
52
+ - User
53
+
54
+ ## Usage
55
+
56
+ ```ruby
57
+ require 'openbeautyfacts'
58
+
59
+ # Browse a product
60
+
61
+ code = "3029330003533"
62
+ product = Openbeautyfacts::Product.get(code, locale: 'fr')
63
+
64
+ product.product_name
65
+ # => "Crousti Moelleux Complet"
66
+
67
+ product.nutriments.to_hash
68
+ # => {"sodium"=>"0.44", "sugars"=>6.5, "fat_unit"=>"g", "carbohydrates_unit"=>"g", "proteins_unit"=>"g", "nutrition-score-fr_100g"=>-2, "fat"=>2.5, "proteins_serving"=>12.8, "sodium_serving"=>0.535, "salt"=>1.1176, "proteins"=>10.5, "nutrition-score-uk_serving"=>-2, "nutrition-score-fr"=>-2, "fat_serving"=>3.04, "sugars_unit"=>"g", "sugars_100g"=>"6.5", "sodium_unit"=>"g", "saturated-fat_unit"=>"g", "saturated-fat_serving"=>0.608, "sodium_100g"=>0.44, "fiber_unit"=>"g", "energy"=>1067, "energy_unit"=>"kJ", "sugars_serving"=>7.9, "carbohydrates_100g"=>44, "nutrition-score-uk"=>-2, "proteins_100g"=>10.5, "fiber_serving"=>7.29, "carbohydrates_serving"=>53.5, "nutrition-score-fr_serving"=>-2, "energy_serving"=>1300, "fat_100g"=>"2.5", "saturated-fat_100g"=>"0.5", "nutrition-score-uk_100g"=>-2, "fiber"=>6, "salt_serving"=>1.36, "salt_100g"=>"1.1176", "carbohydrates"=>44, "fiber_100g"=>6, "energy_100g"=>1067, "saturated-fat"=>0.5}
69
+
70
+ # Update product
71
+
72
+ user = Openbeautyfacts::User.login("USERNAME", "PASSWORD")
73
+ if user
74
+ product.brands = "Jacquet"
75
+ product.update
76
+ # true
77
+ end
78
+
79
+ # Search products
80
+
81
+ products = Openbeautyfacts::Product.search("Chocolate", locale: 'world', page_size: 3)
82
+ # => [#<Openbeautyfacts::Product _id="3045140105502" code="3045140105502" id="3045140105502" image_small_url="http://en.openbeautyfacts.org/images/products/304/514/010/5502/front.7.100.jpg" lc="en" product_name="Milka au lait du Pays Alpin">, #<Openbeautyfacts::Product _id="3046920028363" code="3046920028363" id="3046920028363" image_small_url="http://en.openbeautyfacts.org/images/products/304/692/002/8363/front.5.100.jpg" lc="en" product_name="Tableta de chocolate negro \"Lindt Excellence\" 85% cacao">, #<Openbeautyfacts::Product _id="3046920029759" code="3046920029759" id="3046920029759" image_small_url="http://en.openbeautyfacts.org/images/products/304/692/002/9759/front.9.100.jpg" lc="en" product_name="Tableta de chocolate negro \"Lindt Excellence\" 90% cacao">]
83
+
84
+ # You might need to fetch full product data from results
85
+
86
+ products.first.fetch
87
+
88
+ # By product state
89
+
90
+ product_states = Openbeautyfacts::ProductState.all
91
+ product_states.last.products
92
+
93
+ # Config
94
+
95
+ Openbeautyfacts::Locale.all
96
+ # => ["ad", "ad-es", "ad-fr", "ad-pt", "ae", "al", "am", "ar", "at", "au", "ax", "az", "bd", "be", "be-de", "be-fr", "bf", "bg", "bi", "bi-rn", "bn", "br", "by", "by-be", "bz", "ca", "ca-fr", "cg", "ch", "ch-fr", "ch-it", "ci", "cl", "cn", "co", "cr", "cu", "cy", "cy-tr", "cz", "de", "dk", "do", "dz", "dz-fr", "ec", "eg", "es", "es-ca", "es-eu", "es-gl", "fi", "fi-sv", "fr", "ga", "gf", "gn", "gp", "gr", "hk", "hr", "hu", "id", "ie", "ie-ga", "il", "il-ar", "il-ru", "in", "iq", "iq-ku", "ir", "is", "it", "jp", "ke", "ke-sw", "kh", "kr", "kw", "kz", "kz-kk", "lb", "lk", "lk-ta", "lu", "lu-de", "lu-lb", "lv", "ma", "ma-es", "ma-fr", "mc", "md", "mf", "ml", "mn", "mo", "mo-zh", "mq", "mr", "mt", "mt-mt", "mu", "mx", "my", "nc", "nl", "no", "nz", "nz-mi", "pa", "pe", "pf", "ph", "ph-tl", "pl", "pm", "pt", "qa", "re", "ro", "rs", "ru", "sa", "se", "sg", "sg-ms", "sg-ta", "sg-zh", "si", "sk", "sn", "sy", "th", "tn", "tr", "tw", "tz", "tz-sw", "ua", "uk", "us", "ve", "vn", "vu", "vu-bi", "world", "ye", "yt", "za", "za-af", "za-nr", "za-ss", "za-st", "za-tn", "za-ts", "za-ve", "za-xh", "za-zu"]
97
+ ```
98
+
99
+ ## Development
100
+
101
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
102
+
103
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
104
+
105
+ ## Contributing
106
+
107
+ 1. Fork it ( https://github.com/openbeautyfacts/openbeautyfacts-ruby/fork )
108
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
109
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
110
+ 4. Push to the branch (`git push origin my-new-feature`)
111
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |task|
5
+ task.libs << "lib"
6
+ task.libs << "test"
7
+ task.pattern = "test/test_*.rb"
8
+ end
9
+
10
+ task default: :test
11
+
12
+ desc 'Load gem inside irb console'
13
+ task :console do
14
+ require 'irb'
15
+ require 'irb/completion'
16
+ require File.join(__FILE__, '../lib/openbeautyfacts')
17
+ ARGV.clear
18
+ IRB.start
19
+ end
@@ -0,0 +1,60 @@
1
+ require_relative 'openbeautyfacts/additive'
2
+ require_relative 'openbeautyfacts/brand'
3
+ require_relative 'openbeautyfacts/category'
4
+ require_relative 'openbeautyfacts/city'
5
+ require_relative 'openbeautyfacts/contributor'
6
+ require_relative 'openbeautyfacts/country'
7
+ require_relative 'openbeautyfacts/faq'
8
+ require_relative 'openbeautyfacts/ingredient_that_may_be_from_palm_oil'
9
+ require_relative 'openbeautyfacts/label'
10
+ require_relative 'openbeautyfacts/locale'
11
+ require_relative 'openbeautyfacts/manufacturing_place'
12
+ require_relative 'openbeautyfacts/origin'
13
+ require_relative 'openbeautyfacts/packager_code'
14
+ require_relative 'openbeautyfacts/packaging'
15
+ require_relative 'openbeautyfacts/press'
16
+ require_relative 'openbeautyfacts/product'
17
+ require_relative 'openbeautyfacts/product_state'
18
+ require_relative 'openbeautyfacts/purchase_place'
19
+ require_relative 'openbeautyfacts/store'
20
+ require_relative 'openbeautyfacts/trace'
21
+ require_relative 'openbeautyfacts/user'
22
+ require_relative 'openbeautyfacts/version'
23
+
24
+ require 'json'
25
+ require 'nokogiri'
26
+ require 'open-uri'
27
+
28
+ module Openbeautyfacts
29
+
30
+ DEFAULT_LOCALE = Locale::GLOBAL
31
+ DEFAULT_DOMAIN = 'openbeautyfacts.org'
32
+
33
+ class << self
34
+
35
+ # Return locale from link
36
+ #
37
+ def locale_from_link(link)
38
+ Locale.locale_from_link(link)
39
+ end
40
+
41
+ # Get locales
42
+ #
43
+ def locales
44
+ Locale.all
45
+ end
46
+
47
+ # Get product
48
+ #
49
+ def product(barcode, locale: DEFAULT_LOCALE)
50
+ Product.get(barcode, locale: locale)
51
+ end
52
+
53
+ # Return product API URL
54
+ #
55
+ def product_url(barcode, locale: DEFAULT_LOCALE)
56
+ Product.url(barcode, locale: locale)
57
+ end
58
+
59
+ end
60
+ end
@@ -0,0 +1,51 @@
1
+ require 'hashie'
2
+
3
+ module Openbeautyfacts
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: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
19
+ if path = LOCALE_PATHS[locale]
20
+ page_url = "http://#{locale}.#{domain}/#{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[3].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 Openbeautyfacts
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: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
19
+ if path = LOCALE_PATHS[locale]
20
+ Product.tags_from_page(self, "http://#{locale}.#{domain}/#{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 Openbeautyfacts
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: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
19
+ if path = LOCALE_PATHS[locale]
20
+ Product.tags_from_page(self, "http://#{locale}.#{domain}/#{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 Openbeautyfacts
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: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
19
+ if path = LOCALE_PATHS[locale]
20
+ Product.tags_from_page(self, "http://#{locale}.#{domain}/#{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 Openbeautyfacts
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: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
19
+ if path = LOCALE_PATHS[locale]
20
+ Product.tags_from_page(self, "http://#{locale}.#{domain}/#{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 Openbeautyfacts
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: DEFAULT_LOCALE, domain: DEFAULT_DOMAIN)
19
+ if path = LOCALE_PATHS[locale]
20
+ Product.tags_from_page(self, "http://#{locale}.#{domain}/#{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