Class: Barometer::Query::Format::Coordinates

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

Overview

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 Method Summary collapse

Methods inherited from Barometer::Query::Format

convert_query, converts?, country_code, is?, is_a_query?

Class Method Details

.convertable_formatsObject



14
15
16
# File 'lib/barometer/formats/coordinates.rb', line 14

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

.formatObject



12
# File 'lib/barometer/formats/coordinates.rb', line 12

def self.format; :coordinates; end

.parse_latitude(query) ⇒ Object



46
47
48
49
# File 'lib/barometer/formats/coordinates.rb', line 46

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

.parse_longitude(query) ⇒ Object



51
52
53
54
# File 'lib/barometer/formats/coordinates.rb', line 51

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

.regexObject



13
# File 'lib/barometer/formats/coordinates.rb', line 13

def self.regex; /^[-]?[0-9\.]+[,]{1}\s?[-]?[0-9\.]+$/; end

.to(original_query) ⇒ Object

convert to this format, X -> :coordinates

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/barometer/formats/coordinates.rb', line 20

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