barometer 0.3.2 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (108) hide show
  1. data/README.rdoc +78 -70
  2. data/VERSION.yml +2 -2
  3. data/bin/barometer +100 -37
  4. data/lib/barometer.rb +12 -8
  5. data/lib/barometer/base.rb +48 -20
  6. data/lib/barometer/data.rb +5 -1
  7. data/lib/barometer/data/current.rb +23 -15
  8. data/lib/barometer/data/distance.rb +15 -5
  9. data/lib/barometer/data/forecast.rb +23 -5
  10. data/lib/barometer/data/geo.rb +16 -54
  11. data/lib/barometer/data/local_datetime.rb +137 -0
  12. data/lib/barometer/data/local_time.rb +134 -0
  13. data/lib/barometer/data/location.rb +6 -1
  14. data/lib/barometer/data/measurement.rb +71 -42
  15. data/lib/barometer/data/night.rb +69 -0
  16. data/lib/barometer/data/pressure.rb +15 -5
  17. data/lib/barometer/data/speed.rb +16 -5
  18. data/lib/barometer/data/sun.rb +8 -20
  19. data/lib/barometer/data/temperature.rb +22 -9
  20. data/lib/barometer/data/units.rb +10 -19
  21. data/lib/barometer/data/zone.rb +135 -9
  22. data/lib/barometer/formats.rb +12 -0
  23. data/lib/barometer/formats/coordinates.rb +42 -0
  24. data/lib/barometer/formats/format.rb +46 -0
  25. data/lib/barometer/formats/geocode.rb +51 -0
  26. data/lib/barometer/formats/icao.rb +37 -0
  27. data/lib/barometer/formats/postalcode.rb +22 -0
  28. data/lib/barometer/formats/short_zipcode.rb +17 -0
  29. data/lib/barometer/formats/weather_id.rb +107 -0
  30. data/lib/barometer/formats/zipcode.rb +31 -0
  31. data/lib/barometer/query.rb +61 -232
  32. data/lib/barometer/services.rb +14 -4
  33. data/lib/barometer/translations/icao_country_codes.yml +9 -0
  34. data/lib/barometer/translations/weather_country_codes.yml +17 -0
  35. data/lib/barometer/weather.rb +51 -30
  36. data/lib/barometer/{services → weather_services}/google.rb +23 -26
  37. data/lib/barometer/weather_services/noaa.rb +6 -0
  38. data/lib/barometer/{services → weather_services}/service.rb +101 -92
  39. data/lib/barometer/weather_services/weather_bug.rb +6 -0
  40. data/lib/barometer/weather_services/weather_dot_com.rb +261 -0
  41. data/lib/barometer/{services → weather_services}/wunderground.rb +58 -76
  42. data/lib/barometer/{services → weather_services}/yahoo.rb +91 -121
  43. data/lib/barometer/web_services/geocode.rb +33 -0
  44. data/lib/barometer/web_services/weather_id.rb +37 -0
  45. data/lib/barometer/web_services/web_service.rb +32 -0
  46. data/lib/demometer/demometer.rb +31 -4
  47. data/lib/demometer/views/forecast.erb +20 -0
  48. data/lib/demometer/views/index.erb +10 -3
  49. data/lib/demometer/views/measurement.erb +8 -3
  50. data/lib/demometer/views/readme.erb +63 -24
  51. data/spec/barometer_spec.rb +18 -36
  52. data/spec/{data_current_spec.rb → data/current_spec.rb} +73 -49
  53. data/spec/{data_distance_spec.rb → data/distance_spec.rb} +30 -30
  54. data/spec/{data_forecast_spec.rb → data/forecast_spec.rb} +57 -15
  55. data/spec/data/geo_spec.rb +91 -0
  56. data/spec/data/local_datetime_spec.rb +269 -0
  57. data/spec/data/local_time_spec.rb +239 -0
  58. data/spec/{data_location_spec.rb → data/location_spec.rb} +12 -1
  59. data/spec/{data_measurement_spec.rb → data/measurement_spec.rb} +135 -66
  60. data/spec/data/night_measurement_spec.rb +136 -0
  61. data/spec/{data_pressure_spec.rb → data/pressure_spec.rb} +29 -29
  62. data/spec/{data_speed_spec.rb → data/speed_spec.rb} +30 -30
  63. data/spec/data/sun_spec.rb +49 -0
  64. data/spec/{data_temperature_spec.rb → data/temperature_spec.rb} +44 -44
  65. data/spec/{units_spec.rb → data/units_spec.rb} +6 -6
  66. data/spec/{data_zone_spec.rb → data/zone_spec.rb} +15 -15
  67. data/spec/fixtures/formats/weather_id/90210.xml +1 -0
  68. data/spec/fixtures/formats/weather_id/atlanta.xml +1 -0
  69. data/spec/fixtures/formats/weather_id/from_USGA0028.xml +1 -0
  70. data/spec/fixtures/formats/weather_id/new_york.xml +1 -0
  71. data/spec/fixtures/{geocode_40_73.xml → geocode/40_73.xml} +0 -0
  72. data/spec/fixtures/{geocode_90210.xml → geocode/90210.xml} +0 -0
  73. data/spec/fixtures/{geocode_T5B4M9.xml → geocode/T5B4M9.xml} +0 -0
  74. data/spec/fixtures/geocode/atlanta.xml +1 -0
  75. data/spec/fixtures/{geocode_calgary_ab.xml → geocode/calgary_ab.xml} +0 -0
  76. data/spec/fixtures/{geocode_ksfo.xml → geocode/ksfo.xml} +0 -0
  77. data/spec/fixtures/{geocode_newyork_ny.xml → geocode/newyork_ny.xml} +0 -0
  78. data/spec/fixtures/{google_calgary_ab.xml → services/google/calgary_ab.xml} +0 -0
  79. data/spec/fixtures/services/weather_dot_com/90210.xml +1 -0
  80. data/spec/fixtures/{current_calgary_ab.xml → services/wunderground/current_calgary_ab.xml} +0 -0
  81. data/spec/fixtures/{forecast_calgary_ab.xml → services/wunderground/forecast_calgary_ab.xml} +0 -0
  82. data/spec/fixtures/{yahoo_90210.xml → services/yahoo/90210.xml} +0 -0
  83. data/spec/formats/coordinates_spec.rb +158 -0
  84. data/spec/formats/format_spec.rb +73 -0
  85. data/spec/formats/geocode_spec.rb +179 -0
  86. data/spec/formats/icao_spec.rb +61 -0
  87. data/spec/formats/postalcode_spec.rb +59 -0
  88. data/spec/formats/short_zipcode_spec.rb +53 -0
  89. data/spec/formats/weather_id_spec.rb +191 -0
  90. data/spec/formats/zipcode_spec.rb +111 -0
  91. data/spec/query_spec.rb +261 -288
  92. data/spec/spec_helper.rb +128 -4
  93. data/spec/{service_google_spec.rb → weather_services/google_spec.rb} +46 -46
  94. data/spec/weather_services/services_spec.rb +1118 -0
  95. data/spec/weather_services/weather_dot_com_spec.rb +327 -0
  96. data/spec/weather_services/wunderground_spec.rb +332 -0
  97. data/spec/{service_yahoo_spec.rb → weather_services/yahoo_spec.rb} +65 -81
  98. data/spec/weather_spec.rb +73 -61
  99. data/spec/web_services/geocode_spec.rb +45 -0
  100. data/spec/web_services/web_services_spec.rb +26 -0
  101. metadata +88 -36
  102. data/lib/barometer/services/noaa.rb +0 -6
  103. data/lib/barometer/services/weather_bug.rb +0 -6
  104. data/lib/barometer/services/weather_dot_com.rb +0 -6
  105. data/spec/data_geo_spec.rb +0 -94
  106. data/spec/data_sun_spec.rb +0 -76
  107. data/spec/service_wunderground_spec.rb +0 -330
  108. data/spec/services_spec.rb +0 -1106
