Sha256: a421ef5a4202d37ef03862fe7a0326d18c5b86587a340422eb2d8efa44135722
Contents?: true
Size: 1.59 KB
Versions: 5
Compression:
Stored size: 1.59 KB
Contents
require_relative "initialize_with_redis" require "csv" class Trifle class Loader include InitializeWithRedis def handle options = {} if options[:filename] load_files [options[:filename]] elsif options[:filenames] load_files options[:filenames] elsif options[:data] load_data options[:data] else raise ArgumentError.new("Please provide a :filename, :filenames or an array of :data as an argument.") end end def load_files filenames raise ArgumentError.new("filenames must be an array of strings") unless filenames.is_a?(Array) && !filenames.map{|element| element.is_a?(String)}.include?(false) data = [] filenames.each do |filename| contents = File.open(filename, "rb").read data += parse(contents) end load_data data end def load_data data raise ArgumentError.new("data must be an array as loaded from a GeoIP data set") unless valid?(data) clear sort(data) data.each {|row| append(row) } end def append row entry = row.values_at(2,3,4,5).join(":") redis.rpush key, entry end def sort data data.sort {|a,b| a[2] <=> b[2] } end def parse contents contents.gsub!('", "', '","') CSV.parse(contents) end def valid? data if data.is_a?(Array) && data.count > 0 return data.detect {|row| !is_number(row[2]) || !is_number(row[3])}.nil? end false end def is_number field field.is_a?(Numeric) || field =~ /^\d+$/ end def clear redis.del key end end end
Version data entries
5 entries across 5 versions & 1 rubygems
Version | Path |
---|---|
trifle-0.0.6 | lib/trifle/loader.rb |
trifle-0.0.5 | lib/trifle/loader.rb |
trifle-0.0.4 | lib/trifle/loader.rb |
trifle-0.0.3 | lib/trifle/loader.rb |
trifle-0.0.2 | lib/trifle/loader.rb |