Sha256: d469e75701cbf587656b869e6eab6095084a4733a8ffda57acc9b2a9887a2465

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

module CSI
  class Record
    attr_accessor :type
    attr_accessor :code
    attr_accessor :name
    attr_accessor :correlations

    # Instantiate a CSI record based on a type ('NAICS' or 'SIC') and a code
    # assuming the code/type combination exists.
    #
    # Returns a CSI::Record object or nil if one couldn't be created.
    def initialize(type, code)
      raise CSI::CodeInvalid, 'String required' unless code.is_a? String

      type.downcase!
      raise CSI::TypeInvalid, 'Invalid type - SIC and NAICS only' unless type == 'naics' || type == 'sic'

      data_file = File.expand_path(__FILE__+"/../../data/#{type}/#{code}.toml")
      raise CSI::ClassificationInvalid unless File.exists? data_file

      begin
        @type = type
        @code = code
        toml_record = TOML.load_file(data_file)
        @name = toml_record["name"]

        if type == 'naics'
          @correlations = toml_record["sic_correlations"]
        elsif type == 'sic'
          @correlations = toml_record["naics_correlations"]
        end

        if @correlations.class == String
          @correlations = [@correlations]
        end

      rescue => e
        return nil
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
csi-0.3.3 lib/csi/record.rb