Class: Ip2locationCountry
- Inherits:
-
Object
- Object
- Ip2locationCountry
- Defined in:
- lib/ip2location_ruby.rb
Instance Attribute Summary collapse
-
#fields ⇒ Object
Returns the value of attribute fields.
-
#records ⇒ Object
Returns the value of attribute records.
Instance Method Summary collapse
- #get_country_info(country_code = nil) ⇒ Object
-
#initialize(csv) ⇒ Ip2locationCountry
constructor
A new instance of Ip2locationCountry.
Constructor Details
#initialize(csv) ⇒ Ip2locationCountry
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 |
# File 'lib/ip2location_ruby.rb', line 1105 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[0] != 'country_code' abort('Invalid country information CSV file.') end self.fields = data else self.records[data[0]] = data end line = line + 1 end end end |
Instance Attribute Details
#fields ⇒ Object
Returns the value of attribute fields.
1103 1104 1105 |
# File 'lib/ip2location_ruby.rb', line 1103 def fields @fields end |
#records ⇒ Object
Returns the value of attribute records.
1103 1104 1105 |
# File 'lib/ip2location_ruby.rb', line 1103 def records @records end |
Instance Method Details
#get_country_info(country_code = nil) ⇒ Object
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 |
# File 'lib/ip2location_ruby.rb', line 1138 def get_country_info(country_code = nil) if self.records.empty? abort('No record available.') end if country_code if (self.records[country_code]).nil? return [] end results = Hash.new for i in 0..(self.fields.length()-1) results[self.fields[i]] = self.records[country_code][i] end return results end results = [] self.records.each do |key, value| data = Hash.new for i in 0..(self.fields.length()-1) data[self.fields[i]] = self.records[key][i] end results = results.append(data) end return results end |