Class: NutritionFacts::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/nutrition_facts/item.rb

Constant Summary collapse

@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item_list = {}) ⇒ Item

Returns a new instance of Item.



13
14
15
16
17
# File 'lib/nutrition_facts/item.rb', line 13

def initialize(item_list = {})
  item_list.each do |key, value|
    send("#{key}=", value)
  end
end

Instance Attribute Details

#hitsObject

Returns the value of attribute hits.



2
3
4
# File 'lib/nutrition_facts/item.rb', line 2

def hits
  @hits
end

#item_nameObject

Returns the value of attribute item_name.



2
3
4
# File 'lib/nutrition_facts/item.rb', line 2

def item_name
  @item_name
end

#max_scoreObject

Returns the value of attribute max_score.



2
3
4
# File 'lib/nutrition_facts/item.rb', line 2

def max_score
  @max_score
end

#nf_calcium_dvObject

Returns the value of attribute nf_calcium_dv.



2
3
4
# File 'lib/nutrition_facts/item.rb', line 2

def nf_calcium_dv
  @nf_calcium_dv
end

#nf_caloriesObject

Returns the value of attribute nf_calories.



2
3
4
# File 'lib/nutrition_facts/item.rb', line 2

def nf_calories
  @nf_calories
end

#nf_calories_from_fatObject

Returns the value of attribute nf_calories_from_fat.



2
3
4
# File 'lib/nutrition_facts/item.rb', line 2

def nf_calories_from_fat
  @nf_calories_from_fat
end

#nf_cholesterolObject

Returns the value of attribute nf_cholesterol.



2
3
4
# File 'lib/nutrition_facts/item.rb', line 2

def nf_cholesterol
  @nf_cholesterol
end

#nf_dietary_fiberObject

Returns the value of attribute nf_dietary_fiber.



2
3
4
# File 'lib/nutrition_facts/item.rb', line 2

def nf_dietary_fiber
  @nf_dietary_fiber
end

#nf_iron_dvObject

Returns the value of attribute nf_iron_dv.



2
3
4
# File 'lib/nutrition_facts/item.rb', line 2

def nf_iron_dv
  @nf_iron_dv
end

#nf_proteinObject

Returns the value of attribute nf_protein.



2
3
4
# File 'lib/nutrition_facts/item.rb', line 2

def nf_protein
  @nf_protein
end

#nf_saturated_fatObject

Returns the value of attribute nf_saturated_fat.



2
3
4
# File 'lib/nutrition_facts/item.rb', line 2

def nf_saturated_fat
  @nf_saturated_fat
end

#nf_serving_size_qtyObject

Returns the value of attribute nf_serving_size_qty.



2
3
4
# File 'lib/nutrition_facts/item.rb', line 2

def nf_serving_size_qty
  @nf_serving_size_qty
end

#nf_serving_size_unitObject

Returns the value of attribute nf_serving_size_unit.



2
3
4
# File 'lib/nutrition_facts/item.rb', line 2

def nf_serving_size_unit
  @nf_serving_size_unit
end

#nf_serving_weight_gramsObject

Returns the value of attribute nf_serving_weight_grams.



2
3
4
# File 'lib/nutrition_facts/item.rb', line 2

def nf_serving_weight_grams
  @nf_serving_weight_grams
end

#nf_sodiumObject

Returns the value of attribute nf_sodium.



2
3
4
# File 'lib/nutrition_facts/item.rb', line 2

def nf_sodium
  @nf_sodium
end

#nf_sugarsObject

Returns the value of attribute nf_sugars.



2
3
4
# File 'lib/nutrition_facts/item.rb', line 2

def nf_sugars
  @nf_sugars
end

#nf_total_carbohydrateObject

Returns the value of attribute nf_total_carbohydrate.



2
3
4
# File 'lib/nutrition_facts/item.rb', line 2

def nf_total_carbohydrate
  @nf_total_carbohydrate
end

#nf_total_fatObject

Returns the value of attribute nf_total_fat.



2
3
4
# File 'lib/nutrition_facts/item.rb', line 2

def nf_total_fat
  @nf_total_fat
end

#nf_vitamin_a_dvObject

Returns the value of attribute nf_vitamin_a_dv.



2
3
4
# File 'lib/nutrition_facts/item.rb', line 2

def nf_vitamin_a_dv
  @nf_vitamin_a_dv
end

#nf_vitamin_c_dvObject

Returns the value of attribute nf_vitamin_c_dv.



2
3
4
# File 'lib/nutrition_facts/item.rb', line 2

def nf_vitamin_c_dv
  @nf_vitamin_c_dv
end

#total_hitsObject

Returns the value of attribute total_hits.



2
3
4
# File 'lib/nutrition_facts/item.rb', line 2

def total_hits
  @total_hits
end

Class Method Details

.allObject



39
40
41
# File 'lib/nutrition_facts/item.rb', line 39

def self.all
  @@all
end

.find_by_name(name) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/nutrition_facts/item.rb', line 19

def self.find_by_name(name)
  uri = URI.parse("https://api.nutritionix.com/v1_1/search/#{name}?results=0%3A9&cal_min=0&cal_max=50000&fields=item_name%2Cnf_calories%2Cnf_total_fat%2Cnf_total_carbohydrate%2Cnf_dietary_fiber%2Cnf_sugars%2Cnf_protein%2Cnf_serving_weight_grams%2Cnf_cholesterol%2Cnf_saturated_fat%2Cnf_calories_from_fat%2Cnf_sodium%2Cnf_calcium_dv%2Cnf_iron_dv%2Cnf_vitamin_a_dv%2Cnf_vitamin_c_dv%2Cnf_serving_size_qty%2Cnf_serving_size_unit&appId=da276553&appKey=f501b79647768ba684af20428a44ef59")

  response = Net::HTTP.get_response(uri)

  raw_data = JSON.parse(response.body)

  if raw_data['hits'].empty?
    puts ''
  else
    i = 0
    num = raw_data['hits'].count
    while i < num
      @@all << NutritionFacts::Item.new(raw_data['hits'][i]['fields'])
      i += 1
    end
  end
  @@all
end

.resetObject



43
44
45
# File 'lib/nutrition_facts/item.rb', line 43

def self.reset
  @@all.clear
end