class Csv2schema::Validator
Constants
- FORMATS
Public Instance Methods
build_formats(row)
click to toggle source
# File lib/csv2schema/format_builder.rb, line 4 def build_formats(row) row.each_with_index do |col, i| next if col.nil? || col.empty? @formats[i] ||= Hash.new(0) # require "pry" ; binding.pry format = if float?(col) :float elsif col.strip[FORMATS[:boolean]] :boolean elsif uri?(col) :uri elsif possible_date?(col) date_formats(col) elsif col.strip[FORMATS[:numeric]] :numeric else :string end @formats[i][format] += 1 end end
date_formats(col)
click to toggle source
# File lib/csv2schema/format_builder.rb, line 34 def date_formats(col) if col[FORMATS[:date_db]] && date_format?(Date, col, '%Y-%m-%d') :date_db elsif col[FORMATS[:date_short]] && date_format?(Date, col, '%e %b') :date_short elsif col[FORMATS[:date_rfc822]] && date_format?(Date, col, '%e %b %Y') :date_rfc822 elsif col[FORMATS[:date_long]] && date_format?(Date, col, '%B %e, %Y') :date_long elsif col[FORMATS[:dateTime_time]] && date_format?(Time, col, '%H:%M') :dateTime_time elsif col[FORMATS[:dateTime_hms]] && date_format?(Time, col, '%H:%M:%S') :dateTime_hms elsif col[FORMATS[:dateTime_db]] && date_format?(Time, col, '%Y-%m-%d %H:%M:%S') :dateTime_db elsif col[FORMATS[:dateTime_iso8601]] && date_format?(Time, col, '%Y-%m-%dT%H:%M:%SZ') :dateTime_iso8601 elsif col[FORMATS[:dateTime_short]] && date_format?(Time, col, '%d %b %H:%M') :dateTime_short elsif col[FORMATS[:dateTime_long]] && date_format?(Time, col, '%B %d, %Y %H:%M') :dateTime_long elsif col[FORMATS[:date_month]] && date_format?(Date, col, '%Y-%m') :date_month elsif col.strip[FORMATS[:numeric]] :numeric else :string end end
float?(col)
click to toggle source
# File lib/csv2schema/format_builder.rb, line 30 def float?(col) Float(col).to_s == col rescue false end