Skip to content

Commit

Permalink
support crystal 0.25.0
Browse files Browse the repository at this point in the history
  • Loading branch information
arcage committed Jul 3, 2018
1 parent 4be6a8e commit 7b49b68
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 44 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Logs

## ver. 0.1.3

- support crystal 0.25.0

## ver. 0.1.2

- Replace `as` keyword to `#as(TYPE)` method for crystal 0.19.0
Expand Down
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: ip2country
version: 0.1.2
version: 0.1.3

authors:
- ʕ·ᴥ·ʔAKJ <arcage@denchu.org>
Expand Down
1 change: 0 additions & 1 deletion spec/ip2country_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ describe IP2Country do
it "returns lookuped country name by specified default language from ip" do
IP2Country.new("ja").lookup("8.8.8.8").should eq "アメリカ合衆国"
end

end
17 changes: 8 additions & 9 deletions src/ip2country.cr
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@

require "http/client"
require "uri"
require "yaml"

class IP2Country
VERSION = "0.1.3"

CACHE_DIR = File.expand_path(File.dirname(File.dirname(__FILE__))) + "/cache"
CACHE_DIR = File.expand_path(File.dirname(File.dirname(__FILE__))) + "/cache"
REGISTRARS = {
"ARIN" => "http://ftp.arin.net/pub/stats/arin/delegated-arin-extended-latest",
"RIPE" => "http://ftp.ripe.net/pub/stats/ripencc/delegated-ripencc-extended-latest",
"APNIC" => "https://ftp.apnic.net/stats/apnic/delegated-apnic-extended-latest",
"LACNIC" => "http://ftp.lacnic.net/pub/stats/lacnic/delegated-lacnic-extended-latest",
"AFRINIC" => "http://ftp.afrinic.net/pub/stats/afrinic/delegated-afrinic-extended-latest"
"ARIN" => "http://ftp.arin.net/pub/stats/arin/delegated-arin-extended-latest",
"RIPE" => "https://ftp.ripe.net/pub/stats/ripencc/delegated-ripencc-extended-latest",
"APNIC" => "https://ftp.apnic.net/stats/apnic/delegated-apnic-extended-latest",
"LACNIC" => "http://ftp.lacnic.net/pub/stats/lacnic/delegated-lacnic-extended-latest",
"AFRINIC" => "http://ftp.afrinic.net/pub/stats/afrinic/delegated-afrinic-extended-latest",
}
LANGS = Set.new ["en", "fr", "de", "es", "pt", "ja", "ko", "zh"]

def self.cache_update
STDERR.puts "[IP2Coutnry] Fetching conversion tables."
STDERR << "[IP2Coutnry] Fetching conversion tables.\n"
CC2Country.cache_update
IP2CC.cache_update
end
Expand All @@ -28,7 +27,7 @@ class IP2Country

def initialize(@default_lang = "en")
unless File.directory?(CACHE_DIR)
STDERR.puts "[IP2Coutnry] Conversion tables not found."
STDERR << "[IP2Coutnry] Conversion tables not found.\n"
IP2Country.cache_update
end
@conversion = IP2CC.new
Expand Down
22 changes: 10 additions & 12 deletions src/ip2country/cc2country.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

class IP2Country::CC2Country
CACHE_DIR = IP2Country::CACHE_DIR + "/country"

Expand All @@ -8,38 +7,38 @@ class IP2Country::CC2Country
IP2Country::LANGS.each do |lang|
uri = URI.parse("https://raw.githubusercontent.com/umpirsky/country-list/master/data/#{lang}/country.yaml")
cache_file = CACHE_DIR + "/#{lang}.yaml"
mtime = File.exists?(cache_file) ? File.stat(cache_file).mtime : Time.new(2000, 1, 1)
mtime = File.exists?(cache_file) ? File.info(cache_file).modification_time : Time.new(2000, 1, 1)
http = HTTP::Client.new(uri)
headers = HTTP::Headers.new
headers["If-modified-since"] = HTTP.rfc1123_date(mtime)
headers["If-modified-since"] = HTTP.format_time(mtime)
responce = http.get(uri.path.not_nil!, headers)
case responce.status_code
when 200
STDERR.puts "[IP2Coutnry] Country code table(#{lang}) is updated."
STDERR << "[IP2Coutnry] Country code table(#{lang}) is updated.\n"
File.write(cache_file, responce.body)
modified = true
when 304
STDERR.puts "[IP2Coutnry] Country code table(#{lang}) is not modified."
STDERR << "[IP2Coutnry] Country code table(#{lang}) is not modified.\n"
when 404
STDERR.puts "[IP2Coutnry] Country code table(#{lang}) is not found."
STDERR << "[IP2Coutnry] Country code table(#{lang}) is not found.\n"
else
STDERR.puts "[IP2Coutnry] Receive status code #{responce.status_code} for country code table(#{lang})."
STDERR << "[IP2Coutnry] Receive status code #{responce.status_code} for country code table(#{lang}).\n"
end
end
return modified
end

@table : Hash(String, Hash(String, String))

