Class: Mindee::Product::NutritionFactsLabel::NutritionFactsLabelV1Nutrient

Inherits:
Mindee::Parsing::Standard::FeatureField show all
Includes:
Mindee::Parsing::Standard
Defined in:
lib/mindee/product/nutrition_facts_label/nutrition_facts_label_v1_nutrient.rb

Overview

The amount of nutrients in the product.

Instance Attribute Summary collapse

Attributes inherited from Mindee::Parsing::Standard::AbstractField

#bounding_box, #confidence, #page_id, #polygon

Instance Method Summary collapse

Methods inherited from Mindee::Parsing::Standard::FeatureField

#format_for_display

Methods inherited from Mindee::Parsing::Standard::AbstractField

array_confidence, array_sum, float_to_string

Constructor Details

#initialize(prediction, page_id) ⇒ NutritionFactsLabelV1Nutrient

Returns a new instance of NutritionFactsLabelV1Nutrient.

Parameters:

  • prediction (Hash)
  • page_id (Integer, nil)


29
30
31
32
33
34
35
36
37
# File 'lib/mindee/product/nutrition_facts_label/nutrition_facts_label_v1_nutrient.rb', line 29

def initialize(prediction, page_id)
  super(prediction, page_id)
  @daily_value = prediction['daily_value']
  @name = prediction['name']
  @per_100g = prediction['per_100g']
  @per_serving = prediction['per_serving']
  @unit = prediction['unit']
  @page_id = page_id
end

Instance Attribute Details

#daily_valueFloat (readonly)

DVs are the recommended amounts of nutrients to consume or not to exceed each day.

Returns:

  • (Float)


13
14
15
# File 'lib/mindee/product/nutrition_facts_label/nutrition_facts_label_v1_nutrient.rb', line 13

def daily_value
  @daily_value
end

#nameString (readonly)

The name of nutrients of the product.

Returns:

  • (String)


16
17
18
# File 'lib/mindee/product/nutrition_facts_label/nutrition_facts_label_v1_nutrient.rb', line 16

def name
  @name
end

#per_100gFloat (readonly)

The amount of nutrients per 100g of the product.

Returns:

  • (Float)


19
20
21
# File 'lib/mindee/product/nutrition_facts_label/nutrition_facts_label_v1_nutrient.rb', line 19

def per_100g
  @per_100g
end

#per_servingFloat (readonly)

The amount of nutrients per serving of the product.

Returns:

  • (Float)


22
23
24
# File 'lib/mindee/product/nutrition_facts_label/nutrition_facts_label_v1_nutrient.rb', line 22

def per_serving
  @per_serving
end

#unitString (readonly)

The unit of measurement for the amount of nutrients.

Returns:

  • (String)


25
26
27
# File 'lib/mindee/product/nutrition_facts_label/nutrition_facts_label_v1_nutrient.rb', line 25

def unit
  @unit
end

Instance Method Details

#printable_valuesHash

Returns:

  • (Hash)


40
41
42
43
44
45
46
47
48
# File 'lib/mindee/product/nutrition_facts_label/nutrition_facts_label_v1_nutrient.rb', line 40

def printable_values
  printable = {}
  printable[:daily_value] = @daily_value.nil? ? '' : Field.float_to_string(@daily_value)
  printable[:name] = format_for_display(@name)
  printable[:per_100g] = @per_100g.nil? ? '' : Field.float_to_string(@per_100g)
  printable[:per_serving] = @per_serving.nil? ? '' : Field.float_to_string(@per_serving)
  printable[:unit] = format_for_display(@unit)
  printable
end

#table_printable_valuesHash

Returns:

  • (Hash)


51
52
53
54
55
56
57
58
59
# File 'lib/mindee/product/nutrition_facts_label/nutrition_facts_label_v1_nutrient.rb', line 51

def table_printable_values
  printable = {}
  printable[:daily_value] = @daily_value.nil? ? '' : Field.float_to_string(@daily_value)
  printable[:name] = format_for_display(@name, 20)
  printable[:per_100g] = @per_100g.nil? ? '' : Field.float_to_string(@per_100g)
  printable[:per_serving] = @per_serving.nil? ? '' : Field.float_to_string(@per_serving)
  printable[:unit] = format_for_display(@unit, nil)
  printable
end

#to_sString

Returns:

  • (String)


74
75
76
77
78
79
80
81
82
83
# File 'lib/mindee/product/nutrition_facts_label/nutrition_facts_label_v1_nutrient.rb', line 74

def to_s
  printable = printable_values
  out_str = String.new
  out_str << "\n  :Daily Value: #{printable[:daily_value]}"
  out_str << "\n  :Name: #{printable[:name]}"
  out_str << "\n  :Per 100g: #{printable[:per_100g]}"
  out_str << "\n  :Per Serving: #{printable[:per_serving]}"
  out_str << "\n  :Unit: #{printable[:unit]}"
  out_str
end

#to_table_lineString

Returns:

  • (String)


62
63
64
65
66
67
68
69
70
71
# File 'lib/mindee/product/nutrition_facts_label/nutrition_facts_label_v1_nutrient.rb', line 62

def to_table_line
  printable = table_printable_values
  out_str = String.new
  out_str << format('| %- 12s', printable[:daily_value])
  out_str << format('| %- 21s', printable[:name])
  out_str << format('| %- 9s', printable[:per_100g])
  out_str << format('| %- 12s', printable[:per_serving])
  out_str << format('| %- 5s', printable[:unit])
  out_str << '|'
end