lib/barometer/query.rb in barometer-0.6.7 vs lib/barometer/query.rb in barometer-0.7.0

- old
+ new

@@ -19,28 +19,33 @@ class Query # This array defines the order to check a query for the format # FORMATS = %w( - ShortZipcode Zipcode Postalcode WeatherID Coordinates Icao Geocode + ShortZipcode Zipcode Postalcode WeatherID Coordinates Icao WoeID Geocode ) FORMAT_MAP = { :short_zipcode => "ShortZipcode", :zipcode => "Zipcode", :postalcode => "Postalcode", :weather_id => "WeatherID", :coordinates => "Coordinates", :icao => "Icao", - :geocode => "Geocode" + :woe_id => "WoeID", :geocode => "Geocode" } - attr_accessor :format, :q, :country_code + attr_writer :q + attr_accessor :format, :country_code attr_accessor :geo, :timezone, :conversions def initialize(query=nil) return unless query @q = query self.analyze! @conversions = {} end + + def q + format ? Barometer::Query::Format.const_get(FORMAT_MAP[format.to_sym].to_s).convert_query(@q) : @q + end # analyze the saved query to determine the format. # this delegates the detection to each formats class # until th right one is found # @@ -79,17 +84,18 @@ # go through each acceptable format and try to convert to that converted = false converted_query = Barometer::Query.new preferred_formats.each do |preferred_format| klass = FORMAT_MAP[preferred_format.to_sym] + # if we discover that the format we have is the preferred format, return it if preferred_format == @format converted = true converted_query = Barometer::Query.new(@q) end unless converted unless converted_query = get_conversion(preferred_format) - converted_query = Query::Format.const_get(klass.to_s).to(self) + converted_query = Query::Format.const_get(klass.to_s).to(self) end converted = true if converted_query end if converted converted_query.country_code ||= Query::Format.const_get(klass.to_s).country_code(converted_query.q) @@ -149,8 +155,18 @@ converted_query else nil end end + + def latitude + return nil unless self.format == Query::Format::Coordinates.format + Query::Format::Coordinates.parse_latitude(self.q) + end + + def longitude + return nil unless self.format == Query::Format::Coordinates.format + Query::Format::Coordinates.parse_longitude(self.q) + end end end