Permalink
Browse files

Hold the date of the eTLD database and use it to detect modification.

Run `rake etld_data' to update the database.
  • Loading branch information...
1 parent adfb6c4 commit 355ee99d073b4b243c72f4cbebc37bd65dbc9175 @knu committed Jan 7, 2013
Showing with 43 additions and 18 deletions.
  1. +37 −18 Rakefile
  2. +2 −0 lib/domain_name/etld_data.rb
  3. +2 −0 lib/domain_name/etld_data.rb.erb
  4. +2 −0 tool/gen_etld_data.rb
View
@@ -1,31 +1,50 @@
require 'bundler/gem_tasks'
+require 'uri'
-require 'rake/testtask'
-Rake::TestTask.new(:test) do |test|
- test.libs << 'lib' << 'test'
- test.pattern = 'test/**/test_*.rb'
- test.verbose = true
-end
+ETLD_DATA_URI = URI('http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/effective_tld_names.dat?raw=1')
+ETLD_DATA_FILE = 'data/effective_tld_names.dat'
+ETLD_DATA_RB = 'lib/domain_name/etld_data.rb'
task :default => :test
-task :test => 'lib/domain_name/etld_data.rb'
+task :test => ETLD_DATA_RB
-etld_dat = 'data/effective_tld_names.dat'
+file ETLD_DATA_RB => [
+ ETLD_DATA_FILE,
+ ETLD_DATA_RB + '.erb',
+ 'tool/gen_etld_data.rb'
+] do
+ ruby 'tool/gen_etld_data.rb'
+end
-file etld_dat do
+task :etld_data do
require 'open-uri'
- File.open(etld_dat, 'w') { |dat|
- dat.print URI('http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/effective_tld_names.dat?raw=1').read
- }
+ require 'time'
+
+ begin
+ load File.join('.', ETLD_DATA_RB)
+
+ data = ETLD_DATA_URI.read(
+ 'If-Modified-Since' => Time.parse(DomainName::ETLD_DATA_DATE).rfc2822
+ )
+ puts 'eTLD database is modified.'
+ File.write(ETLD_DATA_FILE, data)
+ File.utime Time.now, data.last_modified, ETLD_DATA_FILE
+ Rake::Task[ETLD_DATA_RB].execute
+ rescue OpenURI::HTTPError => e
+ if e.io.status.first == '304' # Not Modified
+ puts 'eTLD database is up-to-date.'
+ else
+ raise
+ end
+ end
end
-file 'lib/domain_name/etld_data.rb' => [
- etld_dat,
- 'lib/domain_name/etld_data.rb.erb',
- 'tool/gen_etld_data.rb'
-] do
- ruby 'tool/gen_etld_data.rb'
+require 'rake/testtask'
+Rake::TestTask.new(:test) do |test|
+ test.libs << 'lib' << 'test'
+ test.pattern = 'test/**/test_*.rb'
+ test.verbose = true
end
require 'rdoc/task'
@@ -1,4 +1,6 @@
class DomainName
+ ETLD_DATA_DATE = '2012-12-21T00:00:53Z'
+
ETLD_DATA = {
"ac" => 0,
"com.ac" => 0,
@@ -1,4 +1,6 @@
class DomainName
+ ETLD_DATA_DATE = '<%= etld_data_date.utc.strftime('%Y-%m-%dT%H:%M:%SZ') %>'
+
ETLD_DATA = {
<% etld_data.each_pair { |key, value| %> <%= key.inspect %> => <%= value.inspect %>,
<% } %> }
View
@@ -14,6 +14,8 @@ def main
erb_file = dir + 'etld_data.rb.erb'
rb_file = dir + 'etld_data.rb'
+ etld_data_date = File.mtime(dat_file)
+
File.open(dat_file, 'r:utf-8') { |dat|
etld_data = parse(dat)
File.open(rb_file, 'w:utf-8') { |rb|

0 comments on commit 355ee99

Please sign in to comment.