Class: Barometer::Query::Format::WeatherID
- Inherits:
-
Barometer::Query::Format
- Object
- Barometer::Query::Format
- Barometer::Query::Format::WeatherID
- Defined in:
- lib/barometer/formats/weather_id.rb
Overview
Format: Weather ID (specific to weather.com)
eg. USGA0028
This class is used to determine if a query is a :weather_id, how to convert to and from :weather_id and what the country_code is.
Class Method Summary collapse
-
._parse_geocode(text) ⇒ Object
parse the geo_data.
-
._parse_weather_id(text) ⇒ Object
match the first :weather_id (from search results).
-
._reverse(query = nil) ⇒ Object
:weather_id -> :geocode query yahoo with :weather_id and parse geo_data.
-
._search(query = nil) ⇒ Object
:geocode -> :weather_id search weather.com for the given query.
- .convertable_formats ⇒ Object
-
.country_code(query = nil) ⇒ Object
the first two letters of the :weather_id is the country_code.
- .format ⇒ Object
- .regex ⇒ Object
-
.reverse(original_query) ⇒ Object
reverse lookup, :weather_id -> (:geocode || :coordinates).
-
.to(original_query) ⇒ Object
convert to this format, X -> :weather_id.
Methods inherited from Barometer::Query::Format
_fix_country, convert_query, converts?, is?, is_a_query?
Class Method Details
._parse_geocode(text) ⇒ Object
parse the geo_data
84 85 86 87 88 89 |
# File 'lib/barometer/formats/weather_id.rb', line 84 def self._parse_geocode(text) return nil unless text output = [text["city"], text["region"], _fix_country(text["country"])] output.delete("") output.compact.join(', ') end |
._parse_weather_id(text) ⇒ Object
match the first :weather_id (from search results)
76 77 78 79 80 |
# File 'lib/barometer/formats/weather_id.rb', line 76 def self._parse_weather_id(text) return nil unless text match = text.match(/loc id=[\\]?['|""]([0-9a-zA-Z]*)[\\]?['|""]/) match ? match[1] : nil end |
._reverse(query = nil) ⇒ Object
:weather_id -> :geocode query yahoo with :weather_id and parse geo_data
67 68 69 70 71 72 |
# File 'lib/barometer/formats/weather_id.rb', line 67 def self._reverse(query=nil) return nil unless query raise ArgumentError unless is_a_query?(query) response = WebService::WeatherID.reverse(query) _parse_geocode(response) end |
._search(query = nil) ⇒ Object
:geocode -> :weather_id search weather.com for the given query
57 58 59 60 61 62 |
# File 'lib/barometer/formats/weather_id.rb', line 57 def self._search(query=nil) return nil unless query raise ArgumentError unless is_a_query?(query) response = WebService::WeatherID.fetch(query) _parse_weather_id(response) end |
.convertable_formats ⇒ Object
15 16 17 |
# File 'lib/barometer/formats/weather_id.rb', line 15 def self.convertable_formats [:short_zipcode, :zipcode, :coordinates, :icao, :geocode] end |
.country_code(query = nil) ⇒ Object
the first two letters of the :weather_id is the country_code
21 22 23 |
# File 'lib/barometer/formats/weather_id.rb', line 21 def self.country_code(query=nil) (query && query.size >= 2) ? _fix_country(query[0..1]) : nil end |
.format ⇒ Object
13 |
# File 'lib/barometer/formats/weather_id.rb', line 13 def self.format; :weather_id; end |
.regex ⇒ Object
14 |
# File 'lib/barometer/formats/weather_id.rb', line 14 def self.regex; /(^[A-Za-z]{4}[0-9]{4}$)/; end |
.reverse(original_query) ⇒ Object
reverse lookup, :weather_id -> (:geocode || :coordinates)
43 44 45 46 47 48 49 50 |
# File 'lib/barometer/formats/weather_id.rb', line 43 def self.reverse(original_query) raise ArgumentError unless is_a_query?(original_query) return nil unless original_query.format == format converted_query = Barometer::Query.new converted_query.q = _reverse(original_query) converted_query.format = Query::Format::Geocode.format converted_query end |
.to(original_query) ⇒ Object
convert to this format, X -> :weather_id
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/barometer/formats/weather_id.rb', line 27 def self.to(original_query) raise ArgumentError unless is_a_query?(original_query) return nil unless converts?(original_query) # convert original query to :geocode, as that is the only # format we can convert directly from to weather_id converted_query = Barometer::Query.new converted_query = Query::Format::Geocode.to(original_query) converted_query.q = _search(converted_query) converted_query.format = format converted_query.country_code = country_code(converted_query.q) converted_query end |