class SearchApnic::Search

Public Class Methods

countries() click to toggle source
# File lib/search_apnic/search.rb, line 47
def self.countries
  %w{
    AP AS AU BD BN BR BT
    CA CK CN CO
    DE
    ES
    FJ FM FR
    GB GU
    HK
    ID IN IO IR
    JP
    KE KH KI KP KR
    LA LK
    MH MM MN MO MP MU MV MY
    NC NF NL NP NR NU NZ
    PF PG PH PK PW
    SA SB SC SE SG
    TH TK TL TO TR TV TW
    US
    VN VU
    WF WS
    ZA
  }
end

Public Instance Methods

by_country(country, protocol_version: 'ipv4') click to toggle source

File Format ftp.apnic.net/apnic/stats/apnic/README.TXT

# File lib/search_apnic/search.rb, line 6
def by_country(country, protocol_version: 'ipv4')
  ::File.foreach(SearchApnic::File.new.get_path) do |l|
    next if l[0] == "#" # Comment Line

    row = l.strip.split("|")

    unless header
      set_header(row)
      next
    end

    if(row[5] == 'summary')
      add_summary(row)
      next
    end

    record = parse_record(row)
    next if record[:type] == 'asn' # Autonomous System number
    next if record[:cc] != country
    next if record[:type] != protocol_version # ipv4 or ipv6
    add_record record
  end
  self
end
header() click to toggle source
# File lib/search_apnic/search.rb, line 80
def header
  @header
end
include?(ip) click to toggle source
# File lib/search_apnic/search.rb, line 37
def include?(ip)
  search_first_number = ip.split(".").first
  records.select{|ip_and_mask| search_first_number == ip_and_mask.split(".").first}.each do |ip_and_mask|
    if IPAddr.new(ip_and_mask).include? ip
      return ip_and_mask
    end
  end
  return false
end
print() click to toggle source
records() click to toggle source
# File lib/search_apnic/search.rb, line 72
def records
  @records
end
summary() click to toggle source
# File lib/search_apnic/search.rb, line 76
def summary
  @summary
end

Private Instance Methods

add_record(record) click to toggle source
# File lib/search_apnic/search.rb, line 86
def add_record(record)
  @records ||= []
  number_of_ip = record[:value].to_i
  start = record[:start]
  mask = 32 - ("%b" % number_of_ip).length + 1
  ip = "#{start}/#{mask}"

  @records << ip
end
add_summary(row) click to toggle source
# File lib/search_apnic/search.rb, line 109
def add_summary(row)
  @summary ||= []
  @summary << {
    registry: row.shift,
    noop1:    row.shift,
    type:     row.shift,
    noop2:    row.shift,
    count:    row.shift,
    summary:  row.shift,
  }
end
parse_record(row) click to toggle source
# File lib/search_apnic/search.rb, line 96
def parse_record(row)
  {
    registry: row.shift,
    cc: row.shift,
    type: row.shift,
    start: row.shift,
    value: row.shift,
    date: row.shift,
    status: row.shift,
    extensions: row
  }
end
set_header(row) click to toggle source
# File lib/search_apnic/search.rb, line 121
def set_header(row)
  @header = {
    version: row.shift,
    registry: row.shift,
    serial: row.shift,
    records: row.shift,
    startdate: row.shift,
    enddate: row.shift,
    utcoffset: row.shift
  }
end