Class: IPLibrary::Base
- Inherits:
-
Object
- Object
- IPLibrary::Base
- Defined in:
- lib/ip_library/base.rb
Class Method Summary collapse
- .defined_class_methods ⇒ Object
- .find_by_ip(ip) ⇒ Object
- .ip_cities(force_reload = false) ⇒ Object
- .singleton ⇒ Object
- .undef_class_methods(symbols) ⇒ Object
Class Method Details
.defined_class_methods ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/ip_library/base.rb', line 54 def defined_class_methods Configuration.columns.each_with_index do |col, index| next if Configuration.non_optional_columns.include?(col) instance_eval(<<-METHOD, __FILE__, __LINE__ + 1) def ip2#{col.to_s}(ip) find_by_ip(ip).try(:[], #{index}) end METHOD end end |
.find_by_ip(ip) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/ip_library/base.rb', line 45 def find_by_ip(ip) header, ip = get_header_and_ip(ip) ip_cities = Base.ip_cities[header] return nil if ip_cities.blank? ip_cities = ip_cities.select { |ip_city| ip_city[0].to_i <= ip && ip_city[1].to_i >= ip } ip_cities.sort_by { |ip_city| ip_city[1].to_i - ip_city[0].to_i }.first if ip_cities.present? end |
.ip_cities(force_reload = false) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/ip_library/base.rb', line 27 def ip_cities(force_reload = false) return @@ip_cities if !force_reload && defined?(@@ip_cities) && @@ip_cities.present? @@ip_cities = {} File.open(Configuration.file_path, 'r') do |file| datas = file.to_a.split(Configuration.separator) datas.each do |data| header = data.delete_at(0).chomp data.each do |row| ip_city = row.chomp.split(',') @@ip_cities[header] ||= [] @@ip_cities[header] << ip_city end end end @@ip_cities end |
.singleton ⇒ Object
71 72 73 |
# File 'lib/ip_library/base.rb', line 71 def singleton class << Base; self; end end |
.undef_class_methods(symbols) ⇒ Object
65 66 67 68 69 |
# File 'lib/ip_library/base.rb', line 65 def undef_class_methods(symbols) symbols.each do |symbol| singleton.send :undef_method, "ip2#{symbol.to_s}".to_sym end end |