foodnutritionix 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 882a705e0599afa368a316113efcd50d9168183b
4
- data.tar.gz: 859ae595073cde80a69fc340af2552d55ea64718
3
+ metadata.gz: 32924569f95bfa9ace691b227eceb2333fa26a37
4
+ data.tar.gz: 105c8e3d6d76752939e32db1e128591438ad9c42
5
5
  SHA512:
6
- metadata.gz: 228cd67c857681808baed2c07a80af1dd8d1e01848b432a5871e01558416523b923b4f5540a327e3595120e47042123b5e1086808cb884b7cfbbda729aeab1af
7
- data.tar.gz: f031151fcfbab0346aaf556117c66d56c3c6cda0ee1f5f410a9bcef03dc9433dfe2e63709510c711308e89c26cde77393b1ade7c33f9a7ce2e63374c0829246b
6
+ metadata.gz: dadfc90fe834b9ed3cff14e2f4d238a2b4e678bb8e997d0920c777abb2845ac9be2a289a6d4e3ef0d4b098de2400fa6f1e01d90ae3890cbaafed025ec6f21e4e
7
+ data.tar.gz: 5a5fd8da378208266205556b463966e69694941790e48ebf4a3165422ed317a441954a6d425559904dca2cc3bfd5570617a456904b2273bde26b65db6c870075
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+
4
+ Metrics/LineLength:
5
+ Max: 120
6
+
7
+ Documentation:
8
+ Enabled: false
@@ -1,30 +1,51 @@
1
1
  # frozen_string_literal: true
2
2
  module FoodNutritionix
3
3
  class Food
4
- attr_reader :food_name, :consumed_at, :serving_qty, :serving_unit, :serving_weight_grams, :nf_calories, :nf_total_fat, :nf_saturated_fat, :nf_cholesterol, :nf_sodium, :nf_total_carbohydrate, :nf_dietary_fiber, :nf_sugars, :nf_protein, :nf_potassium, :photo
4
+ attr_reader :name,
5
+ :serving_quantity,
6
+ :serving_unit,
7
+ :serving_weight,
8
+ :calories,
9
+ :total_fat,
10
+ :saturated_fat,
11
+ :cholesterol,
12
+ :sodium,
13
+ :carbohydrate,
14
+ :dietary_fiber,
15
+ :sugars,
16
+ :protein,
17
+ :potassium,
18
+ :photo
5
19
 
6
20
  def initialize(data)
7
- @food_name = data['food_name']
8
- @consumed_at = DateTime.parse(data['consumed_at'])
9
- @serving_qty = data['serving_qty']
10
- @serving_unit = data['serving_unit']
11
- @serving_weight_grams = data['serving_weight_grams']
12
- @nf_calories = data['nf_calories']
13
- @nf_total_fat = data['nf_total_fat']
14
- @nf_saturated_fat = data['nf_saturated_fat']
15
- @nf_cholesterol = data['nf_cholesterol']
16
- @nf_sodium = data['nf_sodium']
17
- @nf_total_carbohydrate = data['nf_total_carbohydrate']
18
- @nf_dietary_fiber = data['nf_dietary_fiber']
19
- @nf_sugars = data['nf_sugars']
20
- @nf_protein = data['nf_protein']
21
- @nf_potassium = data['nf_potassium']
21
+ @name = data['food_name']
22
+ populate_serving_information(data)
23
+ populate_nutrition_facts(data)
22
24
  @photo = data['photo']
23
25
  end
24
26
 
25
- def self.search(*tags)
26
- data = FoodNutrixClient.search_foods(tags)
27
- new(data)
27
+ def populate_serving_information(data)
28
+ @serving_quantity = data['serving_qty']
29
+ @serving_unit = data['serving_unit']
30
+ @serving_weight = data['serving_weight_grams']
31
+ end
32
+
33
+ def populate_nutrition_facts(data)
34
+ @calories = data.fetch('nf_calories', 0.0)
35
+ @fat = data.fetch('nf_total_fat', 0.0)
36
+ @saturated_fat = data.fetch('nf_saturated_fat', 0.0)
37
+ @cholesterol = data.fetch('nf_cholesterol', 0.0)
38
+ @sodium = data.fetch('nf_sodium', 0.0)
39
+ @carbohydrate = data.fetch('nf_total_carbohydrate', 0.0)
40
+ @dietary_fiber = data.fetch('nf_dietary_fiber', 0.0)
41
+ @sugars = data.fetch('nf_sugars', 0.0)
42
+ @protein = data.fetch('nf_protein', 0.0)
43
+ @potassium = data.fetch('nf_potassium', 0.0)
44
+ end
45
+
46
+ def self.search(*names)
47
+ FoodNutrixClient.search_foods(names)
48
+ &.map { |food| new(food) }
28
49
  end
29
50
  end
30
51
  end
@@ -18,14 +18,18 @@ module FoodNutritionix
18
18
  end
19
19
 
20
20
  def self.search_foods(*foods)
21
- HTTParty.post(SEARCH_FOOD_ENDPOINT,
22
- headers: authorization_header,
23
- body: { 'query': foods.join(' ') })
24
- .parsed_response['foods'][0]
21
+ result = HTTParty.post(SEARCH_FOOD_ENDPOINT,
22
+ headers: authorization_header,
23
+ body: { query: foods.join(' ') })
24
+ .parsed_response
25
+ result['foods']
25
26
  end
26
27
 
27
28
  def self.authorization_header
28
- @authorization_header ||= { 'x-app-id': "#{config[:x_app_id]}", 'x-app-key': "#{config[:x_app_key]}" }
29
+ @authorization_header ||= {
30
+ 'x-app-id' => config[:x_app_id].to_s,
31
+ 'x-app-key' => config[:x_app_key].to_s
32
+ }
29
33
  end
30
34
  end
31
35
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FoodNutritionix
4
- VERSION = '0.1.1'
4
+ VERSION = '0.1.3'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foodnutritionix
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leo Lee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-11 00:00:00.000000000 Z
11
+ date: 2017-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -186,6 +186,7 @@ extensions: []
186
186
  extra_rdoc_files: []
187
187
  files:
188
188
  - ".gitignore"
189
+ - ".rubocop.yml"
189
190
  - Gemfile
190
191
  - Gemfile.lock
191
192
  - LICENSE.md
@@ -223,7 +224,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
223
224
  version: '0'
224
225
  requirements: []
225
226
  rubyforge_project:
226
- rubygems_version: 2.5.1
227
+ rubygems_version: 2.5.2
227
228
  signing_key:
228
229
  specification_version: 4
229
230
  summary: Get foods nutrients by search it name