class KoRegionCodeMapper

Constants

DATA_INDEX
FILE_NAME

Public Class Methods

new(opts = { filename: FILE_NAME, include_header: true, index_mapping: DATA_INDEX }) click to toggle source
# File lib/ko_region_code_mapper.rb, line 21
def initialize(opts = { filename: FILE_NAME, include_header: true, index_mapping: DATA_INDEX })
  if opts != nil && (opts[:filename].empty? || opts[:include_header].nil? || opts[:index_mapping].empty?)
    raise ArgumentError.new("Check opts parameter. It should include appropriate 'filename', 'include_header', 'index_mapping'.(given: #{opts})")
  end

  if opts[:index_mapping].class != Hash
    raise ArgumentError.new("Check opts[:index_mapping]. It should be hash consist of (key, value) presenting (attribute_name, index of column in table)")
  end

  @index_map = opts[:index_mapping] || DATA_INDEX

  file = CSV.read(File.expand_path(File.dirname(__FILE__) + "/#{opts[:filename] || FILE_NAME}"))
  @headers = file.delete_at(0) if opts[:include_header] || true
  @data = file
end

Public Instance Methods

find_hcodes_by_sigungu_code(sigungu_code) click to toggle source
# File lib/ko_region_code_mapper.rb, line 45
def find_hcodes_by_sigungu_code(sigungu_code)
  raise ArgumentError.new("The required parameter sigungu_code cannot be nil or empty string.") if sigungu_code.nil? || sigungu_code.empty?
  row = find_by_sigungu_code(sigungu_code)
  row.map { |d| d[@index_map[:hcode]].to_s }
end
find_sigungu_code_by_hcode(hcode) click to toggle source
# File lib/ko_region_code_mapper.rb, line 37
def find_sigungu_code_by_hcode(hcode)
  raise ArgumentError.new("The required parameter hcode cannot be nil or empty string.") if hcode.nil? || hcode.empty?
  row = find_by_hcode(hcode).first
  return nil if row.nil?
  
  row[@index_map[:hcategory]]&.slice(0..4)
end

Private Instance Methods

find_by_hcode(hcode) click to toggle source
# File lib/ko_region_code_mapper.rb, line 53
def find_by_hcode(hcode)
  idx = @index_map[:hcode]
  @data.select { |row| row[idx].to_s.eql? hcode.to_s }
end
find_by_sigungu_code(sigungu_code) click to toggle source
# File lib/ko_region_code_mapper.rb, line 58
def find_by_sigungu_code(sigungu_code)
  idx = @index_map[:hcategory]
  @data.select{ |row| row[idx].to_s.start_with? sigungu_code }
end