@@ -5,6 +5,7 @@ require 'barometer/query'
5
5
  require 'barometer/weather'
6
6
  require 'barometer/services'
7
7
  require 'barometer/data'
8
+ require 'barometer/formats'
8
9
 
9
10
  module Barometer
10
11
 
@@ -12,21 +13,20 @@ module Barometer
12
13
  def self.google_geocode_key; @@google_geocode_key; end;
13
14
  def self.google_geocode_key=(key); @@google_geocode_key = key; end;
14
15
 
15
- @@skip_graticule = false
16
- def self.skip_graticule; @@skip_graticule; end;
17
- def self.skip_graticule=(value); @@skip_graticule = value; end;
18
-
19
16
  # sometimes a query is used as is and never gets geocoded (ie zipcode)
20
- # often, it is useful have queries geocoded to know where in the
17
+ # often, it is useful to have queries geocoded to know where in the
21
18
  # world that query points to. you can force the geocoding of
22
19
  # queries (even when not required) so that you have the geocoded
23
20
  # data. the reason this isn't the default is that it will use an
24
21
  # extra web service query when not normally required
22
+ #
25
23
  @@force_geocode = false
26
24
  def self.force_geocode; @@force_geocode; end;
27
25
  def self.force_geocode=(value); @@force_geocode = value; end;
