Sha256: d269932c17be88392c03461586bd3860049e46efdd84d65050b0c0b4440017e3

Contents?: true

Size: 1.68 KB

Versions: 4

Compression:

Stored size: 1.68 KB

Contents

module Barometer
  #
  # Format: Coordinates
  #
  # eg. 123.1234,-123.123
  #
  # This class is used to determine if a query is a
  # :coordinates and how to convert to :coordinates.
  #
  class Query::Format::Coordinates < Query::Format
  
    def self.format; :coordinates; end
    def self.regex; /^[-]?[0-9\.]+[,]{1}\s?[-]?[0-9\.]+$/; end
    def self.convertable_formats
      [:short_zipcode, :zipcode, :postalcode, :weather_id, :coordinates, :icao, :geocode, :woe_id]
    end
    
    # convert to this format, X -> :coordinates
    #
    def self.to(original_query)
      raise ArgumentError unless is_a_query?(original_query)
      return nil unless converts?(original_query)
      converted_query = Barometer::Query.new
      
      # pre-convert
      #
      pre_query = nil
      if original_query.format == :weather_id
        unless pre_query = original_query.get_conversion(Query::Format::Geocode.format)
          pre_query = Query::Format::WeatherID.reverse(original_query)
          original_query.post_conversion(pre_query)
        end
      elsif original_query.format == :woe_id
        pre_query = Query::Format::WoeID.reverse(original_query)
      end
      
      # convert & adjust
      #
      converted_query = Query::Format::Geocode.geocode(pre_query || original_query)
      converted_query.q = converted_query.geo.coordinates if converted_query.geo
      converted_query.format = format

      converted_query
    end
    
    def self.parse_latitude(query)
      coordinates = query.to_s.split(',')
      coordinates ? coordinates[0] : nil
    end
    
    def self.parse_longitude(query)
      coordinates = query.to_s.split(',')
      coordinates ? coordinates[1] : nil
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
barometer-0.7.3 lib/barometer/formats/coordinates.rb
barometer-0.7.2 lib/barometer/formats/coordinates.rb
barometer-0.7.1 lib/barometer/formats/coordinates.rb
barometer-0.7.0 lib/barometer/formats/coordinates.rb