barometer 0.7.3 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/.travis.yml +7 -0
- data/LICENSE +1 -1
- data/{README.rdoc → README.md} +124 -110
- data/Rakefile +1 -21
- data/TODO +8 -9
- data/barometer.gemspec +20 -19
- data/bin/barometer +36 -83
- data/lib/barometer.rb +13 -11
- data/lib/barometer/base.rb +10 -10
- data/lib/barometer/data.rb +1 -1
- data/lib/barometer/data/distance.rb +25 -25
- data/lib/barometer/data/geo.rb +9 -9
- data/lib/barometer/data/local_datetime.rb +24 -20
- data/lib/barometer/data/local_time.rb +13 -13
- data/lib/barometer/data/location.rb +6 -6
- data/lib/barometer/data/pressure.rb +24 -24
- data/lib/barometer/data/speed.rb +28 -28
- data/lib/barometer/data/sun.rb +7 -7
- data/lib/barometer/data/temperature.rb +29 -29
- data/lib/barometer/data/units.rb +9 -9
- data/lib/barometer/data/zone.rb +19 -19
- data/lib/barometer/formats.rb +1 -1
- data/lib/barometer/formats/coordinates.rb +7 -7
- data/lib/barometer/formats/format.rb +6 -6
- data/lib/barometer/formats/geocode.rb +5 -5
- data/lib/barometer/formats/icao.rb +6 -6
- data/lib/barometer/formats/postalcode.rb +3 -3
- data/lib/barometer/formats/short_zipcode.rb +2 -2
- data/lib/barometer/formats/weather_id.rb +10 -10
- data/lib/barometer/formats/woe_id.rb +20 -20
- data/lib/barometer/formats/zipcode.rb +3 -3
- data/lib/barometer/key_file_parser.rb +20 -0
- data/lib/barometer/measurements/measurement.rb +32 -32
- data/lib/barometer/measurements/result.rb +39 -39
- data/lib/barometer/measurements/result_array.rb +12 -12
- data/lib/barometer/query.rb +15 -15
- data/lib/barometer/services.rb +3 -3
- data/lib/barometer/translations/icao_country_codes.yml +20 -20
- data/lib/barometer/translations/weather_country_codes.yml +1 -1
- data/lib/barometer/translations/zone_codes.yml +2 -2
- data/lib/barometer/version.rb +3 -0
- data/lib/barometer/weather.rb +27 -27
- data/lib/barometer/weather_services/noaa.rb +314 -3
- data/lib/barometer/weather_services/service.rb +32 -30
- data/lib/barometer/weather_services/weather_bug.rb +35 -33
- data/lib/barometer/weather_services/wunderground.rb +31 -29
- data/lib/barometer/weather_services/yahoo.rb +36 -35
- data/lib/barometer/web_services/geocode.rb +5 -7
- data/lib/barometer/web_services/noaa_station_id.rb +53 -0
- data/lib/barometer/web_services/placemaker.rb +11 -13
- data/lib/barometer/web_services/timezone.rb +5 -7
- data/lib/barometer/web_services/weather_id.rb +4 -6
- data/lib/barometer/web_services/web_service.rb +4 -4
- data/spec/barometer_spec.rb +25 -27
- data/spec/cassettes/Barometer.json +1 -0
- data/spec/cassettes/Query.json +1 -0
- data/spec/cassettes/Query_Format_Coordinates.json +1 -0
- data/spec/cassettes/Query_Format_Geocode.json +1 -0
- data/spec/cassettes/Query_Format_WeatherID.json +1 -0
- data/spec/cassettes/Query_Format_WoeID.json +1 -0
- data/spec/cassettes/WeatherService.json +1 -0
- data/spec/cassettes/WeatherService_Noaa.json +1 -0
- data/spec/cassettes/WeatherService_WeatherBug.json +1 -0
- data/spec/cassettes/WeatherService_Wunderground.json +1 -0
- data/spec/cassettes/WeatherService_Yahoo.json +1 -0
- data/spec/cassettes/WebService_Geocode.json +1 -0
- data/spec/cassettes/WebService_NoaaStation.json +1 -0
- data/spec/data/distance_spec.rb +60 -60
- data/spec/data/geo_spec.rb +23 -23
- data/spec/data/local_datetime_spec.rb +44 -44
- data/spec/data/local_time_spec.rb +47 -47
- data/spec/data/location_spec.rb +16 -16
- data/spec/data/pressure_spec.rb +61 -61
- data/spec/data/speed_spec.rb +69 -69
- data/spec/data/sun_spec.rb +25 -25
- data/spec/data/temperature_spec.rb +68 -68
- data/spec/data/units_spec.rb +21 -21
- data/spec/data/zone_spec.rb +35 -35
- data/spec/formats/coordinates_spec.rb +27 -27
- data/spec/formats/format_spec.rb +17 -25
- data/spec/formats/geocode_spec.rb +23 -31
- data/spec/formats/icao_spec.rb +26 -32
- data/spec/formats/postalcode_spec.rb +22 -28
- data/spec/formats/short_zipcode_spec.rb +20 -26
- data/spec/formats/weather_id_spec.rb +57 -67
- data/spec/formats/woe_id_spec.rb +59 -59
- data/spec/formats/zipcode_spec.rb +39 -47
- data/spec/key_file_parser_spec.rb +28 -0
- data/spec/measurements/measurement_spec.rb +79 -133
- data/spec/measurements/result_array_spec.rb +23 -38
- data/spec/measurements/result_spec.rb +100 -128
- data/spec/query_spec.rb +83 -100
- data/spec/spec_helper.rb +24 -6
- data/spec/weather_services/noaa_spec.rb +179 -0
- data/spec/weather_services/services_spec.rb +28 -36
- data/spec/weather_services/weather_bug_spec.rb +57 -77
- data/spec/weather_services/wunderground_spec.rb +36 -65
- data/spec/weather_services/yahoo_spec.rb +38 -60
- data/spec/weather_spec.rb +79 -79
- data/spec/web_services/geocode_spec.rb +7 -11
- data/spec/web_services/noaa_station_id_spec.rb +33 -0
- data/spec/web_services/placemaker_spec.rb +7 -12
- data/spec/web_services/web_services_spec.rb +3 -9
- metadata +214 -163
- data/VERSION.yml +0 -5
- data/lib/barometer/weather_services/google.rb +0 -142
- data/lib/barometer/weather_services/weather_dot_com.rb +0 -279
- data/spec/fakeweb_helper.rb +0 -179
- data/spec/fixtures/formats/weather_id/90210.xml +0 -7
- data/spec/fixtures/formats/weather_id/from_USGA0028.xml +0 -3
- data/spec/fixtures/formats/weather_id/ksfo.xml +0 -1
- data/spec/fixtures/formats/weather_id/manhattan.xml +0 -7
- data/spec/fixtures/formats/weather_id/new_york.xml +0 -1
- data/spec/fixtures/formats/weather_id/the_hills.xml +0 -1
- data/spec/fixtures/geocode/40_73_v3.json +0 -497
- data/spec/fixtures/geocode/90210_v3.json +0 -63
- data/spec/fixtures/geocode/T5B4M9_v3.json +0 -68
- data/spec/fixtures/geocode/atlanta_v3.json +0 -58
- data/spec/fixtures/geocode/calgary_ab_v3.json +0 -58
- data/spec/fixtures/geocode/ksfo_v3.json +0 -73
- data/spec/fixtures/geocode/newyork_ny_v3.json +0 -58
- data/spec/fixtures/services/google/calgary_ab.xml +0 -1
- data/spec/fixtures/services/placemaker/T5B4M9.xml +0 -65
- data/spec/fixtures/services/placemaker/atlanta.xml +0 -65
- data/spec/fixtures/services/placemaker/coords.xml +0 -65
- data/spec/fixtures/services/placemaker/ksfo.xml +0 -65
- data/spec/fixtures/services/placemaker/new_york.xml +0 -65
- data/spec/fixtures/services/placemaker/the_hills.xml +0 -65
- data/spec/fixtures/services/placemaker/w615702.xml +0 -47
- data/spec/fixtures/services/weather_bug/90210_current.xml +0 -93
- data/spec/fixtures/services/weather_bug/90210_forecast.xml +0 -76
- data/spec/fixtures/services/weather_dot_com/90210.xml +0 -1
- data/spec/fixtures/services/wunderground/current_calgary_ab.xml +0 -9
- data/spec/fixtures/services/wunderground/forecast_calgary_ab.xml +0 -13
- data/spec/fixtures/services/yahoo/90210.xml +0 -3
- data/spec/weather_services/google_spec.rb +0 -181
- data/spec/weather_services/weather_dot_com_spec.rb +0 -224
@@ -1,25 +1,25 @@
|
|
1
1
|
module Barometer
|
2
2
|
#
|
3
3
|
# A simple Location class
|
4
|
-
#
|
4
|
+
#
|
5
5
|
# Used to store location information about the station that
|
6
6
|
# gave the measurement data for a weather query, or the location
|
7
7
|
# that was queried
|
8
8
|
#
|
9
9
|
class Data::Location
|
10
|
-
|
10
|
+
|
11
11
|
attr_accessor :id, :name, :city
|
12
12
|
attr_accessor :state_name, :state_code, :country, :country_code, :zip_code
|
13
13
|
attr_accessor :latitude, :longitude
|
14
|
-
|
14
|
+
|
15
15
|
def coordinates
|
16
16
|
[@latitude, @longitude].join(',')
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
def to_s
|
20
20
|
[@name, @city, @state_name || @state_cocde,
|
21
21
|
@country || @country_code].compact.join(', ')
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
end
|
25
|
-
end
|
25
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module Barometer
|
2
2
|
#
|
3
3
|
# A simple Pressure class
|
4
|
-
#
|
4
|
+
#
|
5
5
|
# Think of this like the Integer class. Enhancement
|
6
6
|
# is that you can create a number (in a certain unit), then
|
7
7
|
# get that number back already converted to another unit.
|
@@ -13,40 +13,40 @@ module Barometer
|
|
13
13
|
# and it seems that it is more common in this case
|
14
14
|
#
|
15
15
|
class Data::Pressure < Data::Units
|
16
|
-
|
16
|
+
|
17
17
|
METRIC_UNITS = "mb"
|
18
18
|
IMPERIAL_UNITS = "in"
|
19
|
-
|
19
|
+
|
20
20
|
attr_accessor :millibars, :inches
|
21
|
-
|
21
|
+
|
22
22
|
def initialize(metric=true)
|
23
23
|
@millibars = nil
|
24
24
|
@inches = nil
|
25
25
|
super(metric)
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
def metric_default=(value); self.mb = value; end
|
29
29
|
def imperial_default=(value); self.in = value; end
|
30
30
|
|
31
31
|
#
|
32
32
|
# CONVERTERS
|
33
33
|
#
|
34
|
-
|
34
|
+
|
35
35
|
def self.mb_to_in(mb)
|
36
36
|
return nil unless mb && (mb.is_a?(Integer) || mb.is_a?(Float))
|
37
37
|
mb.to_f * 0.02953
|
38
38
|
end
|
39
|
-
|
39
|
+
|
40
40
|
def self.in_to_mb(inches)
|
41
41
|
return nil unless inches &&
|
42
42
|
(inches.is_a?(Integer) || inches.is_a?(Float))
|
43
43
|
inches.to_f * 33.8639
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
#
|
47
47
|
# ACCESSORS
|
48
48
|
#
|
49
|
-
|
49
|
+
|
50
50
|
# store millibars
|
51
51
|
#
|
52
52
|
def mb=(mb)
|
@@ -54,7 +54,7 @@ module Barometer
|
|
54
54
|
@millibars = mb.to_f
|
55
55
|
self.update_inches(mb.to_f)
|
56
56
|
end
|
57
|
-
|
57
|
+
|
58
58
|
# store inches
|
59
59
|
#
|
60
60
|
def in=(inches)
|
@@ -62,57 +62,57 @@ module Barometer
|
|
62
62
|
@inches = inches.to_f
|
63
63
|
self.update_millibars(inches.to_f)
|
64
64
|
end
|
65
|
-
|
65
|
+
|
66
66
|
# return the stored millibars or convert from inches
|
67
67
|
#
|
68
68
|
def mb(as_integer=true)
|
69
69
|
mb = (@millibars || Data::Pressure.in_to_mb(@inches))
|
70
70
|
mb ? (as_integer ? mb.to_i : (100*mb).round/100.0) : nil
|
71
71
|
end
|
72
|
-
|
72
|
+
|
73
73
|
# return the stored inches or convert from millibars
|
74
74
|
#
|
75
75
|
def in(as_integer=true)
|
76
76
|
inches = (@inches || Data::Pressure.mb_to_in(@millibars))
|
77
77
|
inches ? (as_integer ? inches.to_i : (100*inches).round/100.0) : nil
|
78
78
|
end
|
79
|
-
|
79
|
+
|
80
80
|
#
|
81
81
|
# OPERATORS
|
82
82
|
#
|
83
|
-
|
83
|
+
|
84
84
|
def <=>(other)
|
85
85
|
self.mb <=> other.mb
|
86
86
|
end
|
87
|
-
|
87
|
+
|
88
88
|
#
|
89
89
|
# HELPERS
|
90
90
|
#
|
91
|
-
|
91
|
+
|
92
92
|
# will just return the value (no units)
|
93
93
|
#
|
94
94
|
def to_i(metric=nil)
|
95
95
|
(metric || (metric.nil? && self.metric?)) ? self.mb : self.in
|
96
96
|
end
|
97
|
-
|
97
|
+
|
98
98
|
# will just return the value (no units) with more precision
|
99
99
|
#
|
100
100
|
def to_f(metric=nil)
|
101
101
|
(metric || (metric.nil? && self.metric?)) ? self.mb(false) : self.in(false)
|
102
102
|
end
|
103
|
-
|
103
|
+
|
104
104
|
# will return the value with units
|
105
105
|
#
|
106
106
|
def to_s(metric=nil)
|
107
107
|
(metric || (metric.nil? && self.metric?)) ? "#{self.mb} #{METRIC_UNITS}" : "#{self.in} #{IMPERIAL_UNITS}"
|
108
108
|
end
|
109
|
-
|
109
|
+
|
110
110
|
# will just return the units (no value)
|
111
111
|
#
|
112
112
|
def units(metric=nil)
|
113
113
|
(metric || (metric.nil? && self.metric?)) ? METRIC_UNITS : IMPERIAL_UNITS
|
114
114
|
end
|
115
|
-
|
115
|
+
|
116
116
|
# when we set inches, it is possible the a non-equivalent value of
|
117
117
|
# millibars remains. if so, clear it.
|
118
118
|
#
|
@@ -123,7 +123,7 @@ module Barometer
|
|
123
123
|
# then the conversion of inches
|
124
124
|
@millibars = nil unless difference.abs <= 1.0
|
125
125
|
end
|
126
|
-
|
126
|
+
|
127
127
|
# when we set millibars, it is possible the a non-equivalent value of
|
128
128
|
# inches remains. if so, clear it.
|
129
129
|
#
|
@@ -134,10 +134,10 @@ module Barometer
|
|
134
134
|
# then the conversion of millibars
|
135
135
|
@inches = nil unless difference.abs <= 1.0
|
136
136
|
end
|
137
|
-
|
137
|
+
|
138
138
|
def nil?
|
139
139
|
(@millibars || @inches) ? false : true
|
140
140
|
end
|
141
|
-
|
141
|
+
|
142
142
|
end
|
143
|
-
end
|
143
|
+
end
|
data/lib/barometer/data/speed.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module Barometer
|
2
2
|
#
|
3
3
|
# A simple Speed class
|
4
|
-
#
|
4
|
+
#
|
5
5
|
# Think of this like the Integer class. Enhancement
|
6
6
|
# is that you can create a number (in a certain unit), then
|
7
7
|
# get that number back already converted to another unit.
|
@@ -10,19 +10,19 @@ module Barometer
|
|
10
10
|
# the direction is independent of the units
|
11
11
|
#
|
12
12
|
# All comparison operations will be done in metric
|
13
|
-
#
|
13
|
+
#
|
14
14
|
# NOTE: this currently only supports the scale of
|
15
15
|
# kilometers (km) and miles (m) per hour. There is currently
|
16
16
|
# no way to scale to smaller units (eg km -> m -> mm)
|
17
17
|
#
|
18
18
|
class Data::Speed < Data::Units
|
19
|
-
|
19
|
+
|
20
20
|
METRIC_UNITS = "kph"
|
21
21
|
IMPERIAL_UNITS = "mph"
|
22
|
-
|
22
|
+
|
23
23
|
attr_accessor :kilometers, :miles
|
24
24
|
attr_accessor :degrees, :direction
|
25
|
-
|
25
|
+
|
26
26
|
def initialize(metric=true)
|
27
27
|
@kilometers = nil
|
28
28
|
@miles = nil
|
@@ -30,28 +30,28 @@ module Barometer
|
|
30
30
|
@direction = nil
|
31
31
|
super(metric)
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
def metric_default=(value); self.kph = value; end
|
35
35
|
def imperial_default=(value); self.mph = value; end
|
36
|
-
|
36
|
+
|
37
37
|
#
|
38
38
|
# CONVERTERS
|
39
39
|
#
|
40
|
-
|
40
|
+
|
41
41
|
def self.km_to_m(km)
|
42
42
|
return nil unless km && (km.is_a?(Integer) || km.is_a?(Float))
|
43
43
|
km.to_f * 0.622
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
def self.m_to_km(m)
|
47
47
|
return nil unless m && (m.is_a?(Integer) || m.is_a?(Float))
|
48
48
|
m.to_f * 1.609
|
49
49
|
end
|
50
|
-
|
50
|
+
|
51
51
|
#
|
52
52
|
# ACCESSORS
|
53
53
|
#
|
54
|
-
|
54
|
+
|
55
55
|
# store kilometers per hour
|
56
56
|
#
|
57
57
|
def kph=(kph)
|
@@ -59,7 +59,7 @@ module Barometer
|
|
59
59
|
@kilometers = kph.to_f
|
60
60
|
self.update_miles(kph.to_f)
|
61
61
|
end
|
62
|
-
|
62
|
+
|
63
63
|
# store miles per hour
|
64
64
|
#
|
65
65
|
def mph=(mph)
|
@@ -67,67 +67,67 @@ module Barometer
|
|
67
67
|
@miles = mph.to_f
|
68
68
|
self.update_kilometers(mph.to_f)
|
69
69
|
end
|
70
|
-
|
70
|
+
|
71
71
|
def direction=(direction)
|
72
72
|
return if !direction || !direction.is_a?(String)
|
73
73
|
@direction = direction
|
74
74
|
end
|
75
|
-
|
75
|
+
|
76
76
|
def degrees=(degrees)
|
77
77
|
return if !degrees || !(degrees.is_a?(Integer) || degrees.is_a?(Float))
|
78
78
|
@degrees = degrees
|
79
79
|
end
|
80
|
-
|
80
|
+
|
81
81
|
# return the stored kilometers or convert from miles
|
82
82
|
#
|
83
83
|
def kph(as_integer=true)
|
84
84
|
km = (@kilometers || Data::Speed.m_to_km(@miles))
|
85
85
|
km ? (as_integer ? km.to_i : (100*km).round/100.0) : nil
|
86
86
|
end
|
87
|
-
|
87
|
+
|
88
88
|
# return the stored miles or convert from kilometers
|
89
89
|
#
|
90
90
|
def mph(as_integer=true)
|
91
91
|
m = (@miles || Data::Speed.km_to_m(@kilometers))
|
92
92
|
m ? (as_integer ? m.to_i : (100*m).round/100.0) : nil
|
93
93
|
end
|
94
|
-
|
94
|
+
|
95
95
|
#
|
96
96
|
# OPERATORS
|
97
97
|
#
|
98
|
-
|
98
|
+
|
99
99
|
def <=>(other)
|
100
100
|
self.kph <=> other.kph
|
101
101
|
end
|
102
|
-
|
102
|
+
|
103
103
|
#
|
104
104
|
# HELPERS
|
105
105
|
#
|
106
|
-
|
106
|
+
|
107
107
|
# will just return the value (no units)
|
108
108
|
#
|
109
109
|
def to_i(metric=nil)
|
110
110
|
(metric || (metric.nil? && self.metric?)) ? self.kph : self.mph
|
111
111
|
end
|
112
|
-
|
112
|
+
|
113
113
|
# will just return the value (no units) with more precision
|
114
114
|
#
|
115
115
|
def to_f(metric=nil)
|
116
116
|
(metric || (metric.nil? && self.metric?)) ? self.kph(false) : self.mph(false)
|
117
117
|
end
|
118
|
-
|
118
|
+
|
119
119
|
# will return the value with units
|
120
120
|
#
|
121
121
|
def to_s(metric=nil)
|
122
122
|
(metric || (metric.nil? && self.metric?)) ? "#{self.kph} #{METRIC_UNITS}" : "#{self.mph} #{IMPERIAL_UNITS}"
|
123
123
|
end
|
124
|
-
|
124
|
+
|
125
125
|
# will just return the units (no value)
|
126
126
|
#
|
127
127
|
def units(metric=nil)
|
128
128
|
(metric || (metric.nil? && self.metric?)) ? METRIC_UNITS : IMPERIAL_UNITS
|
129
129
|
end
|
130
|
-
|
130
|
+
|
131
131
|
# when we set miles, it is possible the a non-equivalent value of
|
132
132
|
# kilometers remains. if so, clear it.
|
133
133
|
#
|
@@ -138,7 +138,7 @@ module Barometer
|
|
138
138
|
# then the conversion of miles
|
139
139
|
@kilometers = nil unless difference.abs <= 1.0
|
140
140
|
end
|
141
|
-
|
141
|
+
|
142
142
|
# when we set kilometers, it is possible the a non-equivalent value of
|
143
143
|
# miles remains. if so, clear it.
|
144
144
|
#
|
@@ -149,10 +149,10 @@ module Barometer
|
|
149
149
|
# then the conversion of kilometers
|
150
150
|
@miles = nil unless difference.abs <= 1.0
|
151
151
|
end
|
152
|
-
|
152
|
+
|
153
153
|
def nil?
|
154
154
|
(@kilometers || @miles) ? false : true
|
155
155
|
end
|
156
|
-
|
156
|
+
|
157
157
|
end
|
158
|
-
end
|
158
|
+
end
|
data/lib/barometer/data/sun.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
module Barometer
|
2
2
|
#
|
3
3
|
# A simple Sun class
|
4
|
-
#
|
4
|
+
#
|
5
5
|
# Used to store sunrise and sunset information
|
6
6
|
#
|
7
7
|
class Data::Sun
|
8
|
-
|
8
|
+
|
9
9
|
attr_reader :rise, :set
|
10
|
-
|
10
|
+
|
11
11
|
def initialize(rise=nil, set=nil)
|
12
12
|
raise ArgumentError unless (rise.is_a?(Data::LocalTime) || rise.nil?)
|
13
13
|
raise ArgumentError unless (set.is_a?(Data::LocalTime) || set.nil?)
|
@@ -18,16 +18,16 @@ module Barometer
|
|
18
18
|
def nil?
|
19
19
|
(@rise || @set) ? false : true
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
def after_rise?(time)
|
23
23
|
raise ArgumentError unless time.is_a?(Data::LocalTime)
|
24
24
|
time >= @rise
|
25
25
|
end
|
26
|
-
|
26
|
+
|
27
27
|
def before_set?(time)
|
28
28
|
raise ArgumentError unless time.is_a?(Data::LocalTime)
|
29
29
|
time <= @set
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
32
|
end
|
33
|
-
end
|
33
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module Barometer
|
2
2
|
#
|
3
3
|
# A simple Temperature class
|
4
|
-
#
|
4
|
+
#
|
5
5
|
# Think of this like the Integer class. Enhancement
|
6
6
|
# is that you can create a number (in a certain unit), then
|
7
7
|
# get that number back already converted to another unit.
|
@@ -10,66 +10,66 @@ module Barometer
|
|
10
10
|
# scale of Kelvin (K)
|
11
11
|
#
|
12
12
|
class Data::Temperature < Data::Units
|
13
|
-
|
13
|
+
|
14
14
|
METRIC_UNITS = "C"
|
15
15
|
IMPERIAL_UNITS = "F"
|
16
|
-
|
16
|
+
|
17
17
|
attr_accessor :celsius, :fahrenheit, :kelvin
|
18
|
-
|
18
|
+
|
19
19
|
def initialize(metric=true)
|
20
20
|
@celsius = nil
|
21
21
|
@fahrenheit = nil
|
22
22
|
@kelvin = nil
|
23
23
|
super(metric)
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
def metric_default=(value); self.c = value; end
|
27
27
|
def imperial_default=(value); self.f = value; end
|
28
28
|
|
29
29
|
#
|
30
30
|
# CONVERTERS
|
31
31
|
#
|
32
|
-
|
32
|
+
|
33
33
|
def self.c_to_k(c)
|
34
34
|
return nil unless c && (c.is_a?(Integer) || c.is_a?(Float))
|
35
35
|
273.15 + c.to_f
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
38
|
# Tf = (9/5)*Tc+32
|
39
39
|
#
|
40
40
|
def self.c_to_f(c)
|
41
41
|
return nil unless c && (c.is_a?(Integer) || c.is_a?(Float))
|
42
42
|
((9.0/5.0)*c.to_f)+32.0
|
43
43
|
end
|
44
|
-
|
44
|
+
|
45
45
|
# Tc = (5/9)*(Tf-32)
|
46
46
|
#
|
47
47
|
def self.f_to_c(f)
|
48
48
|
return nil unless f && (f.is_a?(Integer) || f.is_a?(Float))
|
49
49
|
(5.0/9.0)*(f.to_f-32.0)
|
50
50
|
end
|
51
|
-
|
51
|
+
|
52
52
|
def self.f_to_k(f)
|
53
53
|
return nil unless f && (f.is_a?(Integer) || f.is_a?(Float))
|
54
54
|
c = self.f_to_c(f.to_f)
|
55
55
|
self.c_to_k(c)
|
56
56
|
end
|
57
|
-
|
57
|
+
|
58
58
|
def self.k_to_c(k)
|
59
59
|
return nil unless k && (k.is_a?(Integer) || k.is_a?(Float))
|
60
60
|
k.to_f - 273.15
|
61
61
|
end
|
62
|
-
|
62
|
+
|
63
63
|
def self.k_to_f(k)
|
64
64
|
return nil unless k && (k.is_a?(Integer) || k.is_a?(Float))
|
65
65
|
c = self.k_to_c(k.to_f)
|
66
66
|
self.c_to_f(c)
|
67
67
|
end
|
68
|
-
|
68
|
+
|
69
69
|
#
|
70
70
|
# ACCESSORS
|
71
71
|
#
|
72
|
-
|
72
|
+
|
73
73
|
# store celsius and kelvin
|
74
74
|
#
|
75
75
|
def c=(c)
|
@@ -78,7 +78,7 @@ module Barometer
|
|
78
78
|
@kelvin = Data::Temperature.c_to_k(c.to_f)
|
79
79
|
self.update_fahrenheit(c.to_f)
|
80
80
|
end
|
81
|
-
|
81
|
+
|
82
82
|
# store fahrenheit and kelvin
|
83
83
|
#
|
84
84
|
def f=(f)
|
@@ -87,7 +87,7 @@ module Barometer
|
|
87
87
|
@kelvin = Data::Temperature.f_to_k(f.to_f)
|
88
88
|
self.update_celsius(f.to_f)
|
89
89
|
end
|
90
|
-
|
90
|
+
|
91
91
|
# store kelvin, convert to all
|
92
92
|
#
|
93
93
|
def k=(k)
|
@@ -96,57 +96,57 @@ module Barometer
|
|
96
96
|
@celsius = Data::Temperature.k_to_c(k.to_f)
|
97
97
|
@fahrenheit = Data::Temperature.k_to_f(k.to_f)
|
98
98
|
end
|
99
|
-
|
99
|
+
|
100
100
|
# return the stored celsius or convert from Kelvin
|
101
101
|
#
|
102
102
|
def c(as_integer=true)
|
103
103
|
c = (@celsius || Data::Temperature.k_to_c(@kelvin))
|
104
104
|
c ? (as_integer ? c.to_i : (100*c).round/100.0) : nil
|
105
105
|
end
|
106
|
-
|
106
|
+
|
107
107
|
# return the stored fahrenheit or convert from Kelvin
|
108
108
|
#
|
109
109
|
def f(as_integer=true)
|
110
110
|
f = (@fahrenheit || Data::Temperature.k_to_f(@kelvin))
|
111
111
|
f ? (as_integer ? f.to_i : (100*f).round/100.0) : nil
|
112
112
|
end
|
113
|
-
|
113
|
+
|
114
114
|
#
|
115
115
|
# OPERATORS
|
116
116
|
#
|
117
|
-
|
117
|
+
|
118
118
|
def <=>(other)
|
119
119
|
@kelvin <=> other.kelvin
|
120
120
|
end
|
121
|
-
|
121
|
+
|
122
122
|
#
|
123
123
|
# HELPERS
|
124
124
|
#
|
125
|
-
|
125
|
+
|
126
126
|
# will just return the value (no units)
|
127
127
|
#
|
128
128
|
def to_i(metric=nil)
|
129
129
|
(metric || (metric.nil? && self.metric?)) ? self.c : self.f
|
130
130
|
end
|
131
|
-
|
131
|
+
|
132
132
|
# will just return the value (no units) with more precision
|
133
133
|
#
|
134
134
|
def to_f(metric=nil)
|
135
135
|
(metric || (metric.nil? && self.metric?)) ? self.c(false) : self.f(false)
|
136
136
|
end
|
137
|
-
|
137
|
+
|
138
138
|
# will return the value with units
|
139
139
|
#
|
140
140
|
def to_s(metric=nil)
|
141
141
|
(metric || (metric.nil? && self.metric?)) ? "#{self.c} #{METRIC_UNITS}" : "#{self.f} #{IMPERIAL_UNITS}"
|
142
142
|
end
|
143
|
-
|
143
|
+
|
144
144
|
# will just return the units (no value)
|
145
145
|
#
|
146
146
|
def units(metric=nil)
|
147
147
|
(metric || (metric.nil? && self.metric?)) ? METRIC_UNITS : IMPERIAL_UNITS
|
148
148
|
end
|
149
|
-
|
149
|
+
|
150
150
|
# when we set fahrenheit, it is possible the a non-equivalent value of
|
151
151
|
# celsius remains. if so, clear it.
|
152
152
|
#
|
@@ -157,7 +157,7 @@ module Barometer
|
|
157
157
|
# then the conversion of fahrenheit
|
158
158
|
@celsius = nil unless difference.abs <= 1.0
|
159
159
|
end
|
160
|
-
|
160
|
+
|
161
161
|
# when we set celsius, it is possible the a non-equivalent value of
|
162
162
|
# fahrenheit remains. if so, clear it.
|
163
163
|
#
|
@@ -168,10 +168,10 @@ module Barometer
|
|
168
168
|
# then the conversion of celsius
|
169
169
|
@fahrenheit = nil unless difference.abs <= 1.0
|
170
170
|
end
|
171
|
-
|
171
|
+
|
172
172
|
def nil?
|
173
173
|
(@celsius || @fahrenheit || @kelvin) ? false : true
|
174
174
|
end
|
175
|
-
|
175
|
+
|
176
176
|
end
|
177
|
-
end
|
177
|
+
end
|