28
26
  def self.force_geocode!; @@force_geocode = true; end;
29
27
 
28
+ # adjust the timeout used when interactind with external web services
29
+ #
30
30
  @@timeout = 15
31
31
  def self.timeout; @@timeout; end;
32
32
  def self.timeout=(value); @@timeout = value; end;
@@ -35,17 +35,21 @@ module Barometer
35
35
  Barometer::Base.new(query)
36
36
  end
37
37
 
38
- def self.selection=(selection=nil)
39
- Barometer::Base.selection = selection
38
+ # update the Barometer configuration
39
+ #
40
+ def self.config=(config=nil)
41
+ Barometer::Base.config = config
40
42
  end
41
43
 
42
44
  # shortcut to Barometer::Service.source method
43
45
  # allows Barometer.source(:wunderground)
46
+ #
44
47
  def self.source(source)
45
- Barometer::Service.source(source)
48
+ Barometer::WeatherService.source(source)
46
49
  end
47
50
 
48
51
  # custom errors
52
+ #
49
53
  class OutOfSources < StandardError; end
50
54
 
51
55
  end
@@ -1,12 +1,12 @@
1
1
  module Barometer
2
-
3
2
  class Base
4
3
 
5
4
  # allow the configuration of specific weather APIs to be used,
6
5
  # and the order in which they would be used
7
- @@selection = { 1 => [:wunderground] }
8
- def self.selection; @@selection; end;
9
- def self.selection=(hash); @@selection = hash; end;
6
+ #
7
+ @@config = { 1 => [:wunderground] }
8
+ def self.config; @@config; end;
9
+ def self.config=(hash); @@config = hash; end;
10
10
 
11
11
  attr_reader :query
12
12
  attr_accessor :weather, :success
@@ -17,36 +17,64 @@ module Barometer
17
17
  @success = false
18
18
  end
19
19
 
20
+ # iterate through all the configured sources and
21
+ # collect weather data for each one
22
+ #
20
23
  def measure(metric=nil)
21
24
  return nil unless @query
22
25
 
23
26
  level = 1
24
27
  until self.success?
25
- if sources = @@selection[level]
26
- if sources.is_a?(Array)
27
- sources.each do |source|
28
- measurement = Barometer.source(source.to_sym).measure(@query, metric)
29
- @success = true if measurement.success?
30
- @weather.measurements << measurement
31
- end
32
- else
33
- measurement = Barometer.source(sources.to_sym).measure(@query, metric)
34
- @success = true if measurement.success?
35
- @weather.measurements << measurement
36
- end
28
+ if sources = @@config[level]
29
+ _dig(sources, nil, metric)
37
30
  else
38
31
  raise OutOfSources
39
32
  end
40
33
  level += 1
