Sha256: 662710ddbcccec4c354295b69b008699a7b1b94d4e634cf6436eff1bfca6b012

Contents?: true

Size: 928 Bytes

Versions: 3

Compression:

Stored size: 928 Bytes

Contents

#!/usr/bin/env ruby
require 'ipaddr'
require 'set'

class GlobetrotterFile
  def initialize(file)
    @set = Set.new
    @file = open(file)
    parse
  end

  attr_accessor :set

  def open(file)
    begin
      file = File.open(file, File::RDONLY|File::CREAT)
    rescue StandardError => e
      abort e.to_s
    end
    file
  end # open

  def parse
    self.open if @file.nil?
    line_number = 0
    # parse line for IP
    @file.each_line do |line|
      line_number += 1
      begin
        ip = IPAddr.new(line.chomp)
        @set.add(ip)
      rescue StandardError => e
        abort "#{e} on line #{line_number}: '#{line}'"
      end
    end
    @file.close unless @file.nil?
    self
  end # parse

  def write
    begin
      File.open(@file, 'w') do |f|
        f.puts(@set.to_a.sort.join("\n"))
      end
    rescue StandardError => e
      abort e.to_s
    end
  end # write

  def to_s
    @file.path
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
globetrotter-0.0.12 lib/globetrotter/file.rb
globetrotter-0.0.11 lib/globetrotter/file.rb
globetrotter-0.0.10 lib/globetrotter/file.rb