class Ryodo::SuffixListFetcher

Public Class Methods

fetch_and_save!(uri = Ryodo::PUBLIC_SUFFIX_DATA_URI, store = Ryodo::PUBLIC_SUFFIX_STORE) click to toggle source
# File lib/ryodo/suffix_list_fetcher.rb, line 10
def fetch_and_save!(uri = Ryodo::PUBLIC_SUFFIX_DATA_URI, store = Ryodo::PUBLIC_SUFFIX_STORE)
  fetcher = new(uri, store)
  fetcher.fetch_data
  fetcher.prepare_data
  fetcher.save_data
  true
rescue
  false
end
new(uri = Ryodo::PUBLIC_SUFFIX_DATA_URI, store = Ryodo::PUBLIC_SUFFIX_STORE) click to toggle source
# File lib/ryodo/suffix_list_fetcher.rb, line 21
def initialize(uri = Ryodo::PUBLIC_SUFFIX_DATA_URI, store = Ryodo::PUBLIC_SUFFIX_STORE)
  @uri = URI.parse(uri)
  @store = store
end

Public Instance Methods

fetch_data() click to toggle source
# File lib/ryodo/suffix_list_fetcher.rb, line 26
def fetch_data
  http         = Net::HTTP.new(@uri.host, @uri.port)
  http.use_ssl = @uri.scheme == "https"
  request      = Net::HTTP::Get.new(@uri.request_uri)
  response     = http.request(request)
  fail Ryodo::FetchError, "Could not fetch suffix data! (#{response})" unless response.is_a?(Net::HTTPSuccess)
  @fetched_data = response.body.lines
end
prepare_data() click to toggle source
# File lib/ryodo/suffix_list_fetcher.rb, line 35
def prepare_data
  @prepared_data = @fetched_data.inject([]) do |acc, line|
    next(acc) if line =~ %r{\A//|\A\n}
    acc << reverse_dn(line)
  end.sort
end
save_data() click to toggle source
# File lib/ryodo/suffix_list_fetcher.rb, line 42
def save_data
  File.open(Ryodo::PUBLIC_SUFFIX_STORE, "w") do |fh|
    fh.write @prepared_data.join("\n")
  end if @prepared_data
end

Private Instance Methods

reverse_dn(domain_name) click to toggle source
# File lib/ryodo/suffix_list_fetcher.rb, line 50
def reverse_dn(domain_name)
  # "foo.bar.baz" => "baz.bar.foo"
  domain_name.strip.split(".").reverse.join(".")
end