tools/geo-ip-summary.rb in dap-0.0.1 vs tools/geo-ip-summary.rb in dap-0.0.2
- old
+ new
@@ -14,14 +14,22 @@
@city_name = city_name
@tree = {}
@tree['count'] = 0
end
+ def stringify(o)
+ if o.kind_of?( ::String )
+ o.to_s.encode(o.encoding, "UTF-8", :invalid => :replace, :undef => :replace, :replace => '')
+ else
+ o.to_s
+ end
+ end
+
def process_hash( json_hash )
- country = json_hash[@country_name]
- region = json_hash[@region_name] || 'Undefined Region'
- city = json_hash[@city_name] || 'Undefined City'
+ country = stringify( json_hash[@country_name] )
+ region = stringify( json_hash[@region_name] || 'Undefined Region' )
+ city = stringify( json_hash[@city_name] || 'Undefined City' )
# Create subhashes and values as needed on down the tree
@tree[country] ||= {}
@tree[country]['count'] ||=0
@tree[country][region] ||= {}
@@ -121,11 +129,11 @@
end
opts.on('--var top-level-var', 'Sets the top level json name, for defining all of country/region/city') do | val |
options[:var] = val
options[:country] = "#{val}.country_name"
- options[:region] = "#{val}.region"
+ options[:region] = "#{val}.region_name"
options[:city] = "#{val}.city"
end
opts.on_tail('-h', '--help', 'Show this message') do
puts opts
@@ -134,16 +142,22 @@
opts.parse!(args)
options
end
options
end
- opts = parse_command_line(ARGV)
- raise 'Need json key names for country,region and city.' if opts[:country].nil? || opts[:region].nil? || opts[:city].nil?
+opts = parse_command_line(ARGV)
- summarizer = GeoIPSummary.new(opts[:country], opts[:region], opts[:city])
- while line=gets
- summarizer.process_hash(Oj.load(line.strip))
- end
- Oj.default_options={:indent=>2}
+raise 'Need json key names for country,region and city.' if opts[:country].nil? || opts[:region].nil? || opts[:city].nil?
- puts Oj.dump(summarizer.order_tree)
+summarizer = GeoIPSummary.new(opts[:country], opts[:region], opts[:city])
+
+
+$stdin.each_line do |line|
+ json = Oj.load(line.unpack("C*").pack("C*").strip) rescue nil
+ next unless json
+ summarizer.process_hash(json)
+end
+
+Oj.default_options={:indent=>2}
+
+puts Oj.dump(summarizer.order_tree)