imc_calculadora 1.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ad96f32c5d3925e0fa529ab42ae682772a5a5c9a678f935989c6d35108e3d4f2
4
+ data.tar.gz: 056b74969467a1d64d36d408ee976df6ea6176b69c7016025f869e0a5db90f6d
5
+ SHA512:
6
+ metadata.gz: 22f33abe1538ce81174806597cafa1a970f799d4eedc8a5c4a5b4c915c779e3933902ff3d368f442d63bf1f12e9e499d0602324ebe279b43daa689302cf9f78b
7
+ data.tar.gz: 222c0382502981a602ef89d6d41fbcca3390e805b0e8194082759bc20ce99ee3829c91df451d270e1d136bf0f4c79d192b68584b5eab36d1da5cdce3ccde383d
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 IMC Calculadora
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # imc_calculadora
2
+
3
+ Calculadora IMC / BMI calculator for adults: body mass index with the WHO classification, the healthy-weight range for a height, metric and imperial input, and Spanish and English labels.
4
+
5
+ - **WHO adult classes** with the three thinness subclasses (severa, moderada, leve) and obesity classes I–III.
6
+ - **Rounds to 1 decimal before classifying**, the way BMI is reported: 24.96 → 25.0 → *Sobrepeso*. No more "24.96 is normal" edge cases.
7
+ - **Healthy-weight range** for a height (BMI 18.5 to 24.9) and how many kg are missing or extra.
8
+ - **Metric and imperial**: kg + cm (or metres, `1,75`), or lb + in using 703 · lb / in².
9
+ - **Comma or dot decimals** (`70,5` and `70.5` both work), handy for Spanish-language forms.
10
+ - **Spanish and English** labels and messages, with a not-medical-advice note.
11
+ - Library + CLI, no dependencies.
12
+
13
+ ## Install
14
+
15
+ ```
16
+ gem install imc_calculadora
17
+ ```
18
+
19
+ ## Library
20
+
21
+ ```ruby
22
+ require "imc_calculadora"
23
+
24
+ r = ImcCalculadora.calculate(weight_kg: 82, height: 175)
25
+ [r.bmi, r.key, r.diff_kg] # => [26.8, "sobrepeso", 5.7]
26
+
27
+ ImcCalculadora.calculate(weight_lb: 154, height_in: 69, lang: "en").label # => "Normal weight"
28
+ ImcCalculadora.classify(24.96).key # => "sobrepeso" (rounded to 25.0 first)
29
+ ImcCalculadora.healthy_range("1,68") # => [52.2, 70.3]
30
+ ```
31
+
32
+ ## CLI
33
+
34
+ ```
35
+ $ imc-calculadora 82 175
36
+ IMC 26,8: Sobrepeso
37
+ Tu IMC indica sobrepeso. Te sobran 5,7 kg respecto al rango normal.
38
+ Peso saludable: 56,7 - 76,3 kg
39
+ El IMC es una referencia general para adultos y no sustituye el consejo de un profesional de la salud.
40
+
41
+ $ imc-calculadora --lb 154 --in 69 --en
42
+ BMI 22.7: Normal weight
43
+ Your BMI is in the normal range for your height.
44
+ Healthy weight: 56.8 - 76.5 kg
45
+ BMI is a general guide for adults and is not medical advice.
46
+
47
+ $ imc-calculadora --rango 1,68
48
+ Peso saludable: 52,2 - 70,3 kg
49
+ ```
50
+
51
+ Add `--json` for machine-readable output.
52
+
53
+ ## WHO classes
54
+
55
+ | Key | Español | English | BMI (1 decimal) |
56
+ |---|---|---|---|
57
+ | `delgadez_severa` | Delgadez severa | Severe thinness | < 16.0 |
58
+ | `delgadez_moderada` | Delgadez moderada | Moderate thinness | 16.0 – 16.9 |
59
+ | `delgadez_leve` | Delgadez leve | Mild thinness | 17.0 – 18.4 |
60
+ | `normal` | Peso normal | Normal weight | 18.5 – 24.9 |
61
+ | `sobrepeso` | Sobrepeso | Overweight | 25.0 – 29.9 |
62
+ | `obesidad_1` | Obesidad grado I | Obesity class I | 30.0 – 34.9 |
63
+ | `obesidad_2` | Obesidad grado II | Obesity class II | 35.0 – 39.9 |
64
+ | `obesidad_3` | Obesidad grado III | Obesity class III | ≥ 40.0 |
65
+
66
+ The BMI is rounded half up to 1 decimal first, then compared with these bounds.
67
+
68
+ ## Note
69
+
70
+ BMI is a general screening measure for adults (18+). It does not tell muscle from fat and is not medical advice. For children and teens, use age- and sex-specific percentiles.
71
+
72
+ ## Links
73
+
74
+ - [Calculadora IMC](https://imccalculadora.app/): free online BMI calculator in Spanish with the WHO table, healthy-weight range and methodology.
75
+ - Source: https://github.com/nbkdevs/imc-calculadora
76
+
77
+ ## License
78
+
79
+ MIT
@@ -0,0 +1,57 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "json"
5
+ require_relative "../lib/imc_calculadora"
6
+
7
+ HELP = <<~TXT
8
+ imc-calculadora: adult BMI (IMC) with WHO classes
9
+
10
+ Usage:
11
+ imc-calculadora <peso-kg> <altura-cm|m> [--en] [--json]
12
+ imc-calculadora --lb <peso-lb> --in <altura-in> [--en] [--json]
13
+ imc-calculadora --rango <altura-cm|m> [--en]
14
+ TXT
15
+
16
+ args = ARGV.dup
17
+ if args.empty? || args.include?("-h") || args.include?("--help")
18
+ puts HELP
19
+ exit 0
20
+ end
21
+ en = !args.delete("--en").nil?
22
+ json = !args.delete("--json").nil?
23
+ lang = en ? "en" : "es"
24
+ take = lambda do |flag|
25
+ i = args.index(flag)
26
+ next nil unless i
27
+
28
+ v = args[i + 1] || ""
29
+ args.slice!(i, 2)
30
+ v
31
+ end
32
+ hw = en ? "Healthy weight" : "Peso saludable"
33
+ f = ->(n) { ImcCalculadora.format(n, lang) }
34
+ begin
35
+ rango = take.call("--rango")
36
+ if rango
37
+ lo, hi = ImcCalculadora.healthy_range(rango)
38
+ puts json ? JSON.generate(min_kg: lo, max_kg: hi) : "#{hw}: #{f.call(lo)} - #{f.call(hi)} kg"
39
+ exit 0
40
+ end
41
+ lb = take.call("--lb")
42
+ inch = take.call("--in")
43
+ r = if lb || inch
44
+ ImcCalculadora.calculate(weight_lb: lb || "", height_in: inch || "", lang: lang)
45
+ else
46
+ ImcCalculadora.calculate(weight_kg: args[0] || "", height: args[1] || "", lang: lang)
47
+ end
48
+ if json
49
+ puts JSON.pretty_generate(r.to_h)
50
+ else
51
+ puts "#{en ? 'BMI' : 'IMC'} #{f.call(r.bmi)}: #{r.label}", r.message,
52
+ "#{hw}: #{f.call(r.min_kg)} - #{f.call(r.max_kg)} kg", r.disclaimer
53
+ end
54
+ rescue ArgumentError => e
55
+ warn "error: #{e.message}\n\n#{HELP}"
56
+ exit 2
57
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ImcCalculadora
4
+ VERSION = "1.0.0"
5
+ end
@@ -0,0 +1,136 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "imc_calculadora/version"
4
+
5
+ # Adult BMI (IMC) with WHO classes, healthy-weight range, metric/imperial input
6
+ # and Spanish/English labels. The BMI is rounded to 1 decimal BEFORE it is
7
+ # classified (24.96 -> 25.0 -> overweight).
8
+ module ImcCalculadora
9
+ LB_KG = 0.45359237
10
+ IN_CM = 2.54
11
+
12
+ ImcClass = Struct.new(:key, :upper, :es, :en, :group)
13
+ Category = Struct.new(:key, :label, :group, :bmi)
14
+ Result = Struct.new(:bmi, :key, :label, :group, :min_kg, :max_kg, :diff_kg, :message, :disclaimer, keyword_init: true)
15
+
16
+ CLASSES = [
17
+ ImcClass.new("delgadez_severa", 16.0, "Delgadez severa", "Severe thinness", "bajo"),
18
+ ImcClass.new("delgadez_moderada", 17.0, "Delgadez moderada", "Moderate thinness", "bajo"),
19
+ ImcClass.new("delgadez_leve", 18.5, "Delgadez leve", "Mild thinness", "bajo"),
20
+ ImcClass.new("normal", 25.0, "Peso normal", "Normal weight", "normal"),
21
+ ImcClass.new("sobrepeso", 30.0, "Sobrepeso", "Overweight", "sobrepeso"),
22
+ ImcClass.new("obesidad_1", 35.0, "Obesidad grado I", "Obesity class I", "obesidad"),
23
+ ImcClass.new("obesidad_2", 40.0, "Obesidad grado II", "Obesity class II", "obesidad"),
24
+ ImcClass.new("obesidad_3", Float::INFINITY, "Obesidad grado III", "Obesity class III", "obesidad")
25
+ ].freeze
26
+
27
+ DISCLAIMER = {
28
+ "es" => "El IMC es una referencia general para adultos y no sustituye el consejo de un profesional de la salud.",
29
+ "en" => "BMI is a general guide for adults and is not medical advice."
30
+ }.freeze
31
+
32
+ NUM = /\A\+?(\d+(\.\d+)?|\.\d+)\z/
33
+
34
+ module_function
35
+
36
+ # Round half up to 1 decimal.
37
+ def round1(x)
38
+ ((x * 10) + 0.5 + 1e-9).floor / 10.0
39
+ end
40
+
41
+ # Parse "70,5", "70.5" or a Numeric. Raises ArgumentError otherwise.
42
+ def parse_number(v)
43
+ return v.to_f if v.is_a?(Numeric) && v.to_f.finite?
44
+
45
+ s = v.to_s.strip.sub(",", ".")
46
+ raise ArgumentError, "invalid number: #{v}" unless NUM.match?(s)
47
+
48
+ s.to_f
49
+ end
50
+
51
+ # Height in cm; values below 3 are taken as metres (1,75 -> 175).
52
+ def height_cm(h)
53
+ n = parse_number(h)
54
+ n < 3 ? n * 100 : n
55
+ end
56
+
57
+ def lang_of(lang)
58
+ lang.to_s == "en" ? "en" : "es"
59
+ end
60
+
61
+ # WHO class for a BMI; the value is rounded to 1 decimal first.
62
+ def classify(bmi, lang: "es")
63
+ b = round1(parse_number(bmi))
64
+ c = CLASSES.find { |k| b < k.upper }
65
+ Category.new(c.key, lang_of(lang) == "en" ? c.en : c.es, c.group, b)
66
+ end
67
+
68
+ # Raw BMI from kg and cm (or m).
69
+ def bmi(weight_kg, height)
70
+ w = parse_number(weight_kg)
71
+ h = height_cm(height) / 100.0
72
+ raise ArgumentError, "weight and height must be greater than 0" unless w.positive? && h.positive?
73
+
74
+ w / (h * h)
75
+ end
76
+
77
+ # Raw BMI from pounds and inches: 703 * lb / in^2.
78
+ def bmi_imperial(weight_lb, height_in)
79
+ w = parse_number(weight_lb)
80
+ h = parse_number(height_in)
81
+ raise ArgumentError, "weight and height must be greater than 0" unless w.positive? && h.positive?
82
+
83
+ 703 * w / (h * h)
84
+ end
85
+
86
+ # Healthy-weight range (BMI 18.5 to 24.9) in kg, 1 decimal: [min, max].
87
+ def healthy_range(height)
88
+ h = height_cm(height) / 100.0
89
+ raise ArgumentError, "height must be greater than 0" unless h.positive?
90
+
91
+ [round1(18.5 * h * h), round1(24.9 * h * h)]
92
+ end
93
+
94
+ def format(n, lang)
95
+ s = Kernel.format("%.1f", n)
96
+ lang_of(lang) == "en" ? s : s.tr(".", ",")
97
+ end
98
+
99
+ def message(cat, diff, lang)
100
+ d = format(diff, lang)
101
+ low = cat.label.downcase
102
+ if lang == "en"
103
+ return "Your BMI is in the normal range for your height." if cat.group == "normal"
104
+
105
+ side = cat.group == "bajo" ? "below" : "above"
106
+ return "Your BMI shows #{low}. You are #{d} kg #{side} the normal range."
107
+ end
108
+ return "Tu IMC está dentro del rango normal para tu estatura." if cat.group == "normal"
109
+ return "Tu IMC indica #{low}. Te faltan #{d} kg para llegar al rango normal." if cat.group == "bajo"
110
+
111
+ "Tu IMC indica #{low}. Te sobran #{d} kg respecto al rango normal."
112
+ end
113
+
114
+ # Full result from weight_kg + height (cm or m), or weight_lb + height_in.
115
+ def calculate(weight_kg: nil, height: nil, weight_lb: nil, height_in: nil, lang: "es")
116
+ lang = lang_of(lang)
117
+ if weight_lb || height_in
118
+ raw = bmi_imperial(weight_lb, height_in)
119
+ kg = parse_number(weight_lb) * LB_KG
120
+ cm = parse_number(height_in) * IN_CM
121
+ else
122
+ raw = bmi(weight_kg, height)
123
+ kg = parse_number(weight_kg)
124
+ cm = height_cm(height)
125
+ end
126
+ cat = classify(raw, lang: lang)
127
+ lo, hi = healthy_range(cm)
128
+ diff = case cat.group
129
+ when "bajo" then round1([lo - kg, 0].max)
130
+ when "normal" then 0.0
131
+ else round1([kg - hi, 0].max)
132
+ end
133
+ Result.new(bmi: cat.bmi, key: cat.key, label: cat.label, group: cat.group, min_kg: lo, max_kg: hi,
134
+ diff_kg: diff, message: message(cat, diff, lang), disclaimer: DISCLAIMER[lang])
135
+ end
136
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: imc_calculadora
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - IMC Calculadora
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: Adult BMI (IMC) with WHO classes including thinness subclasses, 1-decimal
13
+ rounding before classifying, healthy-weight range, kg/cm and lb/in input, comma
14
+ decimals, Spanish and English labels. Library and CLI.
15
+ executables:
16
+ - imc-calculadora
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE
21
+ - README.md
22
+ - exe/imc-calculadora
23
+ - lib/imc_calculadora.rb
24
+ - lib/imc_calculadora/version.rb
25
+ homepage: https://imccalculadora.app/
26
+ licenses:
27
+ - MIT
28
+ metadata:
29
+ homepage_uri: https://imccalculadora.app/
30
+ source_code_uri: https://github.com/nbkdevs/imc-calculadora/tree/main/packages/rubygems
31
+ bug_tracker_uri: https://github.com/nbkdevs/imc-calculadora/issues
32
+ rubygems_mfa_required: 'true'
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 3.0.0
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubygems_version: 4.0.20
48
+ specification_version: 4
49
+ summary: 'Calculadora IMC / BMI calculator: WHO classes, healthy-weight range, metric
50
+ and imperial, ES/EN.'
51
+ test_files: []