Newer
Older
csvlint-webapp / app.rb
require 'rubygems'
require 'bundler/setup'
require 'sinatra'
require 'csvlint'

set :bind, '0.0.0.0'

get '/' do
  erb :index
end

post '/validate' do
  tmp_dir = Dir::mktmpdir('csvlint')
  tidy_tmpfile = params[:tidyDataFile][:tempfile]
  tidy_filename = File::join(tmp_dir, params[:tidyDataFile][:filename].gsub(/[\x00-\x1f\/|?*]/, '_'))
  File.open(tidy_filename, 'wb') do |tf|
    tf.write(tidy_tmpfile.read)
  end
  schema_tmpfile = params[:schemaFile][:tempfile]
  schema_filename = File::join(tmp_dir, params[:schemaFile][:filename].gsub(/[\x00-\x1f\/|?*]/, '_'))
  File.open(schema_filename, 'wb') do |sf|
    sf.write(schema_tmpfile.read)
  end
  csvw_schema = Csvlint::Schema.load_from_uri(schema_filename)
  valid = true
  @validator = nil
  csvw_schema.tables.keys.each do |source|
    @validator = Csvlint::Validator.new(source, {}, csvw_schema)
    valid &= @validator.valid?
  end
  erb :results
end