Sha256: ce04676b56133594d00372487bdc8d5a63769daf26fae3aad2738184025ed5aa

Contents?: true

Size: 1.34 KB

Versions: 5

Compression:

Stored size: 1.34 KB

Contents

require 'bigdecimal'
require 'date'
require 'json'
require 'pathname'

class InsuranceJP
  attr_reader :table

  # load config
  def initialize(dir_path, area=nil, date=nil)
    @pjdir = Pathname(dir_path) + 'dict'
    @area = area
    @date = date
    @table = load_table
  end

  def load_table
    file_name = @pjdir + select_active_filename
    JSON.parse(File.read(file_name.to_s))
  end

  def health_insurance_salary(rank)
    round6(select_health_insurance(rank).dig('insurance_elder_salary'))
  end

  def pension_salary(rank)
    round6(select_pension(rank).dig('pension_salary'))
  end

  def select_health_insurance(rank)
    @table['fee'].filter{|h| h['rank'] == rank}.first
  end

  def select_pension(rank)
    @table['fee'].filter{|h| h['pension_rank'] == rank}.first
  end

  private

  def round6(num)
    BigDecimal(num).round(0, BigDecimal::ROUND_HALF_DOWN).to_i
  end

  # TODO: need selection logic
  def select_active_filename
    list = load_json
    list.first[1]
  end

  def load_json
    table_list = [].tap do |a|
      open_tables do |f, name|
        data = JSON.parse(f.read)
        a << [Date.parse(data['effective_date']), name]
      end
    end
  end

  def open_tables
    Dir.chdir(@pjdir.to_s) do
      Dir.glob("*.json").each do |file_name|
        File.open(file_name, 'r') {|f| yield(f, file_name)}
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
lucasalary-jp-0.1.5 lib/luca_salary/jp/insurance.rb
lucasalary-jp-0.1.4 lib/luca_salary/jp/insurance.rb
lucasalary-jp-0.1.3 lib/luca_salary/jp/insurance.rb
lucasalary-jp-0.1.2 lib/luca_salary/jp/insurance.rb
lucasalary-jp-0.1.1 lib/luca_salary/jp/insurance.rb