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
data/lib/barometer/base.rb
CHANGED
@@ -1,22 +1,22 @@
|
|
1
1
|
module Barometer
|
2
2
|
class Base
|
3
|
-
|
3
|
+
|
4
4
|
# allow the configuration of specific weather APIs to be used,
|
5
5
|
# and the order in which they would be used
|
6
6
|
#
|
7
7
|
@@config = { 1 => [:wunderground] }
|
8
8
|
def self.config; @@config; end;
|
9
9
|
def self.config=(hash); @@config = hash; end;
|
10
|
-
|
10
|
+
|
11
11
|
attr_reader :query
|
12
12
|
attr_accessor :weather, :success
|
13
|
-
|
13
|
+
|
14
14
|
def initialize(query=nil)
|
15
15
|
@query = Barometer::Query.new(query)
|
16
16
|
@weather = Barometer::Weather.new
|
17
17
|
@success = false
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
# iterate through all the configured sources and
|
21
21
|
# collect weather data for each one
|
22
22
|
#
|
@@ -33,15 +33,15 @@ module Barometer
|
|
33
33
|
end
|
34
34
|
level += 1
|
35
35
|
end
|
36
|
-
|
36
|
+
|
37
37
|
@weather.end_at = Time.now.utc
|
38
38
|
@weather
|
39
39
|
end
|
40
|
-
|
40
|
+
|
41
41
|
def success?; @success; end
|
42
|
-
|
42
|
+
|
43
43
|
private
|
44
|
-
|
44
|
+
|
45
45
|
# iterate through the setup until we have a source name (and possibly
|
46
46
|
# a config for that source), then measure with that source
|
47
47
|
#
|
@@ -66,7 +66,7 @@ module Barometer
|
|
66
66
|
end
|
67
67
|
end
|
68
68
|
end
|
69
|
-
|
69
|
+
|
70
70
|
# do that actual source measurement
|
71
71
|
#
|
72
72
|
def _measure(datum, config=nil, metric=nil)
|
@@ -80,4 +80,4 @@ module Barometer
|
|
80
80
|
end
|
81
81
|
|
82
82
|
end
|
83
|
-
end
|
83
|
+
end
|
data/lib/barometer/data.rb
CHANGED
@@ -1,51 +1,51 @@
|
|
1
1
|
module Barometer
|
2
2
|
#
|
3
3
|
# A simple Distance 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.
|
8
8
|
#
|
9
9
|
# All comparison operations will be done in metric
|
10
|
-
#
|
10
|
+
#
|
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
14
|
#
|
15
15
|
class Data::Distance < Data::Units
|
16
|
-
|
16
|
+
|
17
17
|
METRIC_UNITS = "km"
|
18
18
|
IMPERIAL_UNITS = "m"
|
19
|
-
|
19
|
+
|
20
20
|
attr_accessor :kilometers, :miles
|
21
|
-
|
21
|
+
|
22
22
|
def initialize(metric=true)
|
23
23
|
@kilometers = nil
|
24
24
|
@miles = nil
|
25
25
|
super(metric)
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
def metric_default=(value); self.km = value; end
|
29
29
|
def imperial_default=(value); self.m = value; end
|
30
30
|
|
31
31
|
#
|
32
32
|
# CONVERTERS
|
33
33
|
#
|
34
|
-
|
34
|
+
|
35
35
|
def self.km_to_m(km)
|
36
36
|
return nil unless km && (km.is_a?(Integer) || km.is_a?(Float))
|
37
37
|
km.to_f * 0.622
|
38
38
|
end
|
39
|
-
|
39
|
+
|
40
40
|
def self.m_to_km(m)
|
41
41
|
return nil unless m && (m.is_a?(Integer) || m.is_a?(Float))
|
42
42
|
m.to_f * 1.609
|
43
43
|
end
|
44
|
-
|
44
|
+
|
45
45
|
#
|
46
46
|
# ACCESSORS
|
47
47
|
#
|
48
|
-
|
48
|
+
|
49
49
|
# store kilometers
|
50
50
|
#
|
51
51
|
def km=(km)
|
@@ -53,7 +53,7 @@ module Barometer
|
|
53
53
|
@kilometers = km.to_f
|
54
54
|
self.update_miles(km.to_f)
|
55
55
|
end
|
56
|
-
|
56
|
+
|
57
57
|
# store miles
|
58
58
|
#
|
59
59
|
def m=(m)
|
@@ -61,57 +61,57 @@ module Barometer
|
|
61
61
|
@miles = m.to_f
|
62
62
|
self.update_kilometers(m.to_f)
|
63
63
|
end
|
64
|
-
|
64
|
+
|
65
65
|
# return the stored kilometers or convert from miles
|
66
66
|
#
|
67
67
|
def km(as_integer=true)
|
68
68
|
km = (@kilometers || Data::Distance.m_to_km(@miles))
|
69
69
|
km ? (as_integer ? km.to_i : (100*km).round/100.0) : nil
|
70
70
|
end
|
71
|
-
|
71
|
+
|
72
72
|
# return the stored miles or convert from kilometers
|
73
73
|
#
|
74
74
|
def m(as_integer=true)
|
75
75
|
m = (@miles || Data::Distance.km_to_m(@kilometers))
|
76
76
|
m ? (as_integer ? m.to_i : (100*m).round/100.0) : nil
|
77
77
|
end
|
78
|
-
|
78
|
+
|
79
79
|
#
|
80
80
|
# OPERATORS
|
81
81
|
#
|
82
|
-
|
82
|
+
|
83
83
|
def <=>(other)
|
84
84
|
self.km <=> other.km
|
85
85
|
end
|
86
|
-
|
86
|
+
|
87
87
|
#
|
88
88
|
# HELPERS
|
89
89
|
#
|
90
|
-
|
90
|
+
|
91
91
|
# will just return the value (no units)
|
92
92
|
#
|
93
93
|
def to_i(metric=nil)
|
94
94
|
(metric || (metric.nil? && self.metric?)) ? self.km : self.m
|
95
95
|
end
|
96
|
-
|
96
|
+
|
97
97
|
# will just return the value (no units) with more precision
|
98
98
|
#
|
99
99
|
def to_f(metric=nil)
|
100
100
|
(metric || (metric.nil? && self.metric?)) ? self.km(false) : self.m(false)
|
101
101
|
end
|
102
|
-
|
102
|
+
|
103
103
|
# will return the value with units
|
104
104
|
#
|
105
105
|
def to_s(metric=nil)
|
106
106
|
(metric || (metric.nil? && self.metric?)) ? "#{self.km} #{METRIC_UNITS}" : "#{self.m} #{IMPERIAL_UNITS}"
|
107
107
|
end
|
108
|
-
|
108
|
+
|
109
109
|
# will just return the units (no value)
|
110
110
|
#
|
111
111
|
def units(metric=nil)
|
112
112
|
(metric || (metric.nil? && self.metric?)) ? METRIC_UNITS : IMPERIAL_UNITS
|
113
113
|
end
|
114
|
-
|
114
|
+
|
115
115
|
# when we set miles, it is possible the a non-equivalent value of
|
116
116
|
# kilometers remains. if so, clear it.
|
117
117
|
def update_kilometers(m)
|
@@ -121,7 +121,7 @@ module Barometer
|
|
121
121
|
# then the conversion of miles
|
122
122
|
@kilometers = nil unless difference.abs <= 1.0
|
123
123
|
end
|
124
|
-
|
124
|
+
|
125
125
|
# when we set kilometers, it is possible the a non-equivalent value of
|
126
126
|
# miles remains. if so, clear it.
|
127
127
|
#
|
@@ -132,10 +132,10 @@ module Barometer
|
|
132
132
|
# then the conversion of kilometers
|
133
133
|
@miles = nil unless difference.abs <= 1.0
|
134
134
|
end
|
135
|
-
|
135
|
+
|
136
136
|
def nil?
|
137
137
|
(@kilometers || @miles) ? false : true
|
138
138
|
end
|
139
|
-
|
139
|
+
|
140
140
|
end
|
141
|
-
end
|
141
|
+
end
|
data/lib/barometer/data/geo.rb
CHANGED
@@ -1,30 +1,30 @@
|
|
1
1
|
module Barometer
|
2
2
|
#
|
3
3
|
# A simple Geo class
|
4
|
-
#
|
4
|
+
#
|
5
5
|
# Used to store location data
|
6
6
|
#
|
7
7
|
class Data::Geo
|
8
|
-
|
8
|
+
|
9
9
|
attr_accessor :latitude, :longitude, :query
|
10
10
|
attr_accessor :locality, :region, :country, :country_code, :address
|
11
|
-
|
11
|
+
|
12
12
|
def initialize(location=nil)
|
13
13
|
return unless location
|
14
14
|
raise ArgumentError unless location.is_a?(Hash)
|
15
15
|
self.build_from_hash(location)
|
16
16
|
self
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
def build_from_hash(location=nil)
|
20
20
|
return nil unless location
|
21
21
|
raise ArgumentError unless location.is_a?(Hash)
|
22
|
-
|
22
|
+
|
23
23
|
if location["geometry"] && location["geometry"]["location"]
|
24
24
|
@latitude = location["geometry"]["location"]["lat"].to_f
|
25
25
|
@longitude = location["geometry"]["location"]["lng"].to_f
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
query_parts = []
|
29
29
|
if location["address_components"]
|
30
30
|
location["address_components"].each do |address_components|
|
@@ -49,7 +49,7 @@ module Barometer
|
|
49
49
|
end
|
50
50
|
end
|
51
51
|
end
|
52
|
-
|
52
|
+
|
53
53
|
@query = query_parts.join(', ')
|
54
54
|
@address = ""
|
55
55
|
end
|
@@ -57,7 +57,7 @@ module Barometer
|
|
57
57
|
def coordinates
|
58
58
|
[@latitude, @longitude].join(',')
|
59
59
|
end
|
60
|
-
|
60
|
+
|
61
61
|
def to_s
|
62
62
|
s = [@address, @locality, @region, @country || @country_code]
|
63
63
|
s.delete("")
|
@@ -65,4 +65,4 @@ module Barometer
|
|
65
65
|
end
|
66
66
|
|
67
67
|
end
|
68
|
-
end
|
68
|
+
end
|
@@ -2,12 +2,12 @@ require 'date'
|
|
2
2
|
module Barometer
|
3
3
|
#
|
4
4
|
# A simple DateTime class
|
5
|
-
#
|
5
|
+
#
|
6
6
|
# A time class that represents the local date_time ...
|
7
7
|
# it has no concept of time zone
|
8
8
|
#
|
9
9
|
class Data::LocalDateTime < Data::LocalTime
|
10
|
-
|
10
|
+
|
11
11
|
attr_reader :year, :month, :day
|
12
12
|
|
13
13
|
def initialize(y,mon,d,h=0,m=0,s=0)
|
@@ -18,31 +18,31 @@ module Barometer
|
|
18
18
|
super(h,m,s)
|
19
19
|
self
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
def year=(y)
|
23
23
|
unless y && y.is_a?(Fixnum) && Date.civil(y,@month,@day)
|
24
24
|
raise(ArgumentError, "invalid year")
|
25
25
|
end
|
26
26
|
@year = y
|
27
27
|
end
|
28
|
-
|
28
|
+
|
29
29
|
def month=(m)
|
30
30
|
unless m && m.is_a?(Fixnum) && Date.civil(@year,m,@day)
|
31
31
|
raise(ArgumentError, "invalid month")
|
32
32
|
end
|
33
33
|
@month = m
|
34
34
|
end
|
35
|
-
|
35
|
+
|
36
36
|
def day=(d)
|
37
37
|
unless d && d.is_a?(Fixnum) && Date.civil(@year,@month,d)
|
38
38
|
raise(ArgumentError, "invalid day")
|
39
39
|
end
|
40
40
|
@day = d
|
41
41
|
end
|
42
|
-
|
43
|
-
def parse(string)
|
42
|
+
|
43
|
+
def parse(string, format=nil)
|
44
44
|
return unless string
|
45
|
-
new_date = Data::LocalDateTime.parse(string)
|
45
|
+
new_date = Data::LocalDateTime.parse(string, format)
|
46
46
|
@year = new_date.year
|
47
47
|
@month = new_date.month
|
48
48
|
@day = new_date.day
|
@@ -51,11 +51,11 @@ module Barometer
|
|
51
51
|
@sec = new_date.sec
|
52
52
|
self
|
53
53
|
end
|
54
|
-
|
55
|
-
def self.parse(string)
|
54
|
+
|
55
|
+
def self.parse(string, format=nil)
|
56
56
|
return nil unless string
|
57
57
|
return string if string.is_a?(Data::LocalDateTime)
|
58
|
-
|
58
|
+
|
59
59
|
year = nil; month = nil; day = nil;
|
60
60
|
hour = nil; min = nil; sec = nil;
|
61
61
|
if string.is_a?(Time) || string.is_a?(DateTime)
|
@@ -71,7 +71,11 @@ module Barometer
|
|
71
71
|
day = string.day
|
72
72
|
elsif string.is_a?(String)
|
73
73
|
begin
|
74
|
-
datetime =
|
74
|
+
datetime = if format
|
75
|
+
Time.strptime(string, format)
|
76
|
+
else
|
77
|
+
DateTime.parse(string)
|
78
|
+
end
|
75
79
|
year = datetime.year
|
76
80
|
month = datetime.mon
|
77
81
|
day = datetime.day
|
@@ -84,19 +88,19 @@ module Barometer
|
|
84
88
|
end
|
85
89
|
Data::LocalDateTime.new(year, month, day, hour, min, sec)
|
86
90
|
end
|
87
|
-
|
91
|
+
|
88
92
|
# convert to a Date class
|
89
93
|
#
|
90
94
|
def to_d
|
91
95
|
Date.civil(@year, @month, @day)
|
92
96
|
end
|
93
|
-
|
97
|
+
|
94
98
|
# convert to a DateTime class
|
95
99
|
#
|
96
100
|
def to_dt
|
97
101
|
DateTime.new(@year, @month, @day, @hour, @min, @sec)
|
98
102
|
end
|
99
|
-
|
103
|
+
|
100
104
|
def <=>(other)
|
101
105
|
if other.is_a?(String) || other.is_a?(Time) || other.is_a?(DateTime) || other.is_a?(Date)
|
102
106
|
the_other = Data::LocalDateTime.parse(other)
|
@@ -104,7 +108,7 @@ module Barometer
|
|
104
108
|
the_other = other
|
105
109
|
end
|
106
110
|
raise ArgumentError unless the_other.is_a?(Data::LocalDateTime) || the_other.is_a?(Data::LocalTime)
|
107
|
-
|
111
|
+
|
108
112
|
if ((other.is_a?(String) || other.is_a?(Time) || other.is_a?(DateTime)) &&
|
109
113
|
the_other.is_a?(Data::LocalDateTime)) || other.is_a?(Data::LocalDateTime)
|
110
114
|
# we are counting days + seconds
|
@@ -121,21 +125,21 @@ module Barometer
|
|
121
125
|
return total_seconds <=> the_other.total_seconds
|
122
126
|
end
|
123
127
|
end
|
124
|
-
|
128
|
+
|
125
129
|
def to_s(time=false)
|
126
130
|
datetime = self.to_dt
|
127
131
|
format = (time ? "%Y-%m-%d %I:%M:%S %p" : "%Y-%m-%d")
|
128
132
|
datetime.strftime(format).downcase
|
129
133
|
end
|
130
|
-
|
134
|
+
|
131
135
|
def nil?; @year == 0 && @month == 0 && @day == 0 && super; end
|
132
|
-
|
136
|
+
|
133
137
|
# this assumes all years have 366 days (which only is true for leap years)
|
134
138
|
# but since this is just for comparisons, this will be accurate
|
135
139
|
#
|
136
140
|
def _total_days
|
137
141
|
self.to_d.yday + (@year * 366)
|
138
142
|
end
|
139
|
-
|
143
|
+
|
140
144
|
end
|
141
145
|
end
|
@@ -1,13 +1,13 @@
|
|
1
1
|
module Barometer
|
2
2
|
#
|
3
3
|
# A simple Time class
|
4
|
-
#
|
4
|
+
#
|
5
5
|
# A time class that represents the local time ...
|
6
6
|
# it has no concept of time zone or date
|
7
7
|
#
|
8
8
|
class Data::LocalTime
|
9
9
|
include Comparable
|
10
|
-
|
10
|
+
|
11
11
|
attr_reader :hour, :min, :sec
|
12
12
|
|
13
13
|
def initialize(h=0,m=0,s=0)
|
@@ -16,7 +16,7 @@ module Barometer
|
|
16
16
|
self.sec = s
|
17
17
|
self
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
def hour=(h)
|
21
21
|
raise ArgumentError unless (h.is_a?(Fixnum) || h.nil?)
|
22
22
|
hour_cap = 24
|
@@ -26,7 +26,7 @@ module Barometer
|
|
26
26
|
@hour = h
|
27
27
|
end
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
def min=(m)
|
31
31
|
raise ArgumentError unless (m.is_a?(Fixnum) || m.nil?)
|
32
32
|
minute_cap = 60
|
@@ -38,7 +38,7 @@ module Barometer
|
|
38
38
|
@min = m
|
39
39
|
end
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
def sec=(s)
|
43
43
|
raise ArgumentError unless (s.is_a?(Fixnum) || s.nil?)
|
44
44
|
second_cap = 60
|
@@ -50,7 +50,7 @@ module Barometer
|
|
50
50
|
@sec = s
|
51
51
|
end
|
52
52
|
end
|
53
|
-
|
53
|
+
|
54
54
|
def parse(string)
|
55
55
|
if string.is_a?(Time) || string.is_a?(DateTime)
|
56
56
|
@hour = string.hour
|
@@ -64,21 +64,21 @@ module Barometer
|
|
64
64
|
end
|
65
65
|
self
|
66
66
|
end
|
67
|
-
|
67
|
+
|
68
68
|
def self.parse(string)
|
69
69
|
return string if string.is_a?(Data::LocalTime)
|
70
70
|
local = Data::LocalTime.new
|
71
71
|
local.parse(string)
|
72
72
|
local
|
73
73
|
end
|
74
|
-
|
74
|
+
|
75
75
|
# convert to a Time class
|
76
76
|
#
|
77
77
|
def to_t
|
78
78
|
date = Date.today
|
79
79
|
Time.local(date.year,date.month,date.day,@hour,@min,@sec)
|
80
80
|
end
|
81
|
-
|
81
|
+
|
82
82
|
def total_seconds
|
83
83
|
(@hour * 60 * 60) + (@min * 60) + @sec
|
84
84
|
end
|
@@ -92,7 +92,7 @@ module Barometer
|
|
92
92
|
raise ArgumentError unless the_other.is_a?(Data::LocalTime)
|
93
93
|
total_seconds <=> the_other.total_seconds
|
94
94
|
end
|
95
|
-
|
95
|
+
|
96
96
|
def +(seconds)
|
97
97
|
local_time = Data::LocalTime.new
|
98
98
|
if seconds.is_a?(Fixnum) || seconds.is_a?(Float)
|
@@ -121,14 +121,14 @@ module Barometer
|
|
121
121
|
raise ArgumentError unless the_other.is_a?(Data::LocalTime)
|
122
122
|
(self.total_seconds - the_other.total_seconds).to_i.abs
|
123
123
|
end
|
124
|
-
|
124
|
+
|
125
125
|
def to_s(seconds=false)
|
126
126
|
time = self.to_t
|
127
127
|
format = (seconds ? "%I:%M:%S %p" : "%I:%M %p")
|
128
128
|
time.strftime(format).downcase
|
129
129
|
end
|
130
|
-
|
130
|
+
|
131
131
|
def nil?; @hour == 0 && @min == 0 && @sec == 0; end
|
132
|
-
|
132
|
+
|
133
133
|
end
|
134
134
|
end
|