class Ip2locationRegion

Attributes

records[RW]

Public Class Methods

new(csv) click to toggle source
# File lib/ip2location_ruby.rb, line 1103
def initialize(csv)
  if csv == ''
    abort('The CSV file "' + csv + '" is not found.')
  end

  begin
    csvfile = File.open(File.expand_path csv, 'rb')
  rescue
    abort('Error in opening ' + csv + '. No such CSV file in the /your_ip2location_ruby_library_path/rb/ folder.')
  else
  end

  begin
    CSV.parse(csvfile)
  rescue
    abort('Unable to read "' + csv + '".')
  else
    line = 1
    self.records = Hash.new
    CSV.foreach((csvfile)) do |data|
      if line == 1
        if data[1] != 'subdivision_name'
          abort('Invalid region information CSV file.')
        end
      else
        temp_data = Hash.new
        temp_data['code'] = data[2]
        temp_data['name'] = data[1]
        if self.records[data[0]]
          self.records[data[0]].push temp_data
        else
          self.records[data[0]] = [temp_data]
        end
      end
      line = line + 1
    end
  end
end

Public Instance Methods

get_region_code(country_code, region_name) click to toggle source
# File lib/ip2location_ruby.rb, line 1142
def get_region_code(country_code, region_name)
  if self.records.empty?
    abort('No record available.')
  end

  if (self.records[country_code]).nil?
    return
  end

  for i in 0..(self.records[country_code].length()-1)
    if region_name.upcase == self.records[country_code][i]["name"].upcase
      return self.records[country_code][i]["code"]
    end
  end
end