Last active
September 21, 2021 18:39
-
-
Save COOLIRON2311/3d0dee2c63a4c1e8db4785888e26cf14 to your computer and use it in GitHub Desktop.
Small IBExpert query set converter written in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'ibe2txt' | |
begin | |
if !ARGV.empty? | |
if File.exist?(ARGV[0]) | |
if File.extname(ARGV[0]) == '.xml' | |
IBE2Txt.new(ARGV[0]) | |
else | |
puts 'Not an xml file' | |
puts IBE2Txt::USAGE | |
end | |
else | |
puts 'No such file' | |
puts IBE2Txt::USAGE | |
end | |
else | |
puts IBE2Txt::USAGE | |
end | |
rescue StandardError => e | |
puts e | |
puts IBE2Txt::USAGE | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require 'base64' | |
require 'nokogiri' | |
class IBE2Txt | |
USAGE = 'Usage: ibe2txt file.xml' | |
private | |
attr_accessor :path, :doc, :captions, :sources | |
def initialize(path) | |
@path = path | |
@captions = [] | |
@sources = [] | |
@doc = File.open(@path, 'r') { |f| Nokogiri::XML(f) } | |
parse | |
write_back | |
end | |
def parse | |
@captions = @doc.xpath('/ibe_sql_queries/queryset/query/caption').map { |x| Base64.decode64(x.content) } | |
@sources = @doc.xpath('/ibe_sql_queries/queryset/query/source').map { |x| Base64.decode64(x.content) } | |
# puts @sources, @captions | |
end | |
def write_back | |
File.open("#{File.join(File.dirname(@path), File.basename(@path, '.xml'))}.txt", 'w') do |f| | |
@captions.zip(@sources).each do |c, s| | |
line = "#{c}\n#{s}\n" | |
f << line.encode(line.encoding, universal_newline: true) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment