gold-karat-value 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.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +60 -0
- data/lib/gold_karat_value/version.rb +5 -0
- data/lib/gold_karat_value.rb +86 -0
- metadata +52 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 34951e42ea5f3c6429d885af49459c464048da0e1ebc6ce220e5fd1c24f9357a
|
|
4
|
+
data.tar.gz: 61940330e20b042f7184342b1ec56ba5d96c30c739de12ad255e5c59c3911f7f
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 13f3d09187b4b733779638a307dbc3fb23832d2dc4eef4afbb8b7ecb888378cb538adf826c64b06ff990fa3e5b3ee535d6414bffa2427e942361a72d1e26ee98
|
|
7
|
+
data.tar.gz: aa9504bbdcce04c0148d14cdd1fca79f5fcbdc5569950b10fe5224dea1dda916369948c13c571d67816fb857f59d59ce1a80726e89c6245b03e26aaf322d715f
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 gold-karat-value contributors
|
|
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,60 @@
|
|
|
1
|
+
# gold-karat-value
|
|
2
|
+
|
|
3
|
+
Estimate gold melt value from karat, weight, and a 24K price per gram.
|
|
4
|
+
|
|
5
|
+
Jewelry and scrap workflows usually start from the same three inputs: how heavy the piece is, what karat stamp it carries, and what fine gold is trading at today. This gem keeps that math in one place so a script or backend can reuse the same rounding rules.
|
|
6
|
+
|
|
7
|
+
It does **not** call a price API. You pass in the 24K price you already have (spot feed, cached quote, or a number typed by the user).
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```ruby
|
|
12
|
+
gem "gold-karat-value", "~> 0.1"
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
gem install gold-karat-value
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```ruby
|
|
22
|
+
require "gold_karat_value"
|
|
23
|
+
|
|
24
|
+
melt = GoldKaratValue.melt_value(
|
|
25
|
+
weight: 10,
|
|
26
|
+
unit: :gram,
|
|
27
|
+
karat: 14,
|
|
28
|
+
price_24k_per_gram: 65
|
|
29
|
+
)
|
|
30
|
+
puts format("14K melt: $%.2f ($%.2f/g)", melt.total, melt.per_gram)
|
|
31
|
+
|
|
32
|
+
scrap = GoldKaratValue.scrap_value(
|
|
33
|
+
weight: 10,
|
|
34
|
+
unit: :gram,
|
|
35
|
+
karat: 14,
|
|
36
|
+
price_24k_per_gram: 65,
|
|
37
|
+
discount: 0.85
|
|
38
|
+
)
|
|
39
|
+
puts format("85%% scrap offer: $%.2f", scrap.scrap)
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Units: `:gram`, `:pennyweight`, `:troy_ounce`, `:troy_pound`, `:kilogram`. Karat must be between `0` and `24` (including fractional stamps such as `21.6`).
|
|
43
|
+
|
|
44
|
+
## Formula
|
|
45
|
+
|
|
46
|
+
1. Convert the entered weight to grams.
|
|
47
|
+
2. Purity ratio = karat ÷ 24 (14K ≈ 0.5833).
|
|
48
|
+
3. Price per gram at that karat = 24K price per gram × ratio, rounded to cents.
|
|
49
|
+
4. Melt value = grams × that per-gram price, rounded to cents.
|
|
50
|
+
5. Scrap estimate = melt value × a payout ratio between 0 and 1 (for example 0.50 at a pawn shop, 0.85 for some online buyers).
|
|
51
|
+
|
|
52
|
+
The same cent-rounding is applied to per-gram, per-troy-ounce, and per-troy-pound quotes so table output stays consistent with a UI.
|
|
53
|
+
|
|
54
|
+
## When you need live prices
|
|
55
|
+
|
|
56
|
+
This library stops at the arithmetic. If you want the same karat and unit conversion with a regularly updated spot price in the browser, there is a [gold calculator](https://mygoldcalc.com/) that applies these ratios without an account.
|
|
57
|
+
|
|
58
|
+
## License
|
|
59
|
+
|
|
60
|
+
MIT
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "gold_karat_value/version"
|
|
4
|
+
|
|
5
|
+
# Estimate gold melt and scrap value from karat, weight, and a 24K price per gram.
|
|
6
|
+
#
|
|
7
|
+
# This library does not fetch live market data. Pass in a spot price you already
|
|
8
|
+
# have; the helpers convert units, apply karat purity, and round amounts to cents.
|
|
9
|
+
module GoldKaratValue
|
|
10
|
+
GRAMS_PER_TROY_OUNCE = 31.1035
|
|
11
|
+
GRAMS_PER_PENNYWEIGHT = 1.55517384
|
|
12
|
+
GRAMS_PER_TROY_POUND = 373.242
|
|
13
|
+
|
|
14
|
+
WEIGHT_TO_GRAMS = {
|
|
15
|
+
gram: 1.0,
|
|
16
|
+
pennyweight: GRAMS_PER_PENNYWEIGHT,
|
|
17
|
+
troy_ounce: GRAMS_PER_TROY_OUNCE,
|
|
18
|
+
troy_pound: GRAMS_PER_TROY_POUND,
|
|
19
|
+
kilogram: 1000.0
|
|
20
|
+
}.freeze
|
|
21
|
+
|
|
22
|
+
MeltValue = Struct.new(:total, :per_gram, :per_troy_ounce, :per_troy_pound, keyword_init: true)
|
|
23
|
+
ScrapValue = Struct.new(:market, :scrap, keyword_init: true)
|
|
24
|
+
|
|
25
|
+
class Error < StandardError; end
|
|
26
|
+
class NegativeWeight < Error; end
|
|
27
|
+
class InvalidKarat < Error; end
|
|
28
|
+
class NegativePrice < Error; end
|
|
29
|
+
class InvalidDiscount < Error; end
|
|
30
|
+
class UnknownUnit < Error; end
|
|
31
|
+
|
|
32
|
+
module_function
|
|
33
|
+
|
|
34
|
+
def purity_ratio(karat)
|
|
35
|
+
karat = Float(karat)
|
|
36
|
+
raise InvalidKarat, "karat must be between 0 and 24" unless karat.finite? && karat >= 0 && karat <= 24
|
|
37
|
+
|
|
38
|
+
karat / 24.0
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def to_grams(weight, unit)
|
|
42
|
+
weight = require_non_negative(weight, NegativeWeight)
|
|
43
|
+
factor = WEIGHT_TO_GRAMS[unit.to_sym]
|
|
44
|
+
raise UnknownUnit, "unknown unit: #{unit}" unless factor
|
|
45
|
+
|
|
46
|
+
weight * factor
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def round_cents(amount)
|
|
50
|
+
(Float(amount) * 100.0).round / 100.0
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def melt_value(weight:, unit:, karat:, price_24k_per_gram:)
|
|
54
|
+
grams = to_grams(weight, unit)
|
|
55
|
+
price = require_non_negative(price_24k_per_gram, NegativePrice)
|
|
56
|
+
per_gram = round_cents(price * purity_ratio(karat))
|
|
57
|
+
|
|
58
|
+
MeltValue.new(
|
|
59
|
+
total: round_cents(grams * per_gram),
|
|
60
|
+
per_gram: per_gram,
|
|
61
|
+
per_troy_ounce: round_cents(per_gram * GRAMS_PER_TROY_OUNCE),
|
|
62
|
+
per_troy_pound: round_cents(per_gram * GRAMS_PER_TROY_POUND)
|
|
63
|
+
)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def scrap_value(weight:, unit:, karat:, price_24k_per_gram:, discount:)
|
|
67
|
+
discount = Float(discount)
|
|
68
|
+
raise InvalidDiscount, "discount must be between 0 and 1" unless discount.finite? && discount >= 0 && discount <= 1
|
|
69
|
+
|
|
70
|
+
melt = melt_value(
|
|
71
|
+
weight: weight,
|
|
72
|
+
unit: unit,
|
|
73
|
+
karat: karat,
|
|
74
|
+
price_24k_per_gram: price_24k_per_gram
|
|
75
|
+
)
|
|
76
|
+
ScrapValue.new(market: melt.total, scrap: round_cents(melt.total * discount))
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def require_non_negative(value, error_class)
|
|
80
|
+
value = Float(value)
|
|
81
|
+
raise error_class, "value must be finite and non-negative" unless value.finite? && value >= 0
|
|
82
|
+
|
|
83
|
+
value
|
|
84
|
+
end
|
|
85
|
+
private_class_method :require_non_negative
|
|
86
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: gold-karat-value
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- hiltonlee981
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-09-08 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: 'Small Ruby helper for jewelry and scrap workflows: convert weight units,
|
|
14
|
+
apply karat purity, and round melt/scrap quotes to cents. You supply the 24K spot
|
|
15
|
+
price.'
|
|
16
|
+
email:
|
|
17
|
+
- hiltonlee981@gmail.com
|
|
18
|
+
executables: []
|
|
19
|
+
extensions: []
|
|
20
|
+
extra_rdoc_files: []
|
|
21
|
+
files:
|
|
22
|
+
- LICENSE
|
|
23
|
+
- README.md
|
|
24
|
+
- lib/gold_karat_value.rb
|
|
25
|
+
- lib/gold_karat_value/version.rb
|
|
26
|
+
homepage: https://mygoldcalc.com/
|
|
27
|
+
licenses:
|
|
28
|
+
- MIT
|
|
29
|
+
metadata:
|
|
30
|
+
homepage_uri: https://mygoldcalc.com/
|
|
31
|
+
rubygems_mfa_required: 'false'
|
|
32
|
+
post_install_message:
|
|
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'
|
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
42
|
+
requirements:
|
|
43
|
+
- - ">="
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: '0'
|
|
46
|
+
requirements: []
|
|
47
|
+
rubygems_version: 3.5.22
|
|
48
|
+
signing_key:
|
|
49
|
+
specification_version: 4
|
|
50
|
+
summary: Estimate gold melt and scrap value from karat, weight, and a 24K price per
|
|
51
|
+
gram.
|
|
52
|
+
test_files: []
|