8 require 'xmlrpc/server'
10 require 'openils/mapper'
11 require 'edi/edi2json'
12 # require 'edi/mapper'
14 base = File.basename($0, '.rb')
19 'config' => "./#{base}.cnf",
20 'namespace' => "/EDI",
21 # 'defcon' => 0, # uncomment the 2 decons here, and one in the config file to see the collision/interaction
28 # Parse command line to override defaults
30 OptionParser.new do |opts|
31 opts.banner = "Usage: #{$0} [options]"
33 opts.on("-c", "--config [/path/to/file]", "Config file (default: #{defaults['config']})" ) do |x|
37 opts.on("-n", "--namespace [/web/path]", "Web request namespace (default: #{defaults['namespace']})") do |x|
38 options['namespace'] = x
41 opts.on("-p", "--port [PORT]", "Port number (default: #{defaults['port']})" ) do |x|
45 opts.on("-v", "--[no-]verbose", "Set verbosity") do |x|
46 options['verbose'] = x
49 opts.on("-d", "--defaults", "Show defaults") do |x|
50 puts "## Default values:"
51 defaults.each { |key, value| printf "%10s = %s\n", key, value }
55 opts.on("-D", "--dumpconfig", "Show effective settings from command-line, config file or defaults") do |x|
56 options['dumpconfig'] = x
59 opts.on_tail("-h", "--help", "Show this message") do
67 puts "OPTIONS: " ; p options
68 puts "Reading config file #{options['config'] || defaults['config']}"
69 # puts "\n ARGV: " ; p ARGV
72 # Read config file, then integrate
73 c = ParseConfig.new(options['config'] || defaults['config'])
74 # puts c.methods().sort ; print "\n\n"
76 keylist = ["host", "port", "config", "namespace", "verbose"] | c.get_params() | defaults.keys | options.keys
79 src = options.has_key?(key) ? 'command-line' : \
80 c.get_value(key) ? 'config file' : \
81 defaults.has_key?(key) ? 'default' : 'NOWHERE!'
83 options[key] ||= c.get_value(key) || defaults[key]
84 printf "%10s = %-22s (%12s)\n", key, options[key], src if options['dumpconfig']
87 # after this, all values we care about are in the options hash
89 # create a servlet to handle XML-RPC requests:
90 servlet = XMLRPC::WEBrickServlet.new
91 servlet.add_handler("mapper_version") { OpenILS::Mapper::VERSION }
92 servlet.add_handler("upper_case") { |a_string| a_string.upcase }
93 servlet.add_handler("lower_case") { |a_string| a_string.downcase }
94 servlet.add_handler("edi2json" ) { |a_string|
95 File.open('/tmp/ruby_edi2json.tmp', 'w') {|f| f.write(a_string) } # debugging, so we can compare what we rec'd w/ the orig. file
96 interchange = StringIO.open(a_string){ |io| EDI::E::Interchange.parse(io) }
97 # interchange.header.cS002.d0004 = 'sender'
98 # interchange.header.cS003.d0010 = 'recipient'
101 servlet.add_handler("json2edi" ) { |a_string|
102 File.open('/tmp/ruby_json2edi.tmp', 'w') {|f| f.write(a_string) } # debugging, so we can compare what we rec'd w/ the orig. file
103 @map = OpenILS::Mapper.from_json(a_string)
106 servlet.add_introspection
108 # create a WEBrick instance to host the servlets
109 server = WEBrick::HTTPServer.new(:Port => options['port'])
110 trap("INT"){ server.shutdown }
111 server.mount(options['namespace'], servlet)