Class: Ryodo::SuffixListFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/ryodo/suffix_list_fetcher.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri = Ryodo::PUBLIC_SUFFIX_DATA_URI, store = Ryodo::PUBLIC_SUFFIX_STORE) ⇒ SuffixListFetcher

Returns a new instance of SuffixListFetcher.



21
22
23
24
# 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

Class Method Details

.fetch_and_save!(uri = Ryodo::PUBLIC_SUFFIX_DATA_URI, store = Ryodo::PUBLIC_SUFFIX_STORE) ⇒ Object



10
11
12
13
14
15
16
17
18
# 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

Instance Method Details

#fetch_dataObject



26
27
28
29
30
31
32
33
# 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_dataObject



35
36
37
38
39
40
# 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_dataObject



42
43
44
45
46
# 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