Ruby Webservice Client!
Date Nov 21, 2011 @ 19:44

Ruby is simple scripting language. Writing webservice client in Ruby is very simple Here I try to demonstrate a simple Webservice client. I found this Web Service at xmethods.net. There are many websevices are available here which we can use. I have selected one of those web service to get emergeny information of a city here in India. Here is the link for a webservice to get emergency information of Chennai.

There are following steps invlove here.

  • Install Ruby.
  • Install soap4r gem. Gem is like library. We use this library to make SOAP related call from Ruby. Go to <RUBY_HOME>/bin and execute the following command. gem install soap4r --include-dependencies

  • Start Interactive ruby(irb) by executing irb command from <RUBY_HOME>/bin irb is an interactive console where we can write small program and as soon as we stop entering the command it will promot the output.
    C:\Ruby193\bin>irb
    irb(main):001:0> require 'soap/wsdlDriver'
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/xsd/charset.rb:13: warning: variable $KCODE is no longer effective
    C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': iconv will be deprecated in the future, use String#encode instead.
    RuntimeError: XML processor module not found.
            from C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/xsd/xmlparser.rb:74:in `'
            from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
            from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
            from C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/wsdl/parser.rb:12:in `'
            from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
            from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
            from C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/soap/wsdlDriver.rb:9:in `'
            from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:59:in `require'
            from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:59:in `rescue in require'
            from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
            from (irb):1
            from C:/Ruby193/bin/irb:12:in `
    ' irb(main):002:0> require 'soap/wsdlDriver'
    Here is one simple bug. If you get this error then open <RUBY_HOME>\lib\ruby\gems\1.9.1\gems\soap4r-1.5.8\lib\xsd\xmlparser.rb and replace c.downcase == name with c.to_s.downcase == name Then try again with folwing command
    irb(main):002:0> require 'soap/wsdlDriver'
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/xsd/xmlparser.rb:22: warning: already initialized constant NSParseRegexp
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/soap/property.rb:68: warning: encoding option is ignored - u
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/soap/property.rb:69: warning: encoding option is ignored - u
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/soap/property.rb:70: warning: encoding option is ignored - u
    => true
    irb(main):003:0> wsdlLoc = 'http://chennaiemergency.co.in/sree/s2.php?wsdl'
    => "http://chennaiemergency.co.in/sree/s2.php?wsdl"
    irb(main):004:0> soapDriver = SOAP::WSDLDriverFactory.new(wsdlLoc).create_rpc_driver
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/soap/rpc/element.rb:226: warning: Hash#index is deprecated; use Hash#key
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/soap/rpc/element.rb:226: warning: Hash#index is deprecated; use Hash#key
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/soap/rpc/element.rb:226: warning: Hash#index is deprecated; use Hash#key
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/soap/rpc/element.rb:226: warning: Hash#index is deprecated; use Hash#key
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/soap/rpc/element.rb:226: warning: Hash#index is deprecated; use Hash#key
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/soap/rpc/element.rb:226: warning: Hash#index is deprecated; use Hash#key
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/soap/rpc/element.rb:226: warning: Hash#index is deprecated; use Hash#key
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/soap/rpc/element.rb:226: warning: Hash#index is deprecated; use Hash#key
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/soap/rpc/element.rb:226: warning: Hash#index is deprecated; use Hash#key
    => #>
    
    irb(main):006:0> puts "*****"+soapDriver.police(11.0,78.0) +"*****"
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/soap/rpc/element.rb:226: warning: Hash#index is deprecated; use Hash#key
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/soap/rpc/element.rb:226: warning: Hash#index is deprecated; use Hash#key
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/soap/rpc/element.rb:226: warning: Hash#index is deprecated; use Hash#key
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/soap/generator.rb:276: warning: encoding option is ignored - UTF8
    *****12.934697~80.151594~Chitlapakkam Police Station~S-12~2nd Main Road, Chitlapakkam~Near Varadha Raja Theatre~600064~22232005*****
    => nil
    irb(main):007:0>
    
    If you check the last line that was the output of the program. Same program can be written in a file and output can be checked there. Open the text editor and type following program there and save that file to test.rb.
    require 'soap/wsdlDriver'
    wsdlLoc = 'http://chennaiemergency.co.in/sree/s2.php?wsdl'
    soapDriver = SOAP::WSDLDriverFactory.new(wsdlLoc).create_rpc_driver
    puts "*****************"+ soapDriver.police(11.0,78.0) + "*************"
    
    Run the program from <RUBY_HOME>/bin as shown below.
    
    C:\Ruby193\bin>ruby.exe test.rb
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/xsd/charset.rb:13: warning: variable $KCODE is no longer effective
    C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': iconv will be deprecated in the future, use String#encode instead.
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/soap/property.rb:68: warning: encoding option is ignored - u
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/soap/property.rb:69: warning: encoding option is ignored - u
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/soap/property.rb:70: warning: encoding option is ignored - u
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/soap/rpc/element.rb:226: warning: Hash#index is deprecated; use Hash#key
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/soap/rpc/element.rb:226: warning: Hash#index is deprecated; use Hash#key
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/soap/rpc/element.rb:226: warning: Hash#index is deprecated; use Hash#key
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/soap/rpc/element.rb:226: warning: Hash#index is deprecated; use Hash#key
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/soap/rpc/element.rb:226: warning: Hash#index is deprecated; use Hash#key
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/soap/rpc/element.rb:226: warning: Hash#index is deprecated; use Hash#key
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/soap/rpc/element.rb:226: warning: Hash#index is deprecated; use Hash#key
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/soap/rpc/element.rb:226: warning: Hash#index is deprecated; use Hash#key
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/soap/rpc/element.rb:226: warning: Hash#index is deprecated; use Hash#key
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/soap/rpc/element.rb:226: warning: Hash#index is deprecated; use Hash#key
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/soap/rpc/element.rb:226: warning: Hash#index is deprecated; use Hash#key
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/soap/rpc/element.rb:226: warning: Hash#index is deprecated; use Hash#key
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/soap4r-1.5.8/lib/soap/generator.rb:276: warning: encoding option is ignored - UTF8
    *****************12.934697~80.151594~Chitlapakkam Police Station~S-12~2nd Main Road, Chitlapakkam~Near Varadha Raja Theatre~600064~22232005*************
    
    
    Last line shows output of the program.