module DNSCheck::Update

Attributes

indice_location[RW]
indice_store[RW]

Public Instance Methods

update!() click to toggle source
# File lib/dns-check/update.rb, line 8
def update!
  print "-> downloading... (please wait)"

  http do |conn|
    request = Net::HTTP::Get.new(indice_location.request_uri, {})

    buffer = ''

    conn.request request do |res|
      case res
      when Net::HTTPNotModified
        puts "No new content available"
      when Net::HTTPRedirection
        # Do not expect a redirect...
        puts "Redirecting to #{res['Location']}... aborting! open a github issue"
      when Net::HTTPOK
        begin
          res.read_body do |buf|
            buffer << buf
          end
          save buffer
        rescue Exception => e
          raise e
        ensure
          #TODO clean exit?
        end
      when Net::HTTPNotFound
        raise DNSCheck::NotFoundError, "http code 404 - open a github issue"
      else
        raise RuntimeError, "Update failed... #{res.message}"
      end
    end
  end
rescue => e
  raise DNSCheck::UpdateError, "the indice update failed... #{e.message}"
end

Private Instance Methods

http() { |http| ... } click to toggle source
# File lib/dns-check/update.rb, line 52
def http
  yield Net::HTTP.new(indice_location.hostname, 80)
end
sanetize_indice(buffer) click to toggle source
# File lib/dns-check/update.rb, line 56
def sanetize_indice buffer
  indice_records = JSON.parse(buffer)
  # Create new array, faster than calling Array#delete on indice_records
  new_indice_records = DNSCheck::DB.new

  indice_records.each do |record|
    if record['state'] == "valid" and !record['country_id'].nil?
      new_indice_records[record['country_id']] = {
        record['city'] => [
          record['ip'],
          record['name']
        ]
      }
    end
  end
  print " - done\n";
  return new_indice_records
rescue => e
  #TODO raise exception, i.e: upstream key format changed!
end
save(buffer) click to toggle source
# File lib/dns-check/update.rb, line 47
def save buffer
  object = sanetize_indice buffer
  DNSCheck.store object, indice_store
end