41
34
  end
42
-
43
35
  @weather
44
36
  end
45
37
 
46
- def success?
47
- @success
38
+ def success?; @success; end
39
+
40
+ private
41
+
42
+ # iterate through the setup until we have a source name (and possibly
43
+ # a config for that source), then measure with that source
44
+ #
45
+ # this allows for many different config formats, like
46
+ # { 1 => :wunderground }
47
+ # { 1 => [:wunderground]}
48
+ # { 1 => [:wunderground, :yahoo]}
49
+ # { 1 => [:wunderground, {:yahoo => {:weight => 2}}]}
50
+ # { 1 => {:wunderground => {:weight => 2}}}
51
+ # { 1 => [{:wunderground => {:weight => 2}}]}
52
+ #
53
+ def _dig(data, config=nil, metric=nil)
54
+ if data.is_a?(String) || data.is_a?(Symbol)
55
+ _measure(data, config, metric)
56
+ elsif data.is_a?(Array)
57
+ data.each do |datum|
58
+ _dig(datum, nil, metric)
59
+ end
60
+ elsif data.is_a?(Hash)
61
+ data.each do |datum, config|
62
+ _dig(datum, config, metric)
63
+ end
64
+ end
65
+ end
66
+
67
+ # do that actual source measurement
68
+ #
69
+ def _measure(datum, config=nil, metric=nil)
70
+ Barometer.source(datum.to_sym).keys = config[:keys] if (config && config[:keys])
71
+ measurement = Barometer.source(datum.to_sym).measure(@query, metric)
72
+ if config && config[:weight]
73
+ measurement.weight = config[:weight]
74
+ end
75
+ @success = true if measurement.success?
76
+ @weather.measurements << measurement
48
77
  end
49
78
 
50
79
  end
51
-
52
80
  end
@@ -1,9 +1,11 @@
1
1
  $:.unshift(File.dirname(__FILE__))
2
2
 
3
3
  # measurements and units
4
+ #
4
5
  require 'data/measurement'
5
6
  require 'data/current'
6
7
  require 'data/forecast'
8
+ require 'data/night'
7
9
  require 'data/zone'
8
10
  require 'data/sun'
9
11
  require 'data/geo'
@@ -12,4 +14,6 @@ require 'data/units'
12
14
  require 'data/temperature'
13
15
  require 'data/distance'
14
16
  require 'data/speed'
15
- require 'data/pressure'
17
+ require 'data/pressure'
18
+ require 'data/local_time'
19
+ require 'data/local_datetime'
@@ -6,18 +6,15 @@ module Barometer
6
6
  # This is basically a data holding class for the current weather
7
7
  # conditions.
8
8
  #
9
- class CurrentMeasurement
9
+ class Data::CurrentMeasurement
10
10
 
11
- attr_accessor :time, :local_time
11
+ attr_reader :current_at, :updated_at
12
12
  attr_reader :humidity, :icon, :condition
13
13
  attr_reader :temperature, :dew_point, :heat_index, :wind_chill
14
14
  attr_reader :wind, :pressure, :visibility, :sun
15
15
 
16
- def time=(time)
17
- #raise ArgumentError unless time.is_a?(Time)
18
- @time = time
19
- end
20
-
16
+ # accessors (with input checking)
17
+ #
21
18
  def humidity=(humidity)
22
19
  raise ArgumentError unless
23
20
  (humidity.is_a?(Fixnum) || humidity.is_a?(Float))
@@ -35,50 +32,61 @@ module Barometer
35
32
  end
36
33
 
37
34
  def temperature=(temperature)
38
- raise ArgumentError unless temperature.is_a?(Barometer::Temperature)
35
+ raise ArgumentError unless temperature.is_a?(Data::Temperature)
39
36
  @temperature = temperature
40
37
  end
41
38
 
42
39
  def dew_point=(dew_point)
43
- raise ArgumentError unless dew_point.is_a?(Barometer::Temperature)
40
+ raise ArgumentError unless dew_point.is_a?(Data::Temperature)
44
41
  @dew_point = dew_point
