]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/edi_translator/edi_webrick.rb
LP1402770_column_picker_option_for_number_of_holds
[Evergreen.git] / Open-ILS / src / edi_translator / edi_webrick.rb
1 #!/usr/bin/env ruby
2
3 require 'rubygems'
4 require 'optparse'
5 require 'parseconfig'
6 require 'stringio'
7 require 'webrick'
8 require 'xmlrpc/server'
9
10 require 'openils/mapper'
11 require 'edi/edi2json'
12 # require 'edi/mapper'
13
14 base = File.basename($0, '.rb')
15
16 # Defaults
17 defaults = {
18   'port'      => 9191,
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
22 }
23
24 options = {
25 #  'defcon'    => 3,
26 }
27
28 # Parse command line to override defaults
29
30 OptionParser.new do |opts|
31   opts.banner = "Usage: #{$0} [options]"
32
33   opts.on("-c", "--config [/path/to/file]", "Config file           (default: #{defaults['config']})"   ) do |x|
34     options['config'] = x
35   end
36
37   opts.on("-n", "--namespace [/web/path]",  "Web request namespace (default: #{defaults['namespace']})") do |x|
38     options['namespace'] = x
39   end
40
41   opts.on("-p", "--port [PORT]",            "Port number           (default: #{defaults['port']})"     ) do |x|
42     options['port'] = x
43   end
44
45   opts.on("-v", "--[no-]verbose", "Set verbosity") do |x|
46     options['verbose'] = x
47   end
48
49   opts.on("-d", "--defaults",   "Show defaults") do |x|
50     puts "## Default values:"
51     defaults.each { |key, value| printf "%10s = %s\n", key, value }
52     exit
53   end
54
55   opts.on("-D", "--dumpconfig", "Show effective settings from command-line, config file or defaults") do |x|
56     options['dumpconfig'] = x
57   end
58
59   opts.on_tail("-h", "--help",  "Show this message") do
60     puts opts
61     exit
62   end
63
64 end.parse!
65
66 if options['verbose']
67   puts "OPTIONS: " ; p options
68   puts "Reading config file #{options['config'] || defaults['config']}"
69   # puts "\n ARGV: " ; p ARGV
70 end
71
72 # Read config file, then integrate 
73 c = ParseConfig.new(options['config'] || defaults['config'])
74 # puts c.methods().sort ; print "\n\n"
75
76 keylist = ["host", "port", "config", "namespace", "verbose"] | c.get_params() | defaults.keys | options.keys
77
78 for key in keylist
79   src =  options.has_key?(key) ? 'command-line' : \
80               c.get_value(key) ? 'config file'  : \
81         defaults.has_key?(key) ? 'default'      : 'NOWHERE!'
82
83   options[key] ||= c.get_value(key) || defaults[key]
84   printf "%10s = %-22s (%12s)\n", key, options[key], src if options['dumpconfig']
85 end
86
87 # after this, all values we care about are in the options hash
88
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'
99   interchange.to_json
100 }
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)
104   @map.to_s
105 }
106 servlet.add_introspection
107
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)
112 server.start
113