Class: Geolocal::Provider::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/geolocal/provider/base.rb,
lib/geolocal/provider/base.rb

Direct Known Subclasses

DB_IP

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Base

Returns a new instance of Base.



7
8
9
# File 'lib/geolocal/provider/base.rb', line 7

def initialize params={}
  @config = params.merge(Geolocal.configuration.to_hash)
end

Instance Method Details

#add_to_results(results, name, lostr, histr) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/geolocal/provider/base.rb', line 19

def add_to_results results, name, lostr, histr
  loaddr = IPAddr.new(lostr)
  hiaddr = IPAddr.new(histr)
  lofam = loaddr.family
  hifam = hiaddr.family
  raise "#{lostr} is family #{lofam} but #{histr} is #{hifam}" if lofam != hifam

  if lofam == Socket::AF_INET
    namefam = name + 'v4' if config[:ipv4]
  elsif lofam == Socket::AF_INET6
    namefam = name + 'v6' if config[:ipv6]
  else
    raise "unknown family #{lofam} for #{lostr}"
  end

  if namefam
    results[namefam] << "#{loaddr.to_i}..#{hiaddr.to_i},\n"
  end
end

#configObject



11
12
13
# File 'lib/geolocal/provider/base.rb', line 11

def config
  @config
end

#downloadObject



15
16
17
# File 'lib/geolocal/provider/base.rb', line 15

def download
  download_files
end

#output(file, results) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/geolocal/provider/base.rb', line 68

def output file, results
  modname = config[:module]

  write_header file, modname

  config[: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, body|
    status "#{name} "
    write_ranges file, modname, name, body
  end
  status "\n"
end

#status(*args) ⇒ Object



144
145
146
147
148
149
# File 'lib/geolocal/provider/base.rb', line 144

def status *args
  unless config[:quiet]
    Kernel.print(*args)
    $stdout.flush unless args.last.end_with?("\n")
  end
end

#time_blockObject

returns elapsed time of block in seconds



137
138
139
140
141
142
# File 'lib/geolocal/provider/base.rb', line 137

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

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
# File 'lib/geolocal/provider/base.rb', line 59

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

#updateObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/geolocal/provider/base.rb', line 39

def update
  countries = config[:countries].reduce({}) { |a, (k, v)|
    a.merge! k.to_s.upcase => Array(v).map(&:upcase).to_set
  }

  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) }

  File.open(config[:file], 'w') do |file|
    output(file, results)
  end

  status "done, result in #{config[:file]}\n"
end

#write_header(file, modname) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/geolocal/provider/base.rb', line 89

def write_header file, modname
  file.write <<EOL
# This file is autogenerated by the Geolocal gem

module #{modname}

  def self.search address, family=nil, 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
    raise "ipv\#{family == 2 ? 4 : 6} was not compiled in" unless mod
    true if mod.bsearch { |range| num > range.max ? 1 : num < range.min ? -1 : 0 }
  end

EOL
end

#write_method(file, name, v4mod, v6mod) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/geolocal/provider/base.rb', line 111

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, body) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/geolocal/provider/base.rb', line 120

def write_ranges file, modname, name, body
  file.write <<EOL
#{modname}::#{name} = [
#{body}]

EOL
end