Sha256: 24214d1233c134d295a7af102e87531e5e8e81c84e5f8ae42fba1d7610b3e305

Contents?: true

Size: 976 Bytes

Versions: 2

Compression:

Stored size: 976 Bytes

Contents

module Barometer
  module Query
    module Format
      #
      # Weather ID (specific to weather.com)
      # eg. USGA0028
      #
      class WeatherID < Base
        @@fixes_file = File.expand_path(
          File.join(File.dirname(__FILE__), 'translations', 'weather_country_codes.yml'))
        @@fixes = nil

        def self.regex; /(^[A-Za-z]{4}[0-9]{4}$)/; end
        def self.country_code(query)
          (query && query.size >= 2) ? _fix_country(query[0..1]) : nil
        end

        private

        # weather.com uses non-standard two letter country codes that
        # hinder the ability to determine the country or fetch geo_data.
        # correct these "mistakes"
        #
        def self._fix_country(country_code)
          @@fixes ||= YAML.load_file(@@fixes_file)
          @@fixes[country_code.upcase.to_s] || country_code
        end
      end
    end
  end
end

Barometer::Query::Format.register(:weather_id, Barometer::Query::Format::WeatherID)

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
barometer-0.9.1 lib/barometer/query/formats/weather_id.rb
barometer-0.9.0 lib/barometer/query/formats/weather_id.rb