sroller icon

Savon + Nokogiri

sroller | PRO | 10/19/18 12:54:11 PM UTC | 0 ⭐ | 7296 👁️ | Never ⏰ | []
Ruby |

960 B

|

None

|

0 👍

/

0 👎

#!/usr/bin/env ruby
 
# using a now defunct service
 
gem "savon", "~>2.0"
require "savon"
require "nokogiri"
require 'pp'
 
soap = Savon.client(
  wsdl: "http://www.webservicex.net/globalweather.asmx?wsdl",
  convert_request_keys_to: :camelcase,
  log: false,
  log_level: :debug,
  pretty_print_xml: true
)
 
country = (ARGV[0] || 'Canada').capitalize
 
STDERR.print "Listing cities in #{country}\n"
response = soap.call(:get_cities_by_country,
                     :message => { :country_name => country },
                    )
result = response.to_hash[:get_cities_by_country_response][:get_cities_by_country_result]
doc = Nokogiri::XML(result)
# print doc.to_xml(indent: 2)
cities = doc.xpath("//Table")
cities.sort{|a,b| a.css('City').text <=> b.css('City').text}.each do |city|
  # two variants to access elements
  # print "#{(city>'City').text} (#{(city>'Country').text})\n"
  print "\"#{city.css('City').text}\" (#{city.css('Country').text})\n"
end

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