Class: KoRegionCodeMapper
- Inherits:
-
Object
- Object
- KoRegionCodeMapper
- Defined in:
- lib/ko_region_code_mapper.rb
Constant Summary collapse
- FILE_NAME =
'data/ko_region_20181231.csv'- DATA_INDEX =
{ :sido => 0, :sigungu => 1, :hname => 2, :hdong => 3, :bdong => 4, :hcategory => 5, :hcode => 6, :hcreated_at => 7, :bcode => 8, :hname_en => 9 }
Instance Method Summary collapse
- #find_hcodes_by_sigungu_code(sigungu_code) ⇒ Object
- #find_sigungu_code_by_hcode(hcode) ⇒ Object
-
#initialize(opts = { filename: FILE_NAME, include_header: true, index_mapping: DATA_INDEX }) ⇒ KoRegionCodeMapper
constructor
A new instance of KoRegionCodeMapper.
Constructor Details
#initialize(opts = { filename: FILE_NAME, include_header: true, index_mapping: DATA_INDEX }) ⇒ KoRegionCodeMapper
Returns a new instance of KoRegionCodeMapper.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# 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("[ERROR] Check opts parameter. It should include appropriate 'filename', 'include_header', 'index_mapping'.(given: #{opts})") end if opts[:index_mapping].class != Hash raise ArgumentError.new("[ERROR] 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.(File.dirname(__FILE__) + "/#{opts[:filename] || FILE_NAME}")) @headers = file.delete_at(0) if opts[:include_header] || true @data = file end |
Instance Method Details
#find_hcodes_by_sigungu_code(sigungu_code) ⇒ Object
42 43 44 45 |
# File 'lib/ko_region_code_mapper.rb', line 42 def find_hcodes_by_sigungu_code(sigungu_code) row = find_by_sigungu_code(sigungu_code) row.map { |d| d[@index_map[:hcode]].to_s } end |
#find_sigungu_code_by_hcode(hcode) ⇒ Object
37 38 39 40 |
# File 'lib/ko_region_code_mapper.rb', line 37 def find_sigungu_code_by_hcode(hcode) row = find_by_hcode(hcode).first row[@index_map[:hcategory]]&.slice(0..4) end |