us_income_tax 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f115ec7ed006452ac37bdb59de762cd8764e7ed8b1981b80effa022f9007fb8e
4
+ data.tar.gz: fc8db78f200213c505f1f9dfeb34003a755ad7bf91b6a379fbc580442779ebc2
5
+ SHA512:
6
+ metadata.gz: 05417cd8ead5f4fc3c8e51279334c19a8d64d64a0f20262eb5b4c7754c8653afd4c9aa0e9669399da9eaca9817c9cdcea5849e749ef63da106cb00581b93ebe8
7
+ data.tar.gz: a9fcea4a5ecf647238710a39ffd1af1fb8d991a3f3bec9bad94258f2fe2bacfc9f34af9eb21d39de0cdd2ce5a950822784a15144feb3aa1ced044eee45afc63b
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.0
3
+
4
+ Style/StringLiterals:
5
+ EnforcedStyle: double_quotes
6
+
7
+ Style/StringLiteralsInInterpolation:
8
+ EnforcedStyle: double_quotes
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-01-06
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Jason Kim
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # USIncomeTax
2
+
3
+ Calculate US Federal Income Tax
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/us_income_tax`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add us_income_tax
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install us_income_tax
16
+
17
+ ## Usage
18
+
19
+ ```ruby
20
+ year = 2024 # Supports 2024 and 2025 only
21
+ gross_income = 777777
22
+ type = :single # Supports :single, :married_filing_jointly, :married_filing_separately, :head_of_household
23
+
24
+ calculation_result = Calculator.calculate(year, gross_income, type)
25
+
26
+ tax_brackets = calculation_result.brackets[calculation_result.year.to_s][calculation_result.type]
27
+ total_tax = calculation_result.total_tax
28
+ net_income = calculation_result.net_income
29
+ ```
30
+
31
+ ## Development
32
+
33
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
34
+
35
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
36
+
37
+ ## Links
38
+
39
+ - [2024 and 2025 tax brackets and federal income tax rates](https://www.fidelity.com/learning-center/personal-finance/tax-brackets)
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/serv/us_income_tax.
44
+
45
+ ## License
46
+
47
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[test rubocop]
@@ -0,0 +1,78 @@
1
+ {
2
+ "2024": {
3
+ "single": [
4
+ { "rate": 10, "floor": 0, "ceiling": 11600 },
5
+ { "rate": 12, "floor": 11601, "ceiling": 47150 },
6
+ { "rate": 22, "floor": 47151, "ceiling": 100525 },
7
+ { "rate": 24, "floor": 100526, "ceiling": 191950 },
8
+ { "rate": 32, "floor": 191951, "ceiling": 243725 },
9
+ { "rate": 35, "floor": 243726, "ceiling": 609350 },
10
+ { "rate": 37, "floor": 609351, "ceiling": null }
11
+ ],
12
+ "married_filing_jointly": [
13
+ { "rate": 10, "floor": 0, "ceiling": 23200 },
14
+ { "rate": 12, "floor": 23201, "ceiling": 94300 },
15
+ { "rate": 22, "floor": 94301, "ceiling": 201050 },
16
+ { "rate": 24, "floor": 201051, "ceiling": 383900 },
17
+ { "rate": 32, "floor": 383901, "ceiling": 487450 },
18
+ { "rate": 35, "floor": 487451, "ceiling": 731200 },
19
+ { "rate": 37, "floor": 731201, "ceiling": null }
20
+ ],
21
+ "married_filing_separately": [
22
+ { "rate": 10, "floor": 0, "ceiling": 11600 },
23
+ { "rate": 12, "floor": 11601, "ceiling": 47150 },
24
+ { "rate": 22, "floor": 47151, "ceiling": 100525 },
25
+ { "rate": 24, "floor": 100526, "ceiling": 191950 },
26
+ { "rate": 32, "floor": 191951, "ceiling": 243725 },
27
+ { "rate": 35, "floor": 243726, "ceiling": 365600 },
28
+ { "rate": 37, "floor": 365601, "ceiling": null }
29
+ ],
30
+ "head_of_household": [
31
+ { "rate": 10, "floor": 0, "ceiling": 16550 },
32
+ { "rate": 12, "floor": 16551, "ceiling": 63100 },
33
+ { "rate": 22, "floor": 63101, "ceiling": 100500 },
34
+ { "rate": 24, "floor": 100501, "ceiling": 191950 },
35
+ { "rate": 32, "floor": 191951, "ceiling": 243700 },
36
+ { "rate": 35, "floor": 243701, "ceiling": 609350 },
37
+ { "rate": 37, "floor": 609351, "ceiling": null }
38
+ ]
39
+ },
40
+ "2025": {
41
+ "single": [
42
+ { "rate": 10, "floor": 0, "ceiling": 11925 },
43
+ { "rate": 12, "floor": 11926, "ceiling": 48475 },
44
+ { "rate": 22, "floor": 48476, "ceiling": 103350 },
45
+ { "rate": 24, "floor": 103351, "ceiling": 197300 },
46
+ { "rate": 32, "floor": 197301, "ceiling": 250525 },
47
+ { "rate": 35, "floor": 250526, "ceiling": 626350 },
48
+ { "rate": 37, "floor": 626351, "ceiling": null }
49
+ ],
50
+ "married_filing_jointly": [
51
+ { "rate": 10, "floor": 0, "ceiling": 23850 },
52
+ { "rate": 12, "floor": 23851, "ceiling": 96950 },
53
+ { "rate": 22, "floor": 96951, "ceiling": 206700 },
54
+ { "rate": 24, "floor": 206701, "ceiling": 394600 },
55
+ { "rate": 32, "floor": 394601, "ceiling": 501050 },
56
+ { "rate": 35, "floor": 501051, "ceiling": 751600 },
57
+ { "rate": 37, "floor": 751601, "ceiling": null }
58
+ ],
59
+ "married_filing_separately": [
60
+ { "rate": 10, "floor": 0, "ceiling": 11925 },
61
+ { "rate": 12, "floor": 11926, "ceiling": 48475 },
62
+ { "rate": 22, "floor": 48476, "ceiling": 103350 },
63
+ { "rate": 24, "floor": 103351, "ceiling": 197300 },
64
+ { "rate": 32, "floor": 197301, "ceiling": 250525 },
65
+ { "rate": 35, "floor": 250526, "ceiling": 375800 },
66
+ { "rate": 37, "floor": 375801, "ceiling": null }
67
+ ],
68
+ "head_of_household": [
69
+ { "rate": 10, "floor": 0, "ceiling": 17000 },
70
+ { "rate": 12, "floor": 17001, "ceiling": 64850 },
71
+ { "rate": 22, "floor": 64851, "ceiling": 103350 },
72
+ { "rate": 24, "floor": 103351, "ceiling": 197300 },
73
+ { "rate": 32, "floor": 197301, "ceiling": 250500 },
74
+ { "rate": 35, "floor": 250501, "ceiling": 626350 },
75
+ { "rate": 37, "floor": 626351, "ceiling": null }
76
+ ]
77
+ }
78
+ }
@@ -0,0 +1,40 @@
1
+ module USIncomeTax
2
+ class Bracket
3
+ attr_accessor :year, :type, :rate, :floor_amount, :ceiling_amount, :gross_amount, :tax_amount_intermediate,
4
+ :tax_amount, :net_amount_intermediate, :net_amount
5
+
6
+ def initialize(year, type, rate, floor_amount, ceiling_amount)
7
+ @year = year.to_i
8
+ @type = type.to_sym
9
+ @rate = rate.to_f / 100
10
+ @floor_amount = floor_amount
11
+ @ceiling_amount = ceiling_amount
12
+ @gross_amount = nil
13
+ @tax_amount_intermediate = nil
14
+ @tax_amount = nil
15
+ @net_amount_intermediate = nil
16
+ @net_amount = nil
17
+ end
18
+
19
+ def hold_gross_amount(remaining)
20
+ current_floor = @floor_amount == 0 ? @floor_amount.to_f : (@floor_amount - 1).to_f
21
+
22
+ @gross_amount = [if @ceiling_amount
23
+ @ceiling_amount.to_f - current_floor
24
+ else
25
+ Float::INFINITY
26
+ end,
27
+ remaining].min
28
+ end
29
+
30
+ def calculate_tax
31
+ @tax_amount_intermediate = @gross_amount * @rate
32
+ @tax_amount = @tax_amount_intermediate.round(2)
33
+ end
34
+
35
+ def calculate_net_income
36
+ @net_amount_intermediate = @gross_amount - @tax_amount_intermediate
37
+ @net_amount = @net_amount_intermediate.round(2)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,35 @@
1
+ module USIncomeTax
2
+ class BracketLibrary
3
+ attr_accessor :content
4
+
5
+ def initialize(hash)
6
+ @content = {}
7
+ populate_content(hash)
8
+ end
9
+
10
+ def populate_content(hash)
11
+ hash.each do |year, types|
12
+ @content[year] = BracketLibrary.serialize_types(year, types)
13
+ end
14
+ end
15
+
16
+ def self.serialize_types(year, types)
17
+ types_hash = {}
18
+ types.map do |type, brackets|
19
+ temp_symbol = type.to_sym
20
+ types_hash[temp_symbol] = serialize_brackets(year, type, brackets)
21
+ end
22
+ types_hash
23
+ end
24
+
25
+ def self.serialize_brackets(year, type, brackets)
26
+ brackets.map do |bracket|
27
+ serialize_bracket(year, type, bracket)
28
+ end
29
+ end
30
+
31
+ def self.serialize_bracket(year, type, bracket)
32
+ Bracket.new(year, type, bracket["rate"], bracket["floor"], bracket["ceiling"])
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,12 @@
1
+ require "json"
2
+
3
+ module USIncomeTax
4
+ class BracketLoader
5
+ FILE_PATH = "data/federal_tax_bracket.json"
6
+
7
+ def self.load
8
+ file = File.read(FILE_PATH)
9
+ data_hash = JSON.parse(file)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,59 @@
1
+ module USIncomeTax
2
+ class CalculationResult
3
+ attr_accessor :year, :gross_income, :type, :brackets, :total_tax, :net_income
4
+
5
+ def initialize(year, gross_income, type)
6
+ @year = year
7
+ @gross_income = gross_income
8
+ @type = type
9
+
10
+ loaded_static_brackets = ::USIncomeTax::BracketLoader.load
11
+
12
+ @brackets = BracketLibrary.new(loaded_static_brackets).content
13
+ @total_tax = nil
14
+ @net_income = nil
15
+ end
16
+
17
+ def calculate
18
+ assign_income_to_tax_brackets
19
+ calculate_tax_brackets
20
+ calculate_total_tax
21
+ calculate_net_income
22
+ end
23
+
24
+ def assign_income_to_tax_brackets
25
+ types_in_year = @brackets[@year.to_s]
26
+ brackets_in_type = types_in_year[@type.to_sym]
27
+
28
+ remaining_gross_income = @gross_income
29
+ brackets_in_type.each do |bracket|
30
+ bracket.hold_gross_amount(remaining_gross_income)
31
+ remaining_gross_income -= bracket.gross_amount
32
+ end
33
+ end
34
+
35
+ # Must call assign_income_to_tax_brackets before usage
36
+ def calculate_tax_brackets
37
+ types_in_year = @brackets[@year.to_s]
38
+ brackets_in_type = types_in_year[@type.to_sym]
39
+
40
+ brackets_in_type.each do |bracket|
41
+ bracket.calculate_tax
42
+ end
43
+ end
44
+
45
+ # Must call calculate_tax_brackets before usage
46
+ def calculate_total_tax
47
+ types_in_year = @brackets[@year.to_s]
48
+ brackets_in_type = types_in_year[@type.to_sym]
49
+
50
+ @total_tax = brackets_in_type.reduce(0) do |result, element|
51
+ result + element.tax_amount
52
+ end
53
+ end
54
+
55
+ def calculate_net_income
56
+ @net_income = @gross_income - @total_tax
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,9 @@
1
+ module USIncomeTax
2
+ class Calculator
3
+ def self.calculate(year, gross_income, type)
4
+ calculation_result = CalculationResult.new(year, gross_income, type)
5
+ calculation_result.calculate
6
+ calculation_result
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,17 @@
1
+ module USIncomeTax
2
+ # Interface for us_income_tax gem
3
+ class Scenario
4
+ attr_accessor :result
5
+
6
+ def initialize(year, gross_income, type)
7
+ @year = year
8
+ @gross_income = gross_income
9
+ @type = type # TODO: Validate this against 4 types
10
+ @result = nil
11
+ end
12
+
13
+ def calculate
14
+ @result = Calculator.calculate(@year, @gross_income, @type)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module USIncomeTax
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "us_income_tax/version"
4
+ require_relative "us_income_tax/bracket_library"
5
+ require_relative "us_income_tax/bracket_loader"
6
+ require_relative "us_income_tax/bracket"
7
+ require_relative "us_income_tax/calculation_result"
8
+ require_relative "us_income_tax/calculator"
9
+
10
+ module USIncomeTax
11
+ class Error < StandardError; end
12
+ # Your code goes here...
13
+ end
@@ -0,0 +1,4 @@
1
+ module USIncomeTax
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: us_income_tax
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jason Kim
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-02-05 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Calculate US federal income tax. The gem can calculate how much tax should
14
+ be paid for different tax brackets
15
+ email:
16
+ - iamjsonkim@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".rubocop.yml"
22
+ - CHANGELOG.md
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - data/federal_tax_bracket.json
27
+ - lib/us_income_tax.rb
28
+ - lib/us_income_tax/bracket.rb
29
+ - lib/us_income_tax/bracket_library.rb
30
+ - lib/us_income_tax/bracket_loader.rb
31
+ - lib/us_income_tax/calculation_result.rb
32
+ - lib/us_income_tax/calculator.rb
33
+ - lib/us_income_tax/scenario.rb
34
+ - lib/us_income_tax/version.rb
35
+ - sig/us_income_tax.rbs
36
+ homepage: https://github.com/serv/us_income_tax
37
+ licenses:
38
+ - MIT
39
+ metadata:
40
+ homepage_uri: https://github.com/serv/us_income_tax
41
+ source_code_uri: https://github.com/serv/us_income_tax
42
+ changelog_uri: https://github.com/serv/us_income_tax
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: 3.0.0
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubygems_version: 3.5.16
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: Calculate US Federal Income Tax
62
+ test_files: []