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.

Constant Summary collapse

@@fixes_file =
File.expand_path(
File.join(File.dirname(__FILE__), '..', 'translations', 'weather_country_codes.yml'))
@@fixes =
nil

Class Method Summary collapse

Methods inherited from Barometer::Query::Format

converts?, is?, is_a_query?

Class Method Details

.convertable_formatsObject



19
20
21
# File 'lib/barometer/formats/weather_id.rb', line 19

def self.convertable_formats
  [:short_zipcode, :zipcode, :coordinates, :geocode]
end

.country_code(query = nil) ⇒ Object

the first two letters of the :weather_id is the country_code



25
26
27
# File 'lib/barometer/formats/weather_id.rb', line 25

def self.country_code(query=nil)
  (query && query.size >= 2) ? _fix_country(query[0..1]) : nil
end

.formatObject



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

def self.format; :weather_id; end

.regexObject



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

def self.regex; /(^[A-Za-z]{4}[0-9]{4}$)/; end

.reverse(original_query) ⇒ Object

reverse lookup, :weather_id -> (:geocode || :coordinates)

Raises:

  • (ArgumentError)


47
48
49
50
51
52
53
54
# File 'lib/barometer/formats/weather_id.rb', line 47

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)


31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/barometer/formats/weather_id.rb', line 31

def self.to(original_query)
  raise ArgumentError unless is_a_query?(original_query)
  return nil unless converts?(original_query)
  converted_query = Barometer::Query.new

  # convert original query to :geocode, as that is the only
  # format we can convert directly from to weather_id
  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