Class: Barometer::Query::Format::WoeID

Inherits:
Barometer::Query::Format show all
Defined in:
lib/barometer/formats/woe_id.rb

Overview

Format: WOEID

"Where on Earth" ID (by Yahoo!)

eg. 2459115, 615702 or w90210

NOTE: zipcodes and WoeIDs can look exactly the same when the WoeID

is 5 digits long.  For now, a 5 digit number will be detected as
zipcode.  The way to override this is to prepend a number with the
letter 'w'.  Therefore 90210 will be a zipcode and w90210 will be
a WoeID.  In the future, if WoeIDs are moe popular then zipcodes,
then I will reverse this ...

This class is used to determine if a query is a :woe_id and how to convert to a :woe_id.

Class Method Summary collapse

Methods inherited from Barometer::Query::Format

converts?, country_code, is?, is_a_query?

Class Method Details

.convert_query(text) ⇒ Object

remove the ‘w’ from applicable queries (only needed for detection)



28
29
30
31
# File 'lib/barometer/formats/woe_id.rb', line 28

def self.convert_query(text)
  return nil unless text
  text.delete('w')
end

.convertable_formatsObject



22
23
24
# File 'lib/barometer/formats/woe_id.rb', line 22

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

.formatObject



20
# File 'lib/barometer/formats/woe_id.rb', line 20

def self.format; :woe_id; end

.regexObject



21
# File 'lib/barometer/formats/woe_id.rb', line 21

def self.regex; /(^[0-9]{4}$)|(^[0-9]{6,7}$)|(^w[0-9]{4,7}$)/; end

.reverse(original_query) ⇒ Object

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

Raises:

  • (ArgumentError)


83
84
85
86
87
88
89
90
# File 'lib/barometer/formats/woe_id.rb', line 83

def self.reverse(original_query)
  raise ArgumentError unless is_a_query?(original_query)
  return nil unless original_query.format == self.format
  converted_query = Barometer::Query.new
  converted_query.q = _reverse(original_query)
  converted_query.format = Barometer::Query::Format::Geocode.format
  converted_query
end

.to(original_query) ⇒ Object

convert to this format, X -> :woeid

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/barometer/formats/woe_id.rb', line 35

def self.to(original_query)
  raise ArgumentError unless is_a_query?(original_query)
  return nil unless converts?(original_query)

  # pre-convert (:weather_id -> :geocode)
  #
  pre_query = nil
  if original_query.format == :weather_id
    pre_query = Query::Format::WeatherID.reverse(original_query)
  end

  # pre-convert ([:short_zipcode, :zipcode, :postalcode, :icao] -> :geocode)
  #
  unless pre_query
    if [:short_zipcode, :zipcode, :icao].include?(original_query.format)
      unless pre_query = original_query.get_conversion(Query::Format::Geocode.format)
        pre_query = Query::Format::Geocode.to(original_query)
      end
    end
  end

  converted_query = Barometer::Query.new
  converted_query.country_code = original_query.country_code if original_query
  
  # TODO
  # use Geomojo.com (when no Yahoo! appid)
  #
  # if [:coordinates].include?(pre_query ? pre_query.format : original_query.format) &&
  #   Barometer.yahoo_placemaker_app_id.nil?
  #   converted_query.q = _query_geomojo(pre_query || original_query)
  #   converted_query.format = format
  # end

  # use Yahoo! Placemaker
  #
  if [:coordinates, :geocode, :postalcode].include?(pre_query ? pre_query.format : original_query.format) &&
    !Barometer.yahoo_placemaker_app_id.nil?
    converted_query.q = _query_placemaker(pre_query || original_query)
    converted_query.format = format
  end
  
  converted_query.geo = pre_query.geo if pre_query
  converted_query.country_code = pre_query.country_code if pre_query
  converted_query
end