Class: MoonData

Inherits:
Object
  • Object
show all
Defined in:
lib/moonphases/moon_data.rb

Instance Method Summary collapse

Constructor Details

#initialize(nasaDataLines) ⇒ MoonData

Returns a new instance of MoonData.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/moonphases/moon_data.rb', line 5

def initialize( nasaDataLines )
  @dataPoints = Array.new

  # Read the year first, we'll need it in a minute to make Date objects.    
  @year = nasaDataLines[0][/-?\d+/].to_i
  moonFullness = getFirstMoonFullness nasaDataLines[0]
  
  # Parse out all the dates
  nasaDataLines.each do |dataLine|
    dataLine.scan( /([A-Za-z]{3}\s+\d+\s+\d+:\d+\s+)/ ).each do |date|
      @dataPoints << DataPoint.new( parseDate( date[0] ), moonFullness )
      moonFullness = nextMoonFullness moonFullness
    end
  end
end

Instance Method Details

#getDataPoint(index) ⇒ Object



47
48
49
# File 'lib/moonphases/moon_data.rb', line 47

def getDataPoint( index )
  @dataPoints[ index ]
end

#getFirstMoonFullness(dataLine) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/moonphases/moon_data.rb', line 21

def getFirstMoonFullness dataLine
  case dataLine.scan( /([A-Za-z]{3}\s+\d+\s+\d+:\d+\s+)/ ).length
  when 1
    return Fullness.new 50, "-"
  when 2
    return Fullness.new 100, "-"
  when 3
    return Fullness.new 50, "+"
  when 4
    return Fullness.new 0, "+"
  end
  
end

#getMonth(monthString) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/moonphases/moon_data.rb', line 60

def getMonth( monthString )
  lowerCase = monthString.downcase
  if "jan".eql? lowerCase
    return 1
  elsif "feb".eql? lowerCase
    return 2
  elsif "mar".eql? lowerCase
    return 3
  elsif "apr".eql? lowerCase
    return 4
  elsif "may".eql? lowerCase
    return 5
  elsif "jun".eql? lowerCase
    return 6
  elsif "jul".eql? lowerCase
    return 7
  elsif "aug".eql? lowerCase
    return 8
  elsif "sep".eql? lowerCase
    return 9
  elsif "oct".eql? lowerCase
    return 10
  elsif "nov".eql? lowerCase
    return 11
  elsif "dec".eql? lowerCase
    return 12
  end
end

#getNumDataPointsObject



43
44
45
# File 'lib/moonphases/moon_data.rb', line 43

def getNumDataPoints
  @dataPoints.length
end

#getYearObject



51
52
53
# File 'lib/moonphases/moon_data.rb', line 51

def getYear
  @year
end

#nextMoonFullness(currentFullness) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/moonphases/moon_data.rb', line 35

def nextMoonFullness( currentFullness )
  if( currentFullness.getDirection == "+" )
    return Fullness.new currentFullness.getPercent + 50, currentFullness.getPercent == 50 ? "-" : "+"
  else
    return Fullness.new currentFullness.getPercent - 50, currentFullness.getPercent == 50 ? "+" : "-"
  end
end

#parseDate(dateString) ⇒ Object



55
56
57
58
# File 'lib/moonphases/moon_data.rb', line 55

def parseDate( dateString )
  parsed = dateString.scan(/([A-Za-z]{3})\s+(\d+)\s+(\d+:\d+)/)
  Date.new( @year, getMonth( parsed[0][0] ), parsed[0][1].to_i )
end