moonphases 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gem "nokogiri"
@@ -0,0 +1,9 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
6
+
7
+ desc "Run tests"
8
+ task :default => :test
9
+
@@ -0,0 +1,146 @@
1
+ require 'nokogiri'
2
+ require 'open-uri'
3
+ require 'moonphases/moon_data'
4
+
5
+ class MoonPhases
6
+ def initialize
7
+ @documentLog = Array.new
8
+ @documentCache = Hash.new
9
+ end
10
+
11
+ def getDocumentLogItem( item )
12
+ @documentLog[ item ]
13
+ end
14
+
15
+ def getNASAYearBlob( year )
16
+ doc = getNASADoc year
17
+ if !doc.nil?
18
+ return findYearIn doc, year
19
+ end
20
+ nil
21
+ end
22
+
23
+ def getMoonFullness( date )
24
+ pDataPoint = getPreviousDataPoint date
25
+ if pDataPoint.getDate == date
26
+ return pDataPoint.getFullness
27
+ end
28
+
29
+ nDataPoint = getNextDataPoint date
30
+ daysBetweenDataPoints = nDataPoint.getDate.ajd - pDataPoint.getDate.ajd
31
+ daysSincePreviousDataPoint = date.ajd - pDataPoint.getDate.ajd
32
+
33
+ phaseChange = (nDataPoint.getFullness.getPercent - pDataPoint.getFullness.getPercent) * ( daysSincePreviousDataPoint / daysBetweenDataPoints )
34
+ Fullness.new pDataPoint.getFullness.getPercent + phaseChange, phaseChange > 0 ? "+" : "-"
35
+ end
36
+
37
+ def getDocumentLogLength
38
+ @documentLog.length
39
+ end
40
+
41
+ def getNASAData( year )
42
+ MoonData.new separateNASADataLines( year )
43
+ end
44
+
45
+ def getPreviousDataPoint( date )
46
+ yearToCheck = date.year
47
+ while !(yearData = getNASAData yearToCheck ).nil?
48
+ index = yearData.getNumDataPoints-1
49
+
50
+ while index >= 0
51
+ testDate = yearData.getDataPoint index
52
+ if testDate.getDate <= date
53
+ return testDate
54
+ end
55
+ index = index-1
56
+ end
57
+ yearToCheck = yearToCheck-1
58
+ end
59
+ nil
60
+ end
61
+
62
+ def getNextDataPoint( date )
63
+ yearToCheck = date.year
64
+ while !(yearData = getNASAData yearToCheck ).nil?
65
+ index = 0
66
+
67
+ while index < yearData.getNumDataPoints
68
+ testDate = yearData.getDataPoint index
69
+ if( testDate.getDate >= date )
70
+ return testDate
71
+ end
72
+ index = index+1
73
+ end
74
+ yearToCheck = yearToCheck+1
75
+ end
76
+ nil
77
+ end
78
+
79
+ def separateNASADataLines( year )
80
+ dataLines = Array.new
81
+ # Remove blank lines and stupid OSX return characters.
82
+ data = (getNASAYearBlob year).content
83
+ data.scan(/([^\r]*)/).each do |line|
84
+ if line[0].length > 0
85
+ dataLines << line[0]
86
+ end
87
+ end
88
+ #puts data.scan(/^(-?\d+)?(\s*)(([A-Za-z]{3}\s+\d+\s+\d+:\d+\s+[TAHPtpn]?\s*){1,4})(\d\dh\d\dm)?\s*$/).inspect
89
+ #(getNASAYearBlob year).content.scan(/^(-?\d+)?( *)(([A-Za-z]{3} +\d+ +\d+:\d+ +[TAHPtpn]? *){1,4})(\d\dh\d\dm)? *$/).each do |lineMatches|
90
+ # puts lineMatches[2]
91
+ # dataLines << lineMatches[2]
92
+ #end
93
+ dataLines
94
+ end
95
+
96
+ def findYearIn( nasaDoc, year )
97
+ nasaDoc.css( 'pre.indent' ).each do |pre|
98
+ pre.children.each do |child|
99
+ firstNumber = child.content[/-?\d+/]
100
+ if !firstNumber.nil?
101
+ if firstNumber.to_i == year
102
+ return child
103
+ end
104
+ end
105
+ end
106
+ end
107
+ nil
108
+ end
109
+
110
+ def getNASADoc( year )
111
+ url = lookupURL( year )
112
+ document = @documentCache[ url ]
113
+ if document.nil?
114
+ @documentLog << url
115
+ document = Nokogiri::HTML( open( url ))
116
+ @documentCache[ url ] = document
117
+ end
118
+ document
119
+ end
120
+
121
+ def lookupURL( year )
122
+ "http://eclipse.gsfc.nasa.gov/phase/phases" + paddedString( (( year - 1 )/ 100 ) * 100 + 1 ) + ".html"
123
+ end
124
+
125
+ def paddedString( value )
126
+ if value > 0
127
+ if value < 10
128
+ "000" + value.to_s
129
+ elsif value < 100
130
+ "00" + value.to_s
131
+ elsif value < 1000
132
+ "0" + value.to_s
133
+ else
134
+ value.to_s
135
+ end
136
+ else
137
+ if value < -999
138
+ value.to_s
139
+ elsif value < -99
140
+ "-0" + (-value).to_s
141
+ else
142
+ "-00" + (-value).to_s
143
+ end
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,31 @@
1
+ require 'date'
2
+
3
+ class DataPoint
4
+ def initialize( date, fullness )
5
+ @date = date
6
+ @fullness = fullness
7
+ end
8
+
9
+ def getDate
10
+ @date
11
+ end
12
+
13
+ def getFullness
14
+ @fullness
15
+ end
16
+ end
17
+
18
+ class Fullness
19
+ def initialize( percent, direction )
20
+ @percent = percent
21
+ @direction = direction
22
+ end
23
+
24
+ def getPercent
25
+ @percent
26
+ end
27
+
28
+ def getDirection
29
+ @direction
30
+ end
31
+ end
@@ -0,0 +1,88 @@
1
+ require 'date'
2
+ require 'moonphases/data_point'
3
+
4
+ class MoonData
5
+ def initialize( nasaDataLines )
6
+ @dataPoints = Array.new
7
+
8
+ # Read the year first, we'll need it in a minute to make Date objects.
9
+ @year = nasaDataLines[0][/-?\d+/].to_i
10
+ moonFullness = getFirstMoonFullness nasaDataLines[0]
11
+
12
+ # Parse out all the dates
13
+ nasaDataLines.each do |dataLine|
14
+ dataLine.scan( /([A-Za-z]{3}\s+\d+\s+\d+:\d+\s+)/ ).each do |date|
15
+ @dataPoints << DataPoint.new( parseDate( date[0] ), moonFullness )
16
+ moonFullness = nextMoonFullness moonFullness
17
+ end
18
+ end
19
+ end
20
+
21
+ def getFirstMoonFullness dataLine
22
+ case dataLine.scan( /([A-Za-z]{3}\s+\d+\s+\d+:\d+\s+)/ ).length
23
+ when 1
24
+ return Fullness.new 50, "-"
25
+ when 2
26
+ return Fullness.new 100, "-"
27
+ when 3
28
+ return Fullness.new 50, "+"
29
+ when 4
30
+ return Fullness.new 0, "+"
31
+ end
32
+
33
+ end
34
+
35
+ def nextMoonFullness( currentFullness )
36
+ if( currentFullness.getDirection == "+" )
37
+ return Fullness.new currentFullness.getPercent + 50, currentFullness.getPercent == 50 ? "-" : "+"
38
+ else
39
+ return Fullness.new currentFullness.getPercent - 50, currentFullness.getPercent == 50 ? "+" : "-"
40
+ end
41
+ end
42
+
43
+ def getNumDataPoints
44
+ @dataPoints.length
45
+ end
46
+
47
+ def getDataPoint( index )
48
+ @dataPoints[ index ]
49
+ end
50
+
51
+ def getYear
52
+ @year
53
+ end
54
+
55
+ def parseDate( dateString )
56
+ parsed = dateString.scan(/([A-Za-z]{3})\s+(\d+)\s+(\d+:\d+)/)
57
+ Date.new( @year, getMonth( parsed[0][0] ), parsed[0][1].to_i )
58
+ end
59
+
60
+ def getMonth( monthString )
61
+ lowerCase = monthString.downcase
62
+ if "jan".eql? lowerCase
63
+ return 1
64
+ elsif "feb".eql? lowerCase
65
+ return 2
66
+ elsif "mar".eql? lowerCase
67
+ return 3
68
+ elsif "apr".eql? lowerCase
69
+ return 4
70
+ elsif "may".eql? lowerCase
71
+ return 5
72
+ elsif "jun".eql? lowerCase
73
+ return 6
74
+ elsif "jul".eql? lowerCase
75
+ return 7
76
+ elsif "aug".eql? lowerCase
77
+ return 8
78
+ elsif "sep".eql? lowerCase
79
+ return 9
80
+ elsif "oct".eql? lowerCase
81
+ return 10
82
+ elsif "nov".eql? lowerCase
83
+ return 11
84
+ elsif "dec".eql? lowerCase
85
+ return 12
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,426 @@
1
+ require 'test/unit'
2
+ require 'moonphases'
3
+ require 'date'
4
+
5
+ class MoonPhasesTest < Test::Unit::TestCase
6
+
7
+ def test_AD_Years
8
+ if !(defined? @moon)
9
+ @moon = MoonPhases.new
10
+ end
11
+
12
+ assert_equal "http://eclipse.gsfc.nasa.gov/phase/phases0001.html", @moon.lookupURL( 1 )
13
+ assert_equal "http://eclipse.gsfc.nasa.gov/phase/phases0001.html", @moon.lookupURL( 100 )
14
+ assert_equal "http://eclipse.gsfc.nasa.gov/phase/phases0101.html", @moon.lookupURL( 101 )
15
+ assert_equal "http://eclipse.gsfc.nasa.gov/phase/phases0101.html", @moon.lookupURL( 200 )
16
+ assert_equal "http://eclipse.gsfc.nasa.gov/phase/phases0201.html", @moon.lookupURL( 201 )
17
+ assert_equal "http://eclipse.gsfc.nasa.gov/phase/phases0201.html", @moon.lookupURL( 300 )
18
+ assert_equal "http://eclipse.gsfc.nasa.gov/phase/phases0301.html", @moon.lookupURL( 301 )
19
+ assert_equal "http://eclipse.gsfc.nasa.gov/phase/phases0901.html", @moon.lookupURL( 1000 )
20
+ assert_equal "http://eclipse.gsfc.nasa.gov/phase/phases1001.html", @moon.lookupURL( 1001 )
21
+ assert_equal "http://eclipse.gsfc.nasa.gov/phase/phases2001.html", @moon.lookupURL( 2012 )
22
+ end
23
+
24
+ def test_BC_Years
25
+ if !(defined? @moon)
26
+ @moon = MoonPhases.new
27
+ end
28
+
29
+ assert_equal "http://eclipse.gsfc.nasa.gov/phase/phases-0099.html", @moon.lookupURL( 0 )
30
+ assert_equal "http://eclipse.gsfc.nasa.gov/phase/phases-0099.html", @moon.lookupURL( -1 )
31
+ assert_equal "http://eclipse.gsfc.nasa.gov/phase/phases-0099.html", @moon.lookupURL( -99 )
32
+ assert_equal "http://eclipse.gsfc.nasa.gov/phase/phases-0199.html", @moon.lookupURL( -100 )
33
+ assert_equal "http://eclipse.gsfc.nasa.gov/phase/phases-0199.html", @moon.lookupURL( -199 )
34
+ assert_equal "http://eclipse.gsfc.nasa.gov/phase/phases-0299.html", @moon.lookupURL( -200 )
35
+ assert_equal "http://eclipse.gsfc.nasa.gov/phase/phases-0999.html", @moon.lookupURL( -900 )
36
+ assert_equal "http://eclipse.gsfc.nasa.gov/phase/phases-0999.html", @moon.lookupURL( -999 )
37
+ assert_equal "http://eclipse.gsfc.nasa.gov/phase/phases-1099.html", @moon.lookupURL( -1000 )
38
+ assert_equal "http://eclipse.gsfc.nasa.gov/phase/phases-1099.html", @moon.lookupURL( -1099 )
39
+ end
40
+
41
+ def test_padding
42
+ if !(defined? @moon)
43
+ @moon = MoonPhases.new
44
+ end
45
+
46
+ assert_equal "0001", @moon.paddedString( 1 )
47
+ assert_equal "0010", @moon.paddedString( 10 )
48
+ assert_equal "0100", @moon.paddedString( 100 )
49
+ assert_equal "1000", @moon.paddedString( 1000 )
50
+ end
51
+
52
+ def test_get_data
53
+ if !(defined? @moon)
54
+ @moon = MoonPhases.new
55
+ end
56
+
57
+ assert_raise OpenURI::HTTPError do
58
+ @moon.getNASADoc( 50000 )
59
+ end
60
+
61
+ assert_nothing_raised OpenURI::HTTPError do
62
+ doc = @moon.getNASADoc 2012
63
+ assert_equal "NASA - Moon Phases: 2001 to 2100", doc.title
64
+ end
65
+ end
66
+
67
+ def test_find_year_blobs
68
+ if !(defined? @moon)
69
+ @moon = MoonPhases.new
70
+ end
71
+
72
+ doc = @moon.getNASADoc 2012
73
+ assert_not_nil @moon.findYearIn doc, 2012
74
+ assert_nil @moon.findYearIn doc, 1999
75
+
76
+ # Some AD Years
77
+ assert_equal 2015, (@moon.getNASAYearBlob 2015).content[/-?\d+/].to_i
78
+ assert_equal 9, (@moon.getNASAYearBlob 9).content[/-?\d+/].to_i
79
+ assert_equal 567, (@moon.getNASAYearBlob 567).content[/-?\d+/].to_i
80
+ assert_equal 1620, (@moon.getNASAYearBlob 1620).content[/-?\d+/].to_i
81
+
82
+ # OOH! Year 0!
83
+ assert_equal 0, (@moon.getNASAYearBlob 0).content[/-?\d+/].to_i
84
+
85
+ # How about some BC Years?
86
+ assert_equal -1, (@moon.getNASAYearBlob -1).content[/-?\d+/].to_i
87
+ assert_equal -980, (@moon.getNASAYearBlob -980).content[/-?\d+/].to_i
88
+ assert_equal -38, (@moon.getNASAYearBlob -38).content[/-?\d+/].to_i
89
+
90
+ # These are the earliest and latest years for which NASA publishes data.
91
+ assert_equal -1999, (@moon.getNASAYearBlob -1999).content[/-?\d+/].to_i
92
+ assert_equal 4000, (@moon.getNASAYearBlob 4000).content[/-?\d+/].to_i
93
+
94
+ end
95
+
96
+ def test_parse_blob_data
97
+ if !(defined? @moon)
98
+ @moon = MoonPhases.new
99
+ end
100
+
101
+ assert_equal 2002, (@moon.getNASAYearBlob 2002).content[/-?\d+/].to_i
102
+ assert_equal 13, (@moon.separateNASADataLines 2002).length
103
+
104
+ #Now let's see if we're actually parsing good data.
105
+ moonData = @moon.getNASAData( 2002 )
106
+ assert_equal 49, moonData.getNumDataPoints
107
+ assert_equal 2002, moonData.getYear
108
+
109
+ parsedDate = moonData.parseDate( "Jan 12 23:46" )
110
+ assert_equal 2002, parsedDate.year
111
+ assert_equal 1, parsedDate.mon
112
+ assert_equal 12, parsedDate.mday
113
+
114
+ assert_equal 1, moonData.parseDate( "Jan 10 12:03" ).mon
115
+ assert_equal 2, moonData.parseDate( " Feb 8 22:28 " ).mon
116
+ assert_equal 3, moonData.parseDate( " Mar 25 20:58 " ).mon
117
+ assert_equal 4, moonData.parseDate( " Apr 2 00:50 " ).mon
118
+ assert_equal 5, moonData.parseDate( " May 1 06:24 " ).mon
119
+ assert_equal 6, moonData.parseDate( " Jun 15 01:22 " ).mon
120
+ assert_equal 7, moonData.parseDate( " Jul 6 12:03 " ).mon
121
+ assert_equal 8, moonData.parseDate( " Aug 19 17:53" ).mon
122
+ assert_equal 9, moonData.parseDate( " Sep 3 18:45 " ).mon
123
+ assert_equal 10, moonData.parseDate( " Oct 25 01:17 " ).mon
124
+ assert_equal 11, moonData.parseDate( " Nov 2 01:25 " ).mon
125
+ assert_equal 12, moonData.parseDate( "Dec 31 03:12 " ).mon
126
+
127
+ # Let's see if the dates were parsed right for 2002
128
+ assert_equal 1, moonData.getDataPoint( 0 ).getDate.mon
129
+ assert_equal 6, moonData.getDataPoint( 0 ).getDate.mday
130
+ assert_equal 2002, moonData.getDataPoint( 0 ).getDate.year
131
+
132
+ assert_equal 1, moonData.getDataPoint( 1 ).getDate.mon
133
+ assert_equal 13, moonData.getDataPoint( 1 ).getDate.mday
134
+ assert_equal 2002, moonData.getDataPoint( 1 ).getDate.year
135
+
136
+ assert_equal 1, moonData.getDataPoint( 2 ).getDate.mon
137
+ assert_equal 21, moonData.getDataPoint( 2 ).getDate.mday
138
+ assert_equal 2002, moonData.getDataPoint( 2 ).getDate.year
139
+
140
+ assert_equal 1, moonData.getDataPoint( 3 ).getDate.mon
141
+ assert_equal 28, moonData.getDataPoint( 3 ).getDate.mday
142
+ assert_equal 2002, moonData.getDataPoint( 3 ).getDate.year
143
+
144
+ assert_equal 2, moonData.getDataPoint( 4 ).getDate.mon
145
+ assert_equal 4, moonData.getDataPoint( 4 ).getDate.mday
146
+ assert_equal 2002, moonData.getDataPoint( 4 ).getDate.year
147
+
148
+ assert_equal 2, moonData.getDataPoint( 5 ).getDate.mon
149
+ assert_equal 12, moonData.getDataPoint( 5 ).getDate.mday
150
+ assert_equal 2002, moonData.getDataPoint( 5 ).getDate.year
151
+
152
+ assert_equal 2, moonData.getDataPoint( 6 ).getDate.mon
153
+ assert_equal 20, moonData.getDataPoint( 6 ).getDate.mday
154
+ assert_equal 2002, moonData.getDataPoint( 6 ).getDate.year
155
+
156
+ assert_equal 2, moonData.getDataPoint( 7 ).getDate.mon
157
+ assert_equal 27, moonData.getDataPoint( 7 ).getDate.mday
158
+ assert_equal 2002, moonData.getDataPoint( 7 ).getDate.year
159
+
160
+ assert_equal 3, moonData.getDataPoint( 8 ).getDate.mon
161
+ assert_equal 6, moonData.getDataPoint( 8 ).getDate.mday
162
+ assert_equal 2002, moonData.getDataPoint( 8 ).getDate.year
163
+
164
+ assert_equal 3, moonData.getDataPoint( 9 ).getDate.mon
165
+ assert_equal 14, moonData.getDataPoint( 9 ).getDate.mday
166
+ assert_equal 2002, moonData.getDataPoint( 9 ).getDate.year
167
+
168
+ assert_equal 3, moonData.getDataPoint( 10 ).getDate.mon
169
+ assert_equal 22, moonData.getDataPoint( 10 ).getDate.mday
170
+ assert_equal 2002, moonData.getDataPoint( 10 ).getDate.year
171
+
172
+ assert_equal 3, moonData.getDataPoint( 11 ).getDate.mon
173
+ assert_equal 28, moonData.getDataPoint( 11 ).getDate.mday
174
+ assert_equal 2002, moonData.getDataPoint( 11 ).getDate.year
175
+
176
+ assert_equal 4, moonData.getDataPoint( 12 ).getDate.mon
177
+ assert_equal 4, moonData.getDataPoint( 12 ).getDate.mday
178
+ assert_equal 2002, moonData.getDataPoint( 12 ).getDate.year
179
+
180
+ assert_equal 4, moonData.getDataPoint( 13 ).getDate.mon
181
+ assert_equal 12, moonData.getDataPoint( 13 ).getDate.mday
182
+ assert_equal 2002, moonData.getDataPoint( 13 ).getDate.year
183
+
184
+ assert_equal 4, moonData.getDataPoint( 14 ).getDate.mon
185
+ assert_equal 20, moonData.getDataPoint( 14 ).getDate.mday
186
+ assert_equal 2002, moonData.getDataPoint( 14 ).getDate.year
187
+
188
+ assert_equal 4, moonData.getDataPoint( 15 ).getDate.mon
189
+ assert_equal 27, moonData.getDataPoint( 15 ).getDate.mday
190
+ assert_equal 2002, moonData.getDataPoint( 15 ).getDate.year
191
+
192
+ assert_equal 5, moonData.getDataPoint( 16 ).getDate.mon
193
+ assert_equal 4, moonData.getDataPoint( 16 ).getDate.mday
194
+ assert_equal 2002, moonData.getDataPoint( 16 ).getDate.year
195
+
196
+ assert_equal 5, moonData.getDataPoint( 17 ).getDate.mon
197
+ assert_equal 12, moonData.getDataPoint( 17 ).getDate.mday
198
+ assert_equal 2002, moonData.getDataPoint( 17 ).getDate.year
199
+
200
+ assert_equal 5, moonData.getDataPoint( 18 ).getDate.mon
201
+ assert_equal 19, moonData.getDataPoint( 18 ).getDate.mday
202
+ assert_equal 2002, moonData.getDataPoint( 18 ).getDate.year
203
+
204
+ assert_equal 5, moonData.getDataPoint( 19 ).getDate.mon
205
+ assert_equal 26, moonData.getDataPoint( 19 ).getDate.mday
206
+ assert_equal 2002, moonData.getDataPoint( 19 ).getDate.year
207
+
208
+ assert_equal 6, moonData.getDataPoint( 20 ).getDate.mon
209
+ assert_equal 3, moonData.getDataPoint( 20 ).getDate.mday
210
+ assert_equal 2002, moonData.getDataPoint( 20 ).getDate.year
211
+
212
+ assert_equal 6, moonData.getDataPoint( 21 ).getDate.mon
213
+ assert_equal 10, moonData.getDataPoint( 21 ).getDate.mday
214
+ assert_equal 2002, moonData.getDataPoint( 21 ).getDate.year
215
+
216
+ assert_equal 6, moonData.getDataPoint( 22 ).getDate.mon
217
+ assert_equal 18, moonData.getDataPoint( 22 ).getDate.mday
218
+ assert_equal 2002, moonData.getDataPoint( 22 ).getDate.year
219
+
220
+ assert_equal 6, moonData.getDataPoint( 23 ).getDate.mon
221
+ assert_equal 24, moonData.getDataPoint( 23 ).getDate.mday
222
+ assert_equal 2002, moonData.getDataPoint( 23 ).getDate.year
223
+
224
+ assert_equal 7, moonData.getDataPoint( 24 ).getDate.mon
225
+ assert_equal 2, moonData.getDataPoint( 24 ).getDate.mday
226
+ assert_equal 2002, moonData.getDataPoint( 24 ).getDate.year
227
+
228
+ assert_equal 7, moonData.getDataPoint( 25 ).getDate.mon
229
+ assert_equal 10, moonData.getDataPoint( 25 ).getDate.mday
230
+ assert_equal 2002, moonData.getDataPoint( 25 ).getDate.year
231
+
232
+ assert_equal 7, moonData.getDataPoint( 26 ).getDate.mon
233
+ assert_equal 17, moonData.getDataPoint( 26 ).getDate.mday
234
+ assert_equal 2002, moonData.getDataPoint( 26 ).getDate.year
235
+
236
+ assert_equal 7, moonData.getDataPoint( 27 ).getDate.mon
237
+ assert_equal 24, moonData.getDataPoint( 27 ).getDate.mday
238
+ assert_equal 2002, moonData.getDataPoint( 27 ).getDate.year
239
+
240
+ assert_equal 8, moonData.getDataPoint( 28 ).getDate.mon
241
+ assert_equal 1, moonData.getDataPoint( 28 ).getDate.mday
242
+ assert_equal 2002, moonData.getDataPoint( 28 ).getDate.year
243
+
244
+ assert_equal 8, moonData.getDataPoint( 29 ).getDate.mon
245
+ assert_equal 8, moonData.getDataPoint( 29 ).getDate.mday
246
+ assert_equal 2002, moonData.getDataPoint( 29 ).getDate.year
247
+
248
+ assert_equal 8, moonData.getDataPoint( 30 ).getDate.mon
249
+ assert_equal 15, moonData.getDataPoint( 30 ).getDate.mday
250
+ assert_equal 2002, moonData.getDataPoint( 30 ).getDate.year
251
+
252
+ assert_equal 8, moonData.getDataPoint( 31 ).getDate.mon
253
+ assert_equal 22, moonData.getDataPoint( 31 ).getDate.mday
254
+ assert_equal 2002, moonData.getDataPoint( 31 ).getDate.year
255
+
256
+ assert_equal 8, moonData.getDataPoint( 32 ).getDate.mon
257
+ assert_equal 31, moonData.getDataPoint( 32 ).getDate.mday
258
+ assert_equal 2002, moonData.getDataPoint( 32 ).getDate.year
259
+
260
+ assert_equal 9, moonData.getDataPoint( 33 ).getDate.mon
261
+ assert_equal 7, moonData.getDataPoint( 33 ).getDate.mday
262
+ assert_equal 2002, moonData.getDataPoint( 33 ).getDate.year
263
+
264
+ assert_equal 9, moonData.getDataPoint( 34 ).getDate.mon
265
+ assert_equal 13, moonData.getDataPoint( 34 ).getDate.mday
266
+ assert_equal 2002, moonData.getDataPoint( 34 ).getDate.year
267
+
268
+ assert_equal 9, moonData.getDataPoint( 35 ).getDate.mon
269
+ assert_equal 21, moonData.getDataPoint( 35 ).getDate.mday
270
+ assert_equal 2002, moonData.getDataPoint( 35 ).getDate.year
271
+
272
+ assert_equal 9, moonData.getDataPoint( 36 ).getDate.mon
273
+ assert_equal 29, moonData.getDataPoint( 36 ).getDate.mday
274
+ assert_equal 2002, moonData.getDataPoint( 36 ).getDate.year
275
+
276
+ assert_equal 10, moonData.getDataPoint( 37 ).getDate.mon
277
+ assert_equal 6, moonData.getDataPoint( 37 ).getDate.mday
278
+ assert_equal 2002, moonData.getDataPoint( 37 ).getDate.year
279
+
280
+ assert_equal 10, moonData.getDataPoint( 38 ).getDate.mon
281
+ assert_equal 13, moonData.getDataPoint( 38 ).getDate.mday
282
+ assert_equal 2002, moonData.getDataPoint( 38 ).getDate.year
283
+
284
+ assert_equal 10, moonData.getDataPoint( 39 ).getDate.mon
285
+ assert_equal 21, moonData.getDataPoint( 39 ).getDate.mday
286
+ assert_equal 2002, moonData.getDataPoint( 39 ).getDate.year
287
+
288
+ assert_equal 10, moonData.getDataPoint( 40 ).getDate.mon
289
+ assert_equal 29, moonData.getDataPoint( 40 ).getDate.mday
290
+ assert_equal 2002, moonData.getDataPoint( 40 ).getDate.year
291
+
292
+ assert_equal 11, moonData.getDataPoint( 41 ).getDate.mon
293
+ assert_equal 4, moonData.getDataPoint( 41 ).getDate.mday
294
+ assert_equal 2002, moonData.getDataPoint( 41 ).getDate.year
295
+
296
+ assert_equal 11, moonData.getDataPoint( 42 ).getDate.mon
297
+ assert_equal 11, moonData.getDataPoint( 42 ).getDate.mday
298
+ assert_equal 2002, moonData.getDataPoint( 42 ).getDate.year
299
+
300
+ assert_equal 11, moonData.getDataPoint( 43 ).getDate.mon
301
+ assert_equal 20, moonData.getDataPoint( 43 ).getDate.mday
302
+ assert_equal 2002, moonData.getDataPoint( 43 ).getDate.year
303
+
304
+ assert_equal 11, moonData.getDataPoint( 44 ).getDate.mon
305
+ assert_equal 27, moonData.getDataPoint( 44 ).getDate.mday
306
+ assert_equal 2002, moonData.getDataPoint( 44 ).getDate.year
307
+
308
+ assert_equal 12, moonData.getDataPoint( 45 ).getDate.mon
309
+ assert_equal 4, moonData.getDataPoint( 45 ).getDate.mday
310
+ assert_equal 2002, moonData.getDataPoint( 45 ).getDate.year
311
+
312
+ assert_equal 12, moonData.getDataPoint( 46 ).getDate.mon
313
+ assert_equal 11, moonData.getDataPoint( 46 ).getDate.mday
314
+ assert_equal 2002, moonData.getDataPoint( 46 ).getDate.year
315
+
316
+ assert_equal 12, moonData.getDataPoint( 47 ).getDate.mon
317
+ assert_equal 19, moonData.getDataPoint( 47 ).getDate.mday
318
+ assert_equal 2002, moonData.getDataPoint( 47 ).getDate.year
319
+
320
+ assert_equal 12, moonData.getDataPoint( 48 ).getDate.mon
321
+ assert_equal 27, moonData.getDataPoint( 48 ).getDate.mday
322
+ assert_equal 2002, moonData.getDataPoint( 48 ).getDate.year
323
+
324
+ end
325
+
326
+ def test_find_surrounding_data
327
+ if !(defined? @moon)
328
+ @moon = MoonPhases.new
329
+ end
330
+
331
+ testDate = Date.new 2011, 5, 5
332
+
333
+ dateBefore = (@moon.getPreviousDataPoint testDate).getDate
334
+ assert_equal 5, dateBefore.mon
335
+ assert_equal 3, dateBefore.mday
336
+ assert_equal 2011, dateBefore.year
337
+
338
+ dateAfter = (@moon.getNextDataPoint testDate).getDate
339
+ assert_equal 5, dateAfter.mon
340
+ assert_equal 10, dateAfter.mday
341
+ assert_equal 2011, dateAfter.year
342
+
343
+ testDate = Date.new 2001, 1, 1
344
+ dateBefore = (@moon.getPreviousDataPoint testDate).getDate
345
+ assert_equal 12, dateBefore.mon
346
+ assert_equal 25, dateBefore.mday
347
+ assert_equal 2000, dateBefore.year
348
+
349
+ testDate = Date.new 2000, 12, 26
350
+ dateAfter = (@moon.getNextDataPoint testDate).getDate
351
+ assert_equal 1, dateAfter.mon
352
+ assert_equal 2, dateAfter.mday
353
+ assert_equal 2001, dateAfter.year
354
+
355
+ testDate = Date.new 2000, 12, 25
356
+ assert_equal @moon.getPreviousDataPoint( testDate ).getDate, @moon.getNextDataPoint( testDate ).getDate
357
+ end
358
+
359
+ def test_get_moon_phase
360
+ if !(defined? @moon)
361
+ @moon = MoonPhases.new
362
+ end
363
+
364
+ assert_equal 50, @moon.getMoonFullness( Date.new 2005, 1, 3 ).getPercent
365
+ assert_equal "-", @moon.getMoonFullness( Date.new 2005, 1, 3 ).getDirection
366
+ assert_equal 0, @moon.getMoonFullness( Date.new 2005, 5, 8 ).getPercent
367
+ assert_equal 50, @moon.getMoonFullness( Date.new 2005, 5, 16 ).getPercent
368
+ assert_equal 100, @moon.getMoonFullness( Date.new 2005, 8, 19 ).getPercent
369
+
370
+ testFullness = @moon.getMoonFullness( Date.new 2005, 6, 10 )
371
+ assert testFullness.getPercent > 0 && testFullness.getPercent < 50
372
+ assert @moon.getMoonFullness( Date.new 2005, 6, 11 ).getPercent > @moon.getMoonFullness( Date.new 2005, 6, 9 ).getPercent
373
+ assert @moon.getMoonFullness( Date.new 2005, 6, 10 ).getDirection == "+"
374
+ assert @moon.getMoonFullness( Date.new 2005, 6, 25 ).getDirection == "-"
375
+
376
+ testFullness = @moon.getMoonFullness( Date.new 2005, 6, 25 )
377
+ assert testFullness.getPercent < 100 && testFullness.getPercent > 50
378
+
379
+ testFullness = @moon.getMoonFullness( Date.new 2004, 5, 12 )
380
+ assert testFullness.getPercent < 50 && testFullness.getPercent > 0
381
+ assert testFullness.getDirection == "-"
382
+
383
+ fullnesses = Array.new
384
+ for mday in 1..31 do
385
+ fullnesses << @moon.getMoonFullness( Date.new 2003, 1, mday )
386
+ end
387
+
388
+ assert fullnesses[1].getPercent < fullnesses[0].getPercent
389
+ for mday in 2..17 do
390
+ assert fullnesses[mday].getPercent > fullnesses[mday-1].getPercent
391
+ end
392
+ for mday in 18..30 do
393
+ assert fullnesses[mday].getPercent < fullnesses[mday-1].getPercent
394
+ end
395
+
396
+ assert fullnesses[2].getPercent - fullnesses[1].getPercent == fullnesses[9].getPercent - fullnesses[8].getPercent
397
+ end
398
+
399
+
400
+ def test_document_log
401
+ moon = MoonPhases.new
402
+
403
+ # Get a document, and make sure an item is added to the log.
404
+ assert_not_nil moon.getNASAYearBlob 1977
405
+ assert_equal 1, moon.getDocumentLogLength
406
+
407
+ # Get another doc, and make sure another item is added to the log.
408
+ moon.getNASAYearBlob 2011
409
+ assert_not_nil assert_equal 2, moon.getDocumentLogLength
410
+
411
+ #Make sure that the log contains what we think it should.
412
+ assert_equal moon.lookupURL( 2011 ), moon.getDocumentLogItem( 1 )
413
+ assert_equal moon.lookupURL( 1977 ), moon.getDocumentLogItem( 0 )
414
+
415
+ # Get a blob we've gotten before, and make sure that
416
+ # the log DOESN'T grow (because we went to the cache).
417
+ assert_not_nil moon.getNASAYearBlob 1977
418
+ assert_equal 2, moon.getDocumentLogLength
419
+
420
+ # Get a blob from the year after that one (1977 & 1978)
421
+ # should be in the same NASA page, and make sure that the
422
+ # log doesn't grow.
423
+ assert_not_nil moon.getNASAYearBlob 1978
424
+ assert_equal 2, moon.getDocumentLogLength
425
+ end
426
+ end
metadata CHANGED
@@ -1,8 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moonphases
3
3
  version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 1.0.0
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 1
9
+ version: 1.0.1
6
10
  platform: ruby
