Class: Ip2locationRegion
- Inherits:
-
Object
- Object
- Ip2locationRegion
- Defined in:
- lib/ip2location_ruby.rb
Instance Attribute Summary collapse
-
#records ⇒ Object
Returns the value of attribute records.
Instance Method Summary collapse
- #get_region_code(country_code, region_name) ⇒ Object
-
#initialize(csv) ⇒ Ip2locationRegion
constructor
A new instance of Ip2locationRegion.
Constructor Details
#initialize(csv) ⇒ Ip2locationRegion
Returns a new instance of Ip2locationRegion.
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 |
# File 'lib/ip2location_ruby.rb', line 1169 def initialize(csv) if csv == '' abort('The CSV file "' + csv + '" is not found.') end begin csvfile = File.open(File. 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 |
Instance Attribute Details
#records ⇒ Object
Returns the value of attribute records.
1167 1168 1169 |
# File 'lib/ip2location_ruby.rb', line 1167 def records @records end |
Instance Method Details
#get_region_code(country_code, region_name) ⇒ Object
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 |
# File 'lib/ip2location_ruby.rb', line 1208 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 |