Skip to content

Instantly share code, notes, and snippets.

@tfuji
Created September 22, 2020 05:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tfuji/7b824f6a6f7e542ed45928b4d455c7ac to your computer and use it in GitHub Desktop.
Save tfuji/7b824f6a6f7e542ed45928b4d455c7ac to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'json'
require 'pp'
# ruby metabobank-valdator.rb MBEditor_0.5.2_export_mb_mbcheck200914_MTBKS11.json
# See https://ddbj-dev.atlassian.net/browse/MB-88
class MetabobankValidator
def initialize file, options
@file = file
@options = {}
@template = JSON.parse(File.read(File.dirname(__FILE__) + "/200911_MBE/config/template.json"))
@action = JSON.parse(File.read(File.dirname(__FILE__) + "/200911_MBE/config/action.json"))
check_for_json_format
check_for_each_sheet
end
#Check for JSON format
def check_for_json_format
puts "### #{__method__.to_s}"
begin
@data = JSON.parse(File.read(@file))
rescue Exception => ex
puts ex.backtrace.first + ": #{ex.message} (#{ex.class})"
puts "perse error\t#{ex.message}"
exit
end
end
def check_for_each_sheet
puts "### #{__method__.to_s}"
@template.each_with_index do |t,i|
error = 0
warning = 0
sheet_id = t['sheet_id']
flag_sheet = false
flag_data = false
count = 0
data = []
if hash = @data['metadata'].find { |s| s['sheet_id'] == sheet_id }
flag_sheet = true
if sheet_data = hash['data']
flag_data = true
data = sheet_data.shift.values
count = data.size
end
end
warning += 1 if flag_sheet == false
warning += 1 if flag_sheet == true and flag_data == false
contents = t['contents'].map { |c| [c['label_id'], c] }.to_h
properties =[]
data.each do |d|
data_id = d['id']
contents.merge(d){|key, v0, v1|
v0['_data'] = v1; v0
}.each do |k,v|
label_hash = {
:sheet_id => sheet_id,
:data_id => data_id,
:target => 'property',
:property_id => k,
:values => v['_data'],
:type => v['label_type'],
:filter => v['filter']['required'],
:validation_type => v['validation']
}
properties.push(label_hash) if ( v['_data'].nil? and v['filter']['required'] == 'yes')
#puts JSON.pretty_generate(label_hash) if ( v['_data'].nil? and v['filter']['required'] == 'yes')
end
end
error += properties.size
status = {:warning => warning, :error => error }
hash = {
:status => status,
:sheet_id => sheet_id,
:target => 'sheet',
:bool_sheet => flag_sheet,
:bool_data => flag_data,
#:contents => t['contents'],
#:with_data => data,
:data_count => count,
:error_properties => properties,
:method => __method__
}
puts JSON.pretty_generate(hash) if error > 0
end
end
end
input_file = ARGV[0]
options ={}
MetabobankValidator.new(input_file, options)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment