Class: Geolocal::Provider::Base
- Inherits:
-
Object
- Object
- Geolocal::Provider::Base
- Defined in:
- lib/geolocal/provider/base.rb,
lib/geolocal/provider/base.rb
Direct Known Subclasses
Instance Method Summary collapse
- #add_to_results(results, name, lostr, histr) ⇒ Object
- #coalesce_ranges(ranges) ⇒ Object
- #config ⇒ Object
- #countries ⇒ Object
- #download ⇒ Object
-
#initialize(params = {}) ⇒ Base
constructor
A new instance of Base.
- #output(file, results) ⇒ Object
- #status(*args) ⇒ Object
-
#time_block ⇒ Object
returns elapsed time of block in seconds.
- #up_to_date?(file, expiry) ⇒ Boolean
- #update ⇒ Object
- #write_header(file, modname) ⇒ Object
- #write_method(file, name, v4mod, v6mod) ⇒ Object
- #write_ranges(file, modname, name, ranges) ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ Base
Returns a new instance of Base.
8 9 10 |
# File 'lib/geolocal/provider/base.rb', line 8 def initialize params={} @config = params.merge(Geolocal.configuration.to_hash) end |
Instance Method Details
#add_to_results(results, name, lostr, histr) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/geolocal/provider/base.rb', line 20 def add_to_results results, name, lostr, histr loaddr = IPAddr.new(lostr) hiaddr = IPAddr.new(histr) lofam = loaddr.family hifam = hiaddr.family if lofam != hifam raise "#{lostr} and #{histr} must be in the same address family" end loval = loaddr.to_i hival = hiaddr.to_i if loval > hival raise "range supplied in the wrong order: #{lostr}..#{histr}" end if lofam == Socket::AF_INET namefam = name.upcase + 'v4' if config[:ipv4] elsif lofam == Socket::AF_INET6 namefam = name.upcase + 'v6' if config[:ipv6] else raise "unknown address family #{lofam} for #{lostr}" end if namefam results[namefam] << (loaddr.to_i..hiaddr.to_i) end namefam end |
#coalesce_ranges(ranges) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/geolocal/provider/base.rb', line 105 def coalesce_ranges ranges ranges = ranges.sort_by { |r| r.min } uniques = [] lastr = ranges.shift uniques << lastr if lastr ranges.each do |thisr| if lastr.last >= thisr.first - 1 lastr = lastr.first..[thisr.last, lastr.last].max uniques[-1] = lastr else lastr = thisr uniques << lastr end end uniques end |
#config ⇒ Object
12 13 14 |
# File 'lib/geolocal/provider/base.rb', line 12 def config @config end |
#countries ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/geolocal/provider/base.rb', line 49 def countries @countries ||= config[:countries].sort_by { |k, v| k.to_s }.reduce({}) { |a, (k, v)| k = k.to_s.gsub(/[ -]/, '_') raise "invalid identifier: '#{k}'" if k =~ /^[^A-Za-z_]|[^A-Za-z0-9_]|^\s*$/ a.merge! k.to_s.downcase => Array(v).map { |c| c.to_s.upcase }.sort.to_set } end |
#download ⇒ Object
16 17 18 |
# File 'lib/geolocal/provider/base.rb', line 16 def download download_files end |
#output(file, results) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/geolocal/provider/base.rb', line 83 def output file, results modname = config[:module] write_header file, modname countries.keys.each do |name| v4mod = config[:ipv4] ? name.to_s.upcase + 'v4' : 'nil' v6mod = config[:ipv6] ? name.to_s.upcase + 'v6' : 'nil' write_method file, name, v4mod, v6mod end file.write "end\n\n" status " writing " results.each do |name, ranges| coalesced = coalesce_ranges(ranges) status "#{name}-#{ranges.length - coalesced.length} " write_ranges file, modname, name, coalesced end status "\n" end |
#status(*args) ⇒ Object
184 185 186 187 188 189 |
# File 'lib/geolocal/provider/base.rb', line 184 def status *args unless config[:quiet] Kernel.print(*args) $stdout.flush unless args.last.end_with?("\n") end end |
#time_block ⇒ Object
returns elapsed time of block in seconds
177 178 179 180 181 182 |
# File 'lib/geolocal/provider/base.rb', line 177 def time_block start = Time.now yield stop = Time.now stop - start + 0.0000001 # fudge to prevent division by zero end |
#up_to_date?(file, expiry) ⇒ Boolean
74 75 76 77 78 79 80 81 |
# File 'lib/geolocal/provider/base.rb', line 74 def up_to_date?(file, expiry) return false unless File.exist?(file) diff = Time.now - File.mtime(file) if diff < expiry status "using #{file} since it's #{diff.round} seconds old\n" return true end end |
#update ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/geolocal/provider/base.rb', line 57 def update results = countries.keys.reduce({}) { |a, k| a.merge! k.upcase+'v4' => [] if config[:ipv4] a.merge! k.upcase+'v6' => [] if config[:ipv6] a } read_ranges(countries) { |*args| add_to_results(results, *args) } FileUtils.mkdir_p File.dirname(config[:file]) File.open(config[:file], 'w') do |file| output(file, results) end status "done, result in #{config[:file]}\n" end |
#write_header(file, modname) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/geolocal/provider/base.rb', line 126 def write_header file, modname file.write <<EOL # This file is autogenerated by the Geolocal gem require 'ipaddr' module #{modname} def self.search address, family, v4module, v6module address = IPAddr.new(address) if address.is_a?(String) family = address.family unless family num = address.to_i case family when Socket::AF_INET then mod = v4module when Socket::AF_INET6 then mod = v6module else raise "Unknown family \#{family} for address \#{address}" end return false unless mod # if we didn't compile in ipv6, assume nothing's local true if mod.bsearch { |range| num > range.max ? 1 : num < range.min ? -1 : 0 } end EOL end |
#write_method(file, name, v4mod, v6mod) ⇒ Object
150 151 152 153 154 155 156 157 |
# File 'lib/geolocal/provider/base.rb', line 150 def write_method file, name, v4mod, v6mod file.write <<EOL def self.in_#{name}? address, family=nil search address, family, #{v4mod}, #{v6mod} end EOL end |
#write_ranges(file, modname, name, ranges) ⇒ Object
159 160 161 162 163 164 165 166 |
# File 'lib/geolocal/provider/base.rb', line 159 def write_ranges file, modname, name, ranges file.write <<EOL #{modname}::#{name} = [ #{ranges.join(",\n")} ] EOL end |