Undefined Method and using gsub

6 views
Skip to first unread message

Michael Fischer

unread,
Mar 16, 2015, 7:21:32 AM3/16/15
to nokogi...@googlegroups.com
Hello all. I have my code able to successfully print out a weather report using Nokogiri from the Aviationweather.gov website. 

The code prompts the user to input a weather station name, for example: KORD =>(This is the code for Chicago Airport) It puts this variable to upcase method then inserts it into a Nokogiri Open url

This gathers all the information at the XML location and puts it into doc

Once there. I am having trouble using and filtering the data with gsub.  I want to get all the data between "<raw_data> and  </raw_data> only and exclude the rest in output. How can this be done? Any help would be appreciated! Thanks so much

print "Metar Station Name: "
metar = gets.chomp
metar.upcase!

if metar.length == 3 || metar.length == 4
    puts "Searching..."
   
    require "rubygems"
    require "nokogiri"
    require 'open-uri'
           
    doc = Nokogiri::XML(open("http://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&hoursBeforeNow=3&mostRecent=true&stationString=#{metar}"))
 
    output = @doc[/#{metar}(.*?)<\/raw_text>/, 1]
     
      while output.include? "</raw_text>"
        output.gsub!(/<\/raw_text>/, "")
      end
    
    puts output

else
  print "\"#{metar}\" is not a valid airport identifier. Please use a 3 or 4 letter valid airport code. For example: KATL for Atlanta Georgia.\n"
end

Hassan Schroeder

unread,
Mar 16, 2015, 8:12:17 AM3/16/15
to nokogi...@googlegroups.com
On Fri, Mar 13, 2015 at 6:29 AM, Michael Fischer
<pilot.m...@gmail.com> wrote:

> Once there. I am having trouble using and filtering the data with gsub. I
> want to get all the data between "<raw_data> and </raw_data> only and
> exclude the rest in output. How can this be done?

Trying regex/gsub here is ignoring what Nokogiri is about :-)

2.1.5 (main):0 > doc.xpath('//raw_text')
=> <raw_text>KORD 161151Z 22003KT 10SM SCT190 SCT250 09/01 A2996 RMK
AO2 SLP144 T00940006 10122 20089 50007</raw_text>
2.1.5 (main):0 > doc.xpath('//raw_text').text
=> "KORD 161151Z 22003KT 10SM SCT190 SCT250 09/01 A2996 RMK AO2 SLP144
T00940006 10122 20089 50007"
2.1.5 (main):0 >

HTH,
--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan
Consulting Availability : Silicon Valley or remote
Reply all
Reply to author
Forward
0 new messages