Sha256: 964b69960504d7e36c9cb538e334873fccc2ab91401278493347b1d3f40ebffa

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

module Barometer
  #
  # Web Service: NOAA Station ID
  #
  # uses noaa to find the closest station id
  #
  class WebService::NoaaStation < WebService

    # get the closest station id for given coordinates
    #
    def self.fetch(latitude, longitude)

      begin
        require 'nokogiri'
      rescue LoadError
        puts "\n****\nTo use this functionality you will need to install Nokogiri >= 1.3.3\n****\n\n"
        return nil
      end

      puts "fetching NOAA station ID near #{latitude}, #{longitude}" if Barometer::debug?
      return nil unless latitude && longitude
      _fetch_via_noaa(latitude, longitude)
    end

    # http://forecast.weather.gov/MapClick.php?textField1=LATITUDE&textField2=LONGITUDE
    def self._fetch_via_noaa(latitude, longitude)
      response = self.get(
        "http://forecast.weather.gov/MapClick.php?",
        :query => { :textField1 => latitude, :textField2 => longitude },
        :format => :html,
        :timeout => Barometer.timeout
      )

      # parse the station id from the given page
      station_id = nil

      begin
        doc = Nokogiri::HTML.parse(response.body)
        if doc && links = doc.search(".current-conditions-extra a")
          sid_link = links.detect{|link| link.attr("href").match(/sid=(.*)&/)}
          begin
            station_id = sid_link.attr("href").match(/sid=(.*?)&/)[1]
          rescue
          end
        end
      rescue
        puts "[ERROR] finding NOAA station near #{latitude}, #{longitude}" if Barometer::debug?
      end
      station_id
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
barometer-0.8.0 lib/barometer/web_services/noaa_station_id.rb