Class: Barometer::Query::Format::WeatherID

Inherits:
Barometer::Query::Format show all
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

Methods inherited from Barometer::Query::Format

convert_query, converts?, is?, is_a_query?

Class Method Details

.convertable_formatsObject



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

.formatObject



13
# File 'lib/barometer/formats/weather_id.rb', line 13

def self.format; :weather_id; end

.regexObject



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)

Raises:

  • (ArgumentError)


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

Raises:

  • (ArgumentError)


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