geolocal 0.6.1 → 0.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 94b1ea23b169d938aedf7afa726b35f8c59025e7
4
- data.tar.gz: 7bde790f3742741c3ea0f94f0640927f93c02b6c
3
+ metadata.gz: a576db50e71f6e445ffd7159ca299f6301bcbd71
4
+ data.tar.gz: 548b0a82bc3e3459e7b2eb7e09ff40b898a654c1
5
5
  SHA512:
6
- metadata.gz: 5c3267bab152d3583da3aeedb7d2ba1467c9d3762061392d972db95974a5297a56f03056d96e7274c2897bd0f98dc7174710a7e67570c90eddfd26ef38131569
7
- data.tar.gz: 9427d86aa844125c4259c356801a3ad10716651b0b825306af08e3c3eb289d59fd98bc29e530526b72a0302278aa0a425d2e0b37284762fbfba5ff3183ee0974
6
+ metadata.gz: e6d417f096f6cb0bf601c4d74bd698a951766cd37bca686af96fcd66c8c43a92d8f48c8b773d436496a24897f73e7de8f7d6a9c2c82aca91ddd4b78306cf9757
7
+ data.tar.gz: eb79ad467e23ecf5e5d47e262e6ae7a0577b96e261971175bf9a1714f155ad9b53f61d46224546ba73b418ccabfd8a1ad97dd56a90fb606f2ec3444bb1ee23d8
@@ -16,37 +16,38 @@ module Geolocal
16
16
  download_files
17
17
  end
18
18
 
19
+ def add_to_results results, name, lostr, histr
20
+ loaddr = IPAddr.new(lostr)
21
+ hiaddr = IPAddr.new(histr)
22
+ lofam = loaddr.family
23
+ hifam = hiaddr.family
24
+ raise "#{lostr} is family #{lofam} but #{histr} is #{hifam}" if lofam != hifam
25
+
26
+ if lofam == Socket::AF_INET
27
+ namefam = name + 'v4' if config[:ipv4]
28
+ elsif lofam == Socket::AF_INET6
29
+ namefam = name + 'v6' if config[:ipv6]
30
+ else
31
+ raise "unknown family #{lofam} for #{lostr}"
32
+ end
33
+
34
+ if namefam
35
+ results[namefam] << "#{loaddr.to_i}..#{hiaddr.to_i},\n"
36
+ end
37
+ end
38
+
19
39
  def update
20
40
  countries = config[:countries].reduce({}) { |a, (k, v)|
21
41
  a.merge! k.to_s.upcase => Array(v).map(&:upcase).to_set
22
42
  }
23
43
 
24
- ipv4 = Socket::AF_INET if config[:ipv4]
25
- ipv6 = Socket::AF_INET6 if config[:ipv6]
26
-
27
44
  results = countries.keys.reduce({}) { |a, k|
28
- a.merge! k.upcase+'v4' => '' if ipv4
29
- a.merge! k.upcase+'v6' => '' if ipv6
45
+ a.merge! k.upcase+'v4' => '' if config[:ipv4]
46
+ a.merge! k.upcase+'v6' => '' if config[:ipv6]
30
47
  a
31
48
  }
32
49
 
33
- read_ranges(countries) do |name,lostr,histr|
34
- loaddr = IPAddr.new(lostr)
35
- hiaddr = IPAddr.new(histr)
36
- lofam = loaddr.family
37
- hifam = hiaddr.family
38
- raise "#{lostr} is family #{lofam} but #{histr} is #{hifam}" if lofam != hifam
39
-
40
- if lofam == ipv4
41
- namefam = name+'v4'
42
- elsif lofam == ipv6
43
- namefam = name+'v6'
44
- else
45
- raise "unknown family #{lofam} for #{lostr}"
46
- end
47
-
48
- results[namefam] << "#{loaddr.to_i}..#{hiaddr.to_i},\n"
49
- end
50
+ read_ranges(countries) { |*args| add_to_results(results, *args) }
50
51
 
51
52
  File.open(config[:file], 'w') do |file|
52
53
  output(file, results)
@@ -1,3 +1,3 @@
1
1
  module Geolocal
2
- VERSION = "0.6.1"
2
+ VERSION = "0.6.2"
3
3
  end