def initialize()
@table = Hash(String, Hash(String, String)).new do |h,k|
def initialize
@table = Hash(String, Hash(String, String)).new do |h, k|
h[k] = Hash(String, String).new
end
Dir.glob(CACHE_DIR + "/*.yaml").each do |file|
lang = File.basename(file, ".yaml")
data = YAML.parse(File.read(file)).as_h
data.each do |cc, name|
@table[cc.as(String)][lang] = name.as(String)
@table[cc.as_s][lang] = name.as_s
end
end
end
Expand All @@ -57,7 +56,6 @@ class IP2Country::CC2Country
end

def lookup_all(cc : String) : Hash(String, String)
@table[cc]? || { "en" => "[UNKNOWN]" }
@table[cc]? || {"en" => "[UNKNOWN]"}
end

end
34 changes: 15 additions & 19 deletions src/ip2country/ip2cc.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class IP2Country::IP2CC

FILE_NAME = IP2Country::CACHE_DIR + "/conversion.dat"

def self.cache_update : Bool
Expand All @@ -14,42 +13,42 @@ class IP2Country::IP2CC
registorer, cc, ver, start_ip, ip_num, date, desc = tmp
min = IPAddr.new(start_ip)
max = min + (ip_num.to_u32 - 1)
table[min .. max] = cc
table[min..max] = cc
end
end
ranges = table.keys.sort{|a,b| a.begin <=> b.begin}
ranges = table.keys.sort { |a, b| a.begin <=> b.begin }
File.open(FILE_NAME, "w") do |fp|
while range = ranges.shift?
cc = table[range]
while next_range = ranges.first?
if range.end.succ == next_range.begin && table[next_range] == cc
range = range.begin .. next_range.end
range = range.begin..next_range.end
ranges.shift
else
break
end
end
fp.puts [range.begin, range.end, cc].join("\t")
fp << [range.begin, range.end, cc].join("\t")
end
end
STDERR.puts "[IP2Coutnry] IP to CC conversion table updated."
STDERR << "[IP2Coutnry] IP to CC conversion table updated.\n"
else
STDERR.puts "[IP2Coutnry] IP to CC conversion table not modified."
STDERR << "[IP2Coutnry] IP to CC conversion table not modified.\n"
end
return modified
end

@table : Hash(UInt8, Array(Tuple(Range(IPAddr, IPAddr), String)))

def initialize()
@table = Hash(UInt8, Array(Tuple(Range(IPAddr, IPAddr), String))).new do |h,k|
def initialize
@table = Hash(UInt8, Array(Tuple(Range(IPAddr, IPAddr), String))).new do |h, k|
h[k] = Array(Tuple(Range(IPAddr, IPAddr), String)).new
end
File.each_line(FILE_NAME) do |line|
min, max, cc = line.chomp.split("\t")
min_ip = IPAddr.new(min)
max_ip = IPAddr.new(max)
range = min_ip .. max_ip
range = min_ip..max_ip
(min_ip.octet(0)..max_ip.octet(0)).each do |class_a|
@table[class_a] << {range, cc}
end
Expand All @@ -64,7 +63,6 @@ class IP2Country::IP2CC
end

class Registrar

LIST = [] of self

CACHE_DIR = IP2Country::CACHE_DIR + "/registrar"
Expand All @@ -91,26 +89,26 @@ class IP2Country::IP2CC
def initialize(@name, uri_string : String)
@uri = URI.parse(uri_string)
@cache_file = CACHE_DIR + "/#{@name}.dat"
@mtime = File.exists?(@cache_file) ? File.stat(@cache_file).mtime : Time.new(2000, 1, 1)
@mtime = File.exists?(@cache_file) ? File.info(@cache_file).modification_time : Time.new(2000, 1, 1)
end

def cache_update : Bool
modified = false
http = HTTP::Client.new(@uri)
headers = HTTP::Headers.new
headers["If-modified-since"] = HTTP.rfc1123_date(@mtime)
headers["If-modified-since"] = HTTP.format_time(@mtime)
responce = http.get(@uri.path.not_nil!, headers)
case responce.status_code
when 200
STDERR.puts "[IP2Coutnry] IP allocation table of #{@name} is updated."
STDERR << "[IP2Coutnry] IP allocation table of #{@name} is updated.\n"
File.write(@cache_file, responce.body)
modified = true
when 304
STDERR.puts "[IP2Coutnry] IP allocation table of #{@name} is not modified."
STDERR << "[IP2Coutnry] IP allocation table of #{@name} is not modified.\n"
when 404
STDERR.puts "[IP2Coutnry] IP allocation table of #{@name} is not found."
STDERR << "[IP2Coutnry] IP allocation table of #{@name} is not found.\n"
else
STDERR.puts "[IP2Coutnry] Receive status code #{responce.status_code} for IP allocation table of #{@name}."
STDERR << "[IP2Coutnry] Receive status code #{responce.status_code} for IP allocation table of #{@name}.\n"
end
return modified
end
Expand All @@ -122,7 +120,5 @@ class IP2Country::IP2CC
IP2Country::REGISTRARS.each do |name, uri_string|
LIST << Registrar.new(name, uri_string)
end

end

end
3 changes: 1 addition & 2 deletions src/ip2country/ipaddr.cr
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct IP2Country::IPAddr
end

def octets
(0..3).map {|i| octet(i)}
(0..3).map { |i| octet(i) }
end

def inspect(io)
Expand All @@ -51,5 +51,4 @@ struct IP2Country::IPAddr
def to_s(io)
io << octets.join(".")
end

end

0 comments on commit 7b49b68

Please sign in to comment.