7
11
  authors:
8
12
  - Curtis Lacy
@@ -10,16 +14,18 @@ autorequire:
10
14
  bindir: bin
11
15
  cert_chain: []
12
16
 
13
- date: 2012-04-22 00:00:00 Z
17
+ date: 2012-04-21 00:00:00 -04:00
18
+ default_executable:
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: nokogiri
17
22
  prerelease: false
18
23
  requirement: &id001 !ruby/object:Gem::Requirement
19
- none: false
20
24
  requirements:
21
25
  - - ">="
22
26
  - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
23
29
  version: "0"
24
30
  type: :runtime
25
31
  version_requirements: *id001
@@ -31,8 +37,14 @@ extensions: []
31
37
 
32
38
  extra_rdoc_files: []
33
39
 
34
- files: []
35
-
40
+ files:
41
+ - lib/moonphases/data_point.rb
42
+ - lib/moonphases/moon_data.rb
43
+ - lib/moonphases.rb
44
+ - test/test_moonphases.rb
45
+ - Rakefile
46
+ - Gemfile
47
+ has_rdoc: true
36
48
  homepage:
37
49
  licenses: []
38
50
 
@@ -42,23 +54,25 @@ rdoc_options: []
42
54
  require_paths:
43
55
  - lib
44
56
  required_ruby_version: !ruby/object:Gem::Requirement
45
- none: false
46
57
  requirements:
47
58
  - - ">="
48
59
  - !ruby/object:Gem::Version
60
+ segments:
61
+ - 0
49
62
  version: "0"
50
63
  required_rubygems_version: !ruby/object:Gem::Requirement
51
- none: false
52
64
  requirements:
53
65
  - - ">="
54
66
  - !ruby/object:Gem::Version
67
+ segments:
68
+ - 0
55
69
  version: "0"
56
70
  requirements: []
57
71
 
58
72
  rubyforge_project:
59
- rubygems_version: 1.8.22
73
+ rubygems_version: 1.3.6
60
74
  signing_key:
61
75
  specification_version: 3
62
- summary: MoonPhases-1.0.0
76
+ summary: MoonPhases-1.0.1
63
77
  test_files: []
64
78