Binary file
@@ -55,7 +55,7 @@ describe Geolocal::Provider::DB_IP do
55
55
 
56
56
 
57
57
  describe 'generating' do
58
- let(:example_output) {
58
+ let(:example_header) {
59
59
  <<EOL
60
60
  # This file is autogenerated by the Geolocal gem
61
61
 
@@ -74,6 +74,30 @@ module Geolocal
74
74
  true if mod.bsearch { |range| num > range.max ? 1 : num < range.min ? -1 : 0 }
75
75
  end
76
76
 
77
+ EOL
78
+ }
79
+
80
+ def run_test example_body
81
+ outfile = 'tmp/geolocal.rb'
82
+ if File.exist?(outfile)
83
+ File.delete(outfile)
84
+ end
85
+
86
+ Geolocal.configure do |config|
87
+ config.tmpdir = 'spec/data'
88
+ config.file = outfile
89
+ config.countries = { us: 'US', au: 'AU' }
90
+ yield config
91
+ end
92
+
93
+ provider.update
94
+ expect(File.read outfile).to eq example_header + example_body
95
+ File.delete(outfile)
96
+ end
97
+
98
+
99
+ it 'can generate countries from a csv' do
100
+ example_output = <<EOL
77
101
  def self.in_us? address, family=nil
78
102
  search address, family, USv4, USv6
79
103
  end
@@ -89,6 +113,7 @@ Geolocal::USv4 = [
89
113
  ]
90
114
 
91
115
  Geolocal::USv6 = [
116
+ 55854460156896106951106838613354086400..55854460790721407065221539361705689087,
92
117
  ]
93
118
 
94
119
  Geolocal::AUv4 = [
@@ -96,25 +121,69 @@ Geolocal::AUv4 = [
96
121
  ]
97
122
 
98
123
  Geolocal::AUv6 = [
124
+ 55832834671488781931518904937387917312..55832834671488781949965649011097468927,
99
125
  ]
100
126
 
101
127
  EOL
102
- }
103
128
 
104
- it 'can generate countries from a csv' do
105
- outfile = 'tmp/geolocal.rb'
106
- if File.exist?(outfile)
107
- File.delete(outfile)
129
+ run_test example_output do |config|
130
+ # no need to change config
108
131
  end
132
+ end
109
133
 
110
- Geolocal.configure do |config|
111
- config.tmpdir = 'spec/data'
112
- config.file = outfile
113
- config.countries = { us: 'US', au: 'AU' }
134
+ it 'can generate countries from a csv when ipv6 is turned off' do
135
+ example_output = <<EOL
136
+ def self.in_us? address, family=nil
137
+ search address, family, USv4, nil
138
+ end
139
+
140
+ def self.in_au? address, family=nil
141
+ search address, family, AUv4, nil
142
+ end
143
+
144
+ end
145
+
146
+ Geolocal::USv4 = [
147
+ 0..16777215,
148
+ ]
149
+
150
+ Geolocal::AUv4 = [
151
+ 16777216..16777471,
152
+ ]
153
+
154
+ EOL
155
+
156
+ run_test example_output do |config|
157
+ config.ipv6 = false
158
+ end
159
+ end
160
+
161
+
162
+ it 'can generate countries from a csv when ipv4 is turned off' do
163
+ example_output = <<EOL
164
+ def self.in_us? address, family=nil
165
+ search address, family, nil, USv6
166
+ end
167
+
168
+ def self.in_au? address, family=nil
169
+ search address, family, nil, AUv6
170
+ end
171
+
172
+ end
173
+
174
+ Geolocal::USv6 = [
175
+ 55854460156896106951106838613354086400..55854460790721407065221539361705689087,
176
+ ]
177
+
178
+ Geolocal::AUv6 = [
179
+ 55832834671488781931518904937387917312..55832834671488781949965649011097468927,
180
+ ]
181
+
182
+ EOL
183
+
184
+ run_test example_output do |config|
185
+ config.ipv4 = false
114
186
  end
115
- provider.update
116
- expect(File.read outfile).to eq example_output
117
- File.delete(outfile)
118
187
  end
119
188
  end
120
189
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geolocal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Bronson