45
42
  end
46
43
 
47
44
  def heat_index=(heat_index)
48
- raise ArgumentError unless heat_index.is_a?(Barometer::Temperature)
45
+ raise ArgumentError unless heat_index.is_a?(Data::Temperature)
49
46
  @heat_index = heat_index
50
47
  end
51
48
 
52
49
  def wind_chill=(wind_chill)
53
- raise ArgumentError unless wind_chill.is_a?(Barometer::Temperature)
50
+ raise ArgumentError unless wind_chill.is_a?(Data::Temperature)
54
51
  @wind_chill = wind_chill
55
52
  end
56
53
 
57
54
  def wind=(wind)
58
- raise ArgumentError unless wind.is_a?(Barometer::Speed)
55
+ raise ArgumentError unless wind.is_a?(Data::Speed)
59
56
  @wind = wind
60
57
  end
61
58
 
62
59
  def pressure=(pressure)
63
- raise ArgumentError unless pressure.is_a?(Barometer::Pressure)
60
+ raise ArgumentError unless pressure.is_a?(Data::Pressure)
64
61
  @pressure = pressure
65
62
  end
66
63
 
67
64
  def visibility=(visibility)
68
- raise ArgumentError unless visibility.is_a?(Barometer::Distance)
65
+ raise ArgumentError unless visibility.is_a?(Data::Distance)
69
66
  @visibility = visibility
70
67
  end
71
68
 
72
69
  def sun=(sun)
73
- raise ArgumentError unless sun.is_a?(Barometer::Sun)
70
+ raise ArgumentError unless sun.is_a?(Data::Sun)
74
71
  @sun = sun
75
72
  end
76
73
 
74
+ def current_at=(current_at)
75
+ raise ArgumentError unless (current_at.is_a?(Data::LocalTime) || current_at.is_a?(Data::LocalDateTime))
76
+ @current_at = current_at
77
+ end
78
+
79
+ def updated_at=(updated_at)
80
+ raise ArgumentError unless (updated_at.is_a?(Data::LocalTime) || updated_at.is_a?(Data::LocalDateTime))
81
+ @updated_at = updated_at
82
+ end
83
+
77
84
  #
78
85
  # helpers
79
86
  #
80
87
 
81
88
  # creates "?" helpers for all attributes (which maps to nil?)
89
+ #
82
90
  def method_missing(method,*args)
83
91
  # if the method ends in ?, then strip it off and see if we
84
92
  # respond to the method without the ?
@@ -11,7 +11,8 @@ module Barometer
11
11
  # NOTE: this currently only supports the scale of
12
12
  # kilometers (km) and miles (m). There is currently
13
13
  # no way to scale to smaller units (eg km -> m -> mm)
14
- class Distance < Barometer::Units
14
+ #
15
+ class Data::Distance < Data::Units
15
16
 
16
17
  METRIC_UNITS = "km"
17
18
  IMPERIAL_UNITS = "m"
@@ -46,6 +47,7 @@ module Barometer
46
47
  #
47
48
 
48
49
  # store kilometers
50
+ #
49
51
  def km=(km)
50
52
  return if !km || !(km.is_a?(Integer) || km.is_a?(Float))
51
53
  @kilometers = km.to_f
@@ -53,6 +55,7 @@ module Barometer
53
55
  end
54
56
 
55
57
  # store miles
58
+ #
56
59
  def m=(m)
57
60
  return if !m || !(m.is_a?(Integer) || m.is_a?(Float))
58
61
  @miles = m.to_f
@@ -60,14 +63,16 @@ module Barometer
60
63
  end
61
64
 
62
65
  # return the stored kilometers or convert from miles
66
+ #
63
67
  def km(as_integer=true)
64
- km = (@kilometers || Distance.m_to_km(@miles))
68
+ km = (@kilometers || Data::Distance.m_to_km(@miles))
65
69
  km ? (as_integer ? km.to_i : (100*km).round/100.0) : nil
66
70
  end
67
71
 
68
72
  # return the stored miles or convert from kilometers
