Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'rubygems'
- require 'cgi'
- require 'fastercsv'
- class LogRow
- attr_accessor :row
- def ip_address
- @row[0]
- end
- def time_stamp
- @row[1]
- end
- def request
- @row[2].split(' ')
- end
- def user_agent
- @row[3]
- end
- def initialize(row)
- @row = row
- end
- #methods
- def uri
- CGI.unescape(self.request[1])
- end
- def has_valid_email?
- self.uri =~ /e=((?#user_name)[^@\s]+)@((?#domain_name)(?:[-a-z0-9]+\.)+[a-z]{2,})/i
- end
- end
- # minor issue, but typically ruby uses underscore for variables, as in:
- # log_rows = Array.new
- # also, you can use even simpler syntax of:
- # log_rows = []
- class Log_Rows
- attr_accessor :file_path
- attr_reader :invalid_rows, :valid_rows
- def initialize(file_path)
- @file_path = file_path
- @valid_rows = populate_valid_rows
- @invalid_rows = populate_invalid_rows
- end
- def populate_invalid_rows
- l = []
- FasterCSV.foreach(self.file_path) do |row|
- new_row = LogRow.new(row)
- if new_row.has_valid_email? == nil then
- l.push(new_row)
- end
- end
- return l
- end
- def populate_valid_rows
- l = []
- FasterCSV.foreach(self.file_path) do |row|
- new_row = LogRow.new(row)
- if new_row.has_valid_email? then
- l.push(new_row)
- end
- end
- return l
- end
- end
- log = Log_Rows.new("/Users/andrewblass/Documents/Ruby/creature_log.csv")
- log.invalid_rows.each do |row|
- puts row.user_agent
- end
Add Comment
Please, Sign In to add comment