class Cnccs::Ccs

Attributes

code[R]

@return [String]

description[R]

@return [String]

description_full[R]

@return [String]

fieldcode[R]

@return [String]

groupcode[R]

@return [String]

notes[R]

@return [Array<Cnccs::Note>]

subgroupcode[R]

@return [String]

Public Class Methods

new(code = nil, fieldcode: nil, groupcode: nil, subgroupcode: nil) click to toggle source

@param code [String] @param fieldcode [String] @param groupcode [String] @param subgroupcode [String]

# File lib/cnccs/ccs.rb, line 35
def initialize(code = nil, fieldcode: nil, groupcode: nil, subgroupcode: nil)
  if code
    case code.size
    when 1 then fetch_field code
    when 3 then fetch_subgroup code
    end
  elsif fieldcode && groupcode
    @code, @description, @fieldcode, nts = fetch_data(2, fieldcode + groupcode)
    @groupcode = @code[1..-1]
    @notes = fetch_notes nts
    _fldc, desc = fetch_data(1, @fieldcode)
    @description_full = "#{desc}; #{@description}"
  elsif fieldcode
    fetch_field fieldcode
  elsif subgroupcode
    fetch_subgroup subgroupcode
  end    
end

Private Instance Methods

fetch_data(level, match) click to toggle source

Read data from file. @param level [Integer] @param match [String] @return [Array<String, Array>]

# File lib/cnccs/ccs.rb, line 81
def fetch_data(level, match)
  csv = CSV.read "#{__dir__}/../../cn-ccs-codes/cat-level#{level}.csv", encoding: "UTF-8", headers: true
  row = csv.find { |r| r[0] == match }
  row.map(&:last)
end
fetch_field(field_code) click to toggle source

Fetch data by field code. @param field_code [String]

# File lib/cnccs/ccs.rb, line 58
def fetch_field(field_code)
  @code = field_code
  @fieldcode, @description = fetch_data(1, field_code)
  @notes = []
end
fetch_notes(nts) click to toggle source

Fetch notes. @param nts [String] @return [Array<Cnccs::Note>]

# File lib/cnccs/ccs.rb, line 90
def fetch_notes(nts)
  return if nts.nil?
  splitter = [227, 128, 130].pack('c*').force_encoding("UTF-8")
  rgxp = /[A-Z]\d{2}(\/\d{2})?$/
  nts.split(splitter).map do |n|
    ccs_code = n.match(rgxp).to_s
    text = n.sub(rgxp, "{ccs-code}")
    Note.new text, ccs_code
  end
end
fetch_subgroup(subgroup_code) click to toggle source

Fetch data by subgroup code. @param subgroup_code [String]

# File lib/cnccs/ccs.rb, line 66
def fetch_subgroup(subgroup_code)
  @code, @description, sg_code, nts = fetch_data(3, subgroup_code)
  @fieldcode    = @code.slice 0
  @groupcode    = sg_code[1..-1]
  @subgroupcode = @code[1..-1]
  @notes        = fetch_notes nts
  _fldc, desc1 = fetch_data(1, @fieldcode)
  _fldc, desc2 = fetch_data(2, sg_code)
  @description_full = "#{desc1}; #{desc2}; #{@description}"
end