73
+ #
69
74
  def m(as_integer=true)
70
- m = (@miles || Distance.km_to_m(@kilometers))
75
+ m = (@miles || Data::Distance.km_to_m(@kilometers))
71
76
  m ? (as_integer ? m.to_i : (100*m).round/100.0) : nil
72
77
  end
73
78
 
@@ -84,21 +89,25 @@ module Barometer
84
89
  #
85
90
 
86
91
  # will just return the value (no units)
92
+ #
87
93
  def to_i(metric=nil)
88
94
  (metric || (metric.nil? && self.metric?)) ? self.km : self.m
89
95
  end
90
96
 
91
97
  # will just return the value (no units) with more precision
98
+ #
92
99
  def to_f(metric=nil)
93
100
  (metric || (metric.nil? && self.metric?)) ? self.km(false) : self.m(false)
94
101
  end
95
102
 
96
103
  # will return the value with units
104
+ #
97
105
  def to_s(metric=nil)
98
106
  (metric || (metric.nil? && self.metric?)) ? "#{self.km} #{METRIC_UNITS}" : "#{self.m} #{IMPERIAL_UNITS}"
99
107
  end
100
108
 
101
109
  # will just return the units (no value)
110
+ #
102
111
  def units(metric=nil)
103
112
  (metric || (metric.nil? && self.metric?)) ? METRIC_UNITS : IMPERIAL_UNITS
104
113
  end
@@ -107,7 +116,7 @@ module Barometer
107
116
  # kilometers remains. if so, clear it.
108
117
  def update_kilometers(m)
109
118
  return unless @kilometers
110
- difference = Distance.m_to_km(m.to_f) - @kilometers
119
+ difference = Data::Distance.m_to_km(m.to_f) - @kilometers
111
120
  # only clear kilometers if the stored kilometers is off be more then 1 unit
112
121
  # then the conversion of miles
113
122
  @kilometers = nil unless difference.abs <= 1.0
@@ -115,9 +124,10 @@ module Barometer
115
124
 
116
125
  # when we set kilometers, it is possible the a non-equivalent value of
117
126
  # miles remains. if so, clear it.
127
+ #
118
128
  def update_miles(km)
119
129
  return unless @miles
120
- difference = Distance.km_to_m(km.to_f) - @miles
130
+ difference = Data::Distance.km_to_m(km.to_f) - @miles
121
131
  # only clear miles if the stored miles is off be more then 1 unit
122
132
  # then the conversion of kilometers
123
133
  @miles = nil unless difference.abs <= 1.0
@@ -7,11 +7,13 @@ module Barometer
7
7
  # This is basically a data holding class for the forecasted weather
8
8
  # conditions.
9
9
  #
10
- class ForecastMeasurement
10
+ class Data::ForecastMeasurement
11
11
 
12
12
  attr_reader :date, :icon, :condition
13
- attr_reader :low, :high, :pop, :sun
13
+ attr_reader :low, :high, :pop, :wind, :humidity, :sun, :night
14
14
 
15
+ # accessors (with input checking)
16
+ #
15
17
  def date=(date)
16
18
  raise ArgumentError unless date.is_a?(Date)
17
19
  @date = date
@@ -28,12 +30,12 @@ module Barometer
28
30
  end
29
31
 
30
32
  def high=(high)
31
- raise ArgumentError unless high.is_a?(Barometer::Temperature)
33
+ raise ArgumentError unless high.is_a?(Data::Temperature)
32
34
  @high = high
33
35
  end
34
36
 
35
37
  def low=(low)
36
- raise ArgumentError unless low.is_a?(Barometer::Temperature)
38
+ raise ArgumentError unless low.is_a?(Data::Temperature)
37
39
  @low = low
38
40
  end
39
41
 
@@ -42,16 +44,32 @@ module Barometer
42
44
  @pop = pop
43
45
  end
44
46
 
