lib/barometer/query.rb in barometer-0.7.3 vs lib/barometer/query.rb in barometer-0.8.0

- old
+ new

@@ -15,11 +15,11 @@ # (most likely this conversion will use Googles geocoding service using # the Graticule gem). Worst case scenario is that the Weather API will # not accept the query string. # class Query - + # This array defines the order to check a query for the format # FORMATS = %w( ShortZipcode Zipcode Postalcode WeatherID Coordinates Icao WoeID Geocode ) @@ -27,22 +27,22 @@ :short_zipcode => "ShortZipcode", :zipcode => "Zipcode", :postalcode => "Postalcode", :weather_id => "WeatherID", :coordinates => "Coordinates", :icao => "Icao", :woe_id => "WoeID", :geocode => "Geocode" } - + 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. @@ -57,18 +57,18 @@ @country_code = Query::Format.const_get(format.to_s).country_code(@q) break end end end - + # take a list of acceptable (and ordered by preference) formats and convert # the current query (q) into the most preferred and acceptable format. a # side effect of the conversions may reveal the country_code, if so save it # def convert!(preferred_formats=nil) raise ArgumentError unless (preferred_formats && preferred_formats.size > 0) - + # why convert if we are already there? # (except in the case that the serivce excepts coordinates and we have a # a geocode ... the google geocode results are superior) # skip_conversion = false @@ -77,11 +77,11 @@ if preferred_formats.include?(@format.to_sym) skip_conversion = true converted_query = self.dup end end - + unless skip_conversion # go through each acceptable format and try to convert to that converted = false converted_query = Barometer::Query.new preferred_formats.each do |preferred_format| @@ -92,21 +92,21 @@ 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) - end + 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) post_conversion(converted_query) break end end end - + # force geocode?, unless we already did # if Barometer.force_geocode && !@geo if converted_query && converted_query.geo @geo = converted_query.geo @@ -115,11 +115,11 @@ geo_query = Query::Format::Coordinates.to(converted_query) @geo = geo_query.geo if (geo_query && geo_query.geo) converted_query.geo = @geo.dup end end - + # enhance timezone?, unless we already did # if Barometer.enhance_timezone && !@timezone if converted_query && converted_query.timezone @geo = converted_query.timezone @@ -127,14 +127,14 @@ puts "enhance timezone: #{@geo.latitude}, #{@geo.longitude}" if Barometer::debug? @timezone = WebService::Timezone.fetch(@geo.latitude,@geo.longitude) converted_query.timezone = @timezone.dup end end - + converted_query end - + # save the important parts of the conversion ... by saving conversion we # can avoid doing the same conversion multiple times # def post_conversion(converted_query) return unless (converted_query && converted_query.q && converted_query.format) @@ -160,13 +160,13 @@ 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 +end