47
+ def wind=(wind)
48
+ raise ArgumentError unless wind.is_a?(Data::Speed)
49
+ @wind = wind
50
+ end
51
+
52
+ def humidity=(humidity)
53
+ raise ArgumentError unless humidity.is_a?(Fixnum)
54
+ @humidity = humidity
55
+ end
56
+
45
57
  def sun=(sun)
46
- raise ArgumentError unless sun.is_a?(Barometer::Sun)
58
+ raise ArgumentError unless sun.is_a?(Data::Sun)
47
59
  @sun = sun
48
60
  end
49
61
 
62
+ def night=(night)
63
+ raise ArgumentError unless night.is_a?(Data::NightMeasurement)
64
+ @night = night
65
+ end
66
+
50
67
  #
51
68
  # helpers
52
69
  #
53
70
 
54
71
  # creates "?" helpers for all attributes (which maps to nil?)
72
+ #
55
73
  def method_missing(method,*args)
56
74
  # if the method ends in ?, then strip it off and see if we
57
75
  # respond to the method without the ?
@@ -2,71 +2,27 @@ module Barometer
2
2
  #
3
3
  # A simple Geo class
4
4
  #
5
- # Used to store location data from Graticule or HTTParty and convert
6
- # into just the data needed for geocoding
5
+ # Used to store location data
7
6
  #
8
- class Geo
7
+ class Data::Geo
9
8
 
10
- attr_accessor :latitude, :longitude
9
+ attr_accessor :latitude, :longitude, :query
11
10
  attr_accessor :locality, :region, :country, :country_code, :address
12
11
 
13
- #
14
- # this will take a Location object (either generated by Graticule
15
- # or HTTParty), and fill in the applicable data
16
- #
17
12
  def initialize(location=nil)
18
13
  return unless location
19
-
20
- has_graticule = false
21
- begin
22
- Graticule
23
- has_graticule = true
24
- rescue
25
- # do nothing, Graticule not available
26
- end
27
-
28
- if has_graticule
29
- raise ArgumentError unless
30
- (location.is_a?(Graticule::Location) || location.is_a?(Hash))
31
- else
32
- raise ArgumentError unless location.is_a?(Hash)
33
- end
34
-
35
- if has_graticule && location.class == Graticule::Location
36
- self.build_from_graticule(location)
37
- elsif location.class == Hash
38
- self.build_from_httparty(location)
39
- end
14
+ raise ArgumentError unless location.is_a?(Hash)
15
+ self.build_from_hash(location)
40
16
  self
41
17
  end
42
18
 
43
- def build_from_graticule(location=nil)
44
- return nil unless location
45
-
46
- begin
47
- require 'rubygems'
48
- require 'graticule'
49
- $:.unshift(File.dirname(__FILE__))
50
- # load some changes to Graticule
51
- # TODO: attempt to get changes into Graticule gem
52
- require 'extensions/graticule'
53
- end
54
-
55
- raise ArgumentError unless location.is_a?(Graticule::Location)
56
-
57
- @latitude = location.latitude
58
- @longitude = location.longitude
59
- @locality = location.locality
60
- @region = location.region
61
- @country = location.country
62
- @country_code = location.country_code
63
- @address = location.address_line
64
- end
65
-
66
- def build_from_httparty(location=nil)
19
+ # build the Geo object from a Hash
20
+ #
21
+ def build_from_hash(location=nil)
67
22
  return nil unless location
68
23
  raise ArgumentError unless location.is_a?(Hash)
69
24
 
25
+ @query = location["name"]
70
26
  placemark = location["Placemark"]
71
27
  placemark = placemark.first if placemark.is_a?(Array)
72
28
 
@@ -91,10 +47,16 @@ module Barometer
91
47
  @address = placemark["AddressDetails"]["Country"]["AddressLine"]
92
48
  end
93
49
  end
94
-
50
+
95
51
  def coordinates
96
52
  [@latitude, @longitude].join(',')
97
53
  end
98
54
 
55
+ def to_s
56
+ s = [@address, @locality, @region, @country || @country_code]
57
+ s.delete("")
58
+ s.compact.join(', ')
59
+ end
60
+
99
61
  end
100
62
  end