moon_phase_tracker 1.3.2 → 2.0.0
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +33 -0
- data/README.md +501 -98
- data/Rakefile +8 -8
- data/context7.json +4 -0
- data/examples/eight_phases_example.rb +100 -100
- data/examples/usage_example.rb +74 -74
- data/lib/moon_phase_tracker/client.rb +7 -5
- data/lib/moon_phase_tracker/lunar_calculator.rb +96 -0
- data/lib/moon_phase_tracker/phase/formatter.rb +4 -1
- data/lib/moon_phase_tracker/phase/parser.rb +7 -2
- data/lib/moon_phase_tracker/phase.rb +22 -4
- data/lib/moon_phase_tracker/phase_calculator/cycle_estimator.rb +1 -1
- data/lib/moon_phase_tracker/phase_calculator/phase_interpolator.rb +1 -1
- data/lib/moon_phase_tracker/tracker.rb +18 -0
- data/lib/moon_phase_tracker/version.rb +1 -1
- data/lib/moon_phase_tracker.rb +17 -0
- metadata +5 -3
data/Rakefile
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require 'bundler/gem_tasks'
|
|
4
|
-
require 'rspec/core/rake_task'
|
|
5
|
-
|
|
6
|
-
RSpec::Core::RakeTask.new(:spec)
|
|
7
|
-
|
|
8
|
-
task default: :spec
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'bundler/gem_tasks'
|
|
4
|
+
require 'rspec/core/rake_task'
|
|
5
|
+
|
|
6
|
+
RSpec::Core::RakeTask.new(:spec)
|
|
7
|
+
|
|
8
|
+
task default: :spec
|
data/context7.json
ADDED
|
@@ -1,100 +1,100 @@
|
|
|
1
|
-
#!/usr/bin/env ruby
|
|
2
|
-
# frozen_string_literal: true
|
|
3
|
-
|
|
4
|
-
# Add the lib directory to the load path
|
|
5
|
-
$LOAD_PATH.unshift File.expand_path('../lib', __dir__)
|
|
6
|
-
|
|
7
|
-
require 'moon_phase_tracker'
|
|
8
|
-
|
|
9
|
-
puts '=== Moon Phase Tracker - 8 Phases Example ==='
|
|
10
|
-
puts 'Demonstrating all 8 lunar phases (4 major + 4 intermediate)'
|
|
11
|
-
puts ''
|
|
12
|
-
|
|
13
|
-
begin
|
|
14
|
-
# Example 1: Get all 8 phases for August 2025
|
|
15
|
-
puts '🌙 All phases for August 2025:'
|
|
16
|
-
puts '=' * 40
|
|
17
|
-
phases = MoonPhaseTracker.all_phases_for_month(2025, 8)
|
|
18
|
-
|
|
19
|
-
if phases.any?
|
|
20
|
-
phases.each do |phase|
|
|
21
|
-
prefix = phase.interpolated ? '~ ' : ' '
|
|
22
|
-
puts "#{prefix}#{phase}"
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
major_count = phases.count { |p| !p.interpolated }
|
|
26
|
-
interpolated_count = phases.count(&:interpolated)
|
|
27
|
-
|
|
28
|
-
puts ''
|
|
29
|
-
puts "Total: #{phases.size} phases (#{major_count} major, #{interpolated_count} interpolated)"
|
|
30
|
-
puts '~ indicates interpolated phases'
|
|
31
|
-
else
|
|
32
|
-
puts 'No phases found for this period.'
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
puts ''
|
|
36
|
-
puts '-' * 50
|
|
37
|
-
puts ''
|
|
38
|
-
|
|
39
|
-
# Example 2: Compare 4 vs 8 phases
|
|
40
|
-
puts '🔍 Comparison: 4 Major Phases vs 8 Complete Phases'
|
|
41
|
-
puts '=' * 55
|
|
42
|
-
|
|
43
|
-
major_phases = MoonPhaseTracker.phases_for_month(2025, 8)
|
|
44
|
-
all_phases = MoonPhaseTracker.all_phases_for_month(2025, 8)
|
|
45
|
-
|
|
46
|
-
puts "Major phases only (#{major_phases.size}):"
|
|
47
|
-
major_phases.each { |phase| puts " #{phase}" }
|
|
48
|
-
|
|
49
|
-
puts ''
|
|
50
|
-
puts "All phases including interpolated (#{all_phases.size}):"
|
|
51
|
-
all_phases.each do |phase|
|
|
52
|
-
prefix = phase.interpolated ? '~ ' : ' '
|
|
53
|
-
puts "#{prefix}#{phase}"
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
puts ''
|
|
57
|
-
puts '-' * 50
|
|
58
|
-
puts ''
|
|
59
|
-
|
|
60
|
-
# Example 3: Show phase symbols
|
|
61
|
-
puts '🎭 Phase Symbols Reference'
|
|
62
|
-
puts '=' * 30
|
|
63
|
-
MoonPhaseTracker::Phase::PHASE_SYMBOLS.each do |type, symbol|
|
|
64
|
-
name = type.to_s.split('_').map(&:capitalize).join(' ')
|
|
65
|
-
puts "#{symbol} #{name}"
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
puts ''
|
|
69
|
-
puts '-' * 50
|
|
70
|
-
puts ''
|
|
71
|
-
|
|
72
|
-
# Example 4: Get phases from a specific date with 8 phases
|
|
73
|
-
puts '📅 8 Phases from a specific date (2025-08-01, 2 cycles)'
|
|
74
|
-
puts '=' * 60
|
|
75
|
-
date_phases = MoonPhaseTracker.all_phases_from_date('2025-08-01', 2)
|
|
76
|
-
|
|
77
|
-
if date_phases.any?
|
|
78
|
-
date_phases.each do |phase|
|
|
79
|
-
prefix = phase.interpolated ? '~ ' : ' '
|
|
80
|
-
puts "#{prefix}#{phase}"
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
major_count = date_phases.count { |p| !p.interpolated }
|
|
84
|
-
interpolated_count = date_phases.count(&:interpolated)
|
|
85
|
-
|
|
86
|
-
puts ''
|
|
87
|
-
puts "Total: #{date_phases.size} phases over 2 lunar cycles"
|
|
88
|
-
puts "(#{major_count} major, #{interpolated_count} interpolated)"
|
|
89
|
-
end
|
|
90
|
-
rescue MoonPhaseTracker::Error => e
|
|
91
|
-
puts "Error: #{e.message}"
|
|
92
|
-
rescue StandardError => e
|
|
93
|
-
puts "Unexpected error: #{e.message}"
|
|
94
|
-
puts 'This example requires an active internet connection to fetch moon phase data.'
|
|
95
|
-
end
|
|
96
|
-
|
|
97
|
-
puts ''
|
|
98
|
-
puts 'Note: This example uses real astronomical data from the US Naval Observatory.'
|
|
99
|
-
puts 'Interpolated phases (~) are calculated estimates between official phases.'
|
|
100
|
-
puts 'All dates are in ISO 8601 format (YYYY-MM-DD) and times are in UTC.'
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
# Add the lib directory to the load path
|
|
5
|
+
$LOAD_PATH.unshift File.expand_path('../lib', __dir__)
|
|
6
|
+
|
|
7
|
+
require 'moon_phase_tracker'
|
|
8
|
+
|
|
9
|
+
puts '=== Moon Phase Tracker - 8 Phases Example ==='
|
|
10
|
+
puts 'Demonstrating all 8 lunar phases (4 major + 4 intermediate)'
|
|
11
|
+
puts ''
|
|
12
|
+
|
|
13
|
+
begin
|
|
14
|
+
# Example 1: Get all 8 phases for August 2025
|
|
15
|
+
puts '🌙 All phases for August 2025:'
|
|
16
|
+
puts '=' * 40
|
|
17
|
+
phases = MoonPhaseTracker.all_phases_for_month(2025, 8)
|
|
18
|
+
|
|
19
|
+
if phases.any?
|
|
20
|
+
phases.each do |phase|
|
|
21
|
+
prefix = phase.interpolated ? '~ ' : ' '
|
|
22
|
+
puts "#{prefix}#{phase}"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
major_count = phases.count { |p| !p.interpolated }
|
|
26
|
+
interpolated_count = phases.count(&:interpolated)
|
|
27
|
+
|
|
28
|
+
puts ''
|
|
29
|
+
puts "Total: #{phases.size} phases (#{major_count} major, #{interpolated_count} interpolated)"
|
|
30
|
+
puts '~ indicates interpolated phases'
|
|
31
|
+
else
|
|
32
|
+
puts 'No phases found for this period.'
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
puts ''
|
|
36
|
+
puts '-' * 50
|
|
37
|
+
puts ''
|
|
38
|
+
|
|
39
|
+
# Example 2: Compare 4 vs 8 phases
|
|
40
|
+
puts '🔍 Comparison: 4 Major Phases vs 8 Complete Phases'
|
|
41
|
+
puts '=' * 55
|
|
42
|
+
|
|
43
|
+
major_phases = MoonPhaseTracker.phases_for_month(2025, 8)
|
|
44
|
+
all_phases = MoonPhaseTracker.all_phases_for_month(2025, 8)
|
|
45
|
+
|
|
46
|
+
puts "Major phases only (#{major_phases.size}):"
|
|
47
|
+
major_phases.each { |phase| puts " #{phase}" }
|
|
48
|
+
|
|
49
|
+
puts ''
|
|
50
|
+
puts "All phases including interpolated (#{all_phases.size}):"
|
|
51
|
+
all_phases.each do |phase|
|
|
52
|
+
prefix = phase.interpolated ? '~ ' : ' '
|
|
53
|
+
puts "#{prefix}#{phase}"
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
puts ''
|
|
57
|
+
puts '-' * 50
|
|
58
|
+
puts ''
|
|
59
|
+
|
|
60
|
+
# Example 3: Show phase symbols
|
|
61
|
+
puts '🎭 Phase Symbols Reference'
|
|
62
|
+
puts '=' * 30
|
|
63
|
+
MoonPhaseTracker::Phase::PHASE_SYMBOLS.each do |type, symbol|
|
|
64
|
+
name = type.to_s.split('_').map(&:capitalize).join(' ')
|
|
65
|
+
puts "#{symbol} #{name}"
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
puts ''
|
|
69
|
+
puts '-' * 50
|
|
70
|
+
puts ''
|
|
71
|
+
|
|
72
|
+
# Example 4: Get phases from a specific date with 8 phases
|
|
73
|
+
puts '📅 8 Phases from a specific date (2025-08-01, 2 cycles)'
|
|
74
|
+
puts '=' * 60
|
|
75
|
+
date_phases = MoonPhaseTracker.all_phases_from_date('2025-08-01', 2)
|
|
76
|
+
|
|
77
|
+
if date_phases.any?
|
|
78
|
+
date_phases.each do |phase|
|
|
79
|
+
prefix = phase.interpolated ? '~ ' : ' '
|
|
80
|
+
puts "#{prefix}#{phase}"
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
major_count = date_phases.count { |p| !p.interpolated }
|
|
84
|
+
interpolated_count = date_phases.count(&:interpolated)
|
|
85
|
+
|
|
86
|
+
puts ''
|
|
87
|
+
puts "Total: #{date_phases.size} phases over 2 lunar cycles"
|
|
88
|
+
puts "(#{major_count} major, #{interpolated_count} interpolated)"
|
|
89
|
+
end
|
|
90
|
+
rescue MoonPhaseTracker::Error => e
|
|
91
|
+
puts "Error: #{e.message}"
|
|
92
|
+
rescue StandardError => e
|
|
93
|
+
puts "Unexpected error: #{e.message}"
|
|
94
|
+
puts 'This example requires an active internet connection to fetch moon phase data.'
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
puts ''
|
|
98
|
+
puts 'Note: This example uses real astronomical data from the US Naval Observatory.'
|
|
99
|
+
puts 'Interpolated phases (~) are calculated estimates between official phases.'
|
|
100
|
+
puts 'All dates are in ISO 8601 format (YYYY-MM-DD) and times are in UTC.'
|
data/examples/usage_example.rb
CHANGED
|
@@ -1,74 +1,74 @@
|
|
|
1
|
-
#!/usr/bin/env ruby
|
|
2
|
-
# frozen_string_literal: true
|
|
3
|
-
|
|
4
|
-
require_relative '../lib/moon_phase_tracker'
|
|
5
|
-
|
|
6
|
-
puts '=== Moon Phase Tracker - Usage Examples =='
|
|
7
|
-
puts
|
|
8
|
-
|
|
9
|
-
tracker = MoonPhaseTracker::Tracker.new
|
|
10
|
-
|
|
11
|
-
begin
|
|
12
|
-
puts '1. Moon phases for August 2025:'
|
|
13
|
-
puts '-' * 40
|
|
14
|
-
august_phases = MoonPhaseTracker.phases_for_month(2025, 8)
|
|
15
|
-
puts tracker.format_phases(august_phases, "#{MoonPhaseTracker::Tracker.month_name(8)} 2025 Phases")
|
|
16
|
-
puts
|
|
17
|
-
|
|
18
|
-
puts '2. All moon phases in 2025 (first 8):'
|
|
19
|
-
puts '-' * 50
|
|
20
|
-
year_phases = MoonPhaseTracker.phases_for_year(2025)
|
|
21
|
-
puts tracker.format_phases(year_phases.first(8), 'First 8 phases of 2025')
|
|
22
|
-
puts
|
|
23
|
-
|
|
24
|
-
puts '3. Next 6 phases starting from 2025-08-01:'
|
|
25
|
-
puts '-' * 45
|
|
26
|
-
future_phases = MoonPhaseTracker.phases_from_date('2025-08-01', 6)
|
|
27
|
-
puts tracker.format_phases(future_phases, 'Upcoming phases')
|
|
28
|
-
puts
|
|
29
|
-
|
|
30
|
-
puts '4. Current month phases:'
|
|
31
|
-
puts '-' * 25
|
|
32
|
-
current_phases = tracker.current_month_phases
|
|
33
|
-
if current_phases.any?
|
|
34
|
-
puts tracker.format_phases(current_phases, 'Current month phases')
|
|
35
|
-
else
|
|
36
|
-
puts 'No phases found for the current month.'
|
|
37
|
-
end
|
|
38
|
-
puts
|
|
39
|
-
|
|
40
|
-
puts '5. Next moon phase:'
|
|
41
|
-
puts '-' * 25
|
|
42
|
-
next_phase = tracker.next_phase
|
|
43
|
-
if next_phase
|
|
44
|
-
puts next_phase
|
|
45
|
-
puts "Details: #{next_phase.to_h}"
|
|
46
|
-
else
|
|
47
|
-
puts 'Could not retrieve the next phase.'
|
|
48
|
-
end
|
|
49
|
-
puts
|
|
50
|
-
|
|
51
|
-
puts '6. Different phase representations:'
|
|
52
|
-
puts '-' * 42
|
|
53
|
-
if august_phases.any?
|
|
54
|
-
phase = august_phases.first
|
|
55
|
-
puts "String: #{phase}"
|
|
56
|
-
puts "Hash: #{phase.to_h}"
|
|
57
|
-
puts "Symbol: #{phase.symbol}"
|
|
58
|
-
puts "Formatted date: #{phase.formatted_date}"
|
|
59
|
-
puts "Formatted time: #{phase.formatted_time}"
|
|
60
|
-
end
|
|
61
|
-
rescue MoonPhaseTracker::NetworkError => e
|
|
62
|
-
puts "Network error: #{e.message}"
|
|
63
|
-
puts 'Please check your internet connection.'
|
|
64
|
-
rescue MoonPhaseTracker::APIError => e
|
|
65
|
-
puts "API error: #{e.message}"
|
|
66
|
-
puts 'The service may be temporarily unavailable.'
|
|
67
|
-
rescue MoonPhaseTracker::InvalidDateError => e
|
|
68
|
-
puts "Date error: #{e.message}"
|
|
69
|
-
rescue StandardError => e
|
|
70
|
-
puts "Unexpected error: #{e.message}"
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
puts
|
|
74
|
-
puts '=== End of Examples ==='
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require_relative '../lib/moon_phase_tracker'
|
|
5
|
+
|
|
6
|
+
puts '=== Moon Phase Tracker - Usage Examples =='
|
|
7
|
+
puts
|
|
8
|
+
|
|
9
|
+
tracker = MoonPhaseTracker::Tracker.new
|
|
10
|
+
|
|
11
|
+
begin
|
|
12
|
+
puts '1. Moon phases for August 2025:'
|
|
13
|
+
puts '-' * 40
|
|
14
|
+
august_phases = MoonPhaseTracker.phases_for_month(2025, 8)
|
|
15
|
+
puts tracker.format_phases(august_phases, "#{MoonPhaseTracker::Tracker.month_name(8)} 2025 Phases")
|
|
16
|
+
puts
|
|
17
|
+
|
|
18
|
+
puts '2. All moon phases in 2025 (first 8):'
|
|
19
|
+
puts '-' * 50
|
|
20
|
+
year_phases = MoonPhaseTracker.phases_for_year(2025)
|
|
21
|
+
puts tracker.format_phases(year_phases.first(8), 'First 8 phases of 2025')
|
|
22
|
+
puts
|
|
23
|
+
|
|
24
|
+
puts '3. Next 6 phases starting from 2025-08-01:'
|
|
25
|
+
puts '-' * 45
|
|
26
|
+
future_phases = MoonPhaseTracker.phases_from_date('2025-08-01', 6)
|
|
27
|
+
puts tracker.format_phases(future_phases, 'Upcoming phases')
|
|
28
|
+
puts
|
|
29
|
+
|
|
30
|
+
puts '4. Current month phases:'
|
|
31
|
+
puts '-' * 25
|
|
32
|
+
current_phases = tracker.current_month_phases
|
|
33
|
+
if current_phases.any?
|
|
34
|
+
puts tracker.format_phases(current_phases, 'Current month phases')
|
|
35
|
+
else
|
|
36
|
+
puts 'No phases found for the current month.'
|
|
37
|
+
end
|
|
38
|
+
puts
|
|
39
|
+
|
|
40
|
+
puts '5. Next moon phase:'
|
|
41
|
+
puts '-' * 25
|
|
42
|
+
next_phase = tracker.next_phase
|
|
43
|
+
if next_phase
|
|
44
|
+
puts next_phase
|
|
45
|
+
puts "Details: #{next_phase.to_h}"
|
|
46
|
+
else
|
|
47
|
+
puts 'Could not retrieve the next phase.'
|
|
48
|
+
end
|
|
49
|
+
puts
|
|
50
|
+
|
|
51
|
+
puts '6. Different phase representations:'
|
|
52
|
+
puts '-' * 42
|
|
53
|
+
if august_phases.any?
|
|
54
|
+
phase = august_phases.first
|
|
55
|
+
puts "String: #{phase}"
|
|
56
|
+
puts "Hash: #{phase.to_h}"
|
|
57
|
+
puts "Symbol: #{phase.symbol}"
|
|
58
|
+
puts "Formatted date: #{phase.formatted_date}"
|
|
59
|
+
puts "Formatted time: #{phase.formatted_time}"
|
|
60
|
+
end
|
|
61
|
+
rescue MoonPhaseTracker::NetworkError => e
|
|
62
|
+
puts "Network error: #{e.message}"
|
|
63
|
+
puts 'Please check your internet connection.'
|
|
64
|
+
rescue MoonPhaseTracker::APIError => e
|
|
65
|
+
puts "API error: #{e.message}"
|
|
66
|
+
puts 'The service may be temporarily unavailable.'
|
|
67
|
+
rescue MoonPhaseTracker::InvalidDateError => e
|
|
68
|
+
puts "Date error: #{e.message}"
|
|
69
|
+
rescue StandardError => e
|
|
70
|
+
puts "Unexpected error: #{e.message}"
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
puts
|
|
74
|
+
puts '=== End of Examples ==='
|
|
@@ -50,12 +50,13 @@ module MoonPhaseTracker
|
|
|
50
50
|
begin
|
|
51
51
|
response = fetch_with_timeout(uri)
|
|
52
52
|
parse_response(response)
|
|
53
|
-
rescue Net::
|
|
53
|
+
rescue Net::OpenTimeout, Net::ReadTimeout => e
|
|
54
54
|
raise NetworkError, "Request timeout: #{e.message}"
|
|
55
55
|
rescue Net::HTTPError, SocketError => e
|
|
56
56
|
raise NetworkError, "Network error: #{e.message}"
|
|
57
|
-
rescue JSON::ParserError
|
|
58
|
-
|
|
57
|
+
rescue Net::HTTPBadResponse, JSON::ParserError
|
|
58
|
+
# Remote text stays out of messages and causes: it can carry credentials into user logs
|
|
59
|
+
raise APIError, "Malformed response from API", cause: nil
|
|
59
60
|
end
|
|
60
61
|
end
|
|
61
62
|
|
|
@@ -78,7 +79,7 @@ module MoonPhaseTracker
|
|
|
78
79
|
response = http.request(request)
|
|
79
80
|
|
|
80
81
|
unless response.is_a?(Net::HTTPSuccess)
|
|
81
|
-
raise APIError, "API request failed
|
|
82
|
+
raise APIError, "API request failed with HTTP #{response.code}"
|
|
82
83
|
end
|
|
83
84
|
|
|
84
85
|
response
|
|
@@ -88,7 +89,8 @@ module MoonPhaseTracker
|
|
|
88
89
|
def parse_response(response)
|
|
89
90
|
data = JSON.parse(response.body)
|
|
90
91
|
|
|
91
|
-
|
|
92
|
+
# Server-supplied text stays out of messages: it can carry credentials into user logs
|
|
93
|
+
raise APIError, "API returned an error (HTTP #{response.code})" if data["error"]
|
|
92
94
|
|
|
93
95
|
data
|
|
94
96
|
end
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module MoonPhaseTracker
|
|
4
|
+
class LunarCalculator
|
|
5
|
+
SYNODIC_MONTH = 29.530588853
|
|
6
|
+
KNOWN_NEW_MOON_JD = 2451550.26 # Jan 6, 2000 18:14 UTC
|
|
7
|
+
|
|
8
|
+
PHASE_BOUNDARIES = [
|
|
9
|
+
[ 0.0, "New Moon" ],
|
|
10
|
+
[ 0.0625, "Waxing Crescent" ],
|
|
11
|
+
[ 0.1875, "First Quarter" ],
|
|
12
|
+
[ 0.3125, "Waxing Gibbous" ],
|
|
13
|
+
[ 0.4375, "Full Moon" ],
|
|
14
|
+
[ 0.5625, "Waning Gibbous" ],
|
|
15
|
+
[ 0.6875, "Last Quarter" ],
|
|
16
|
+
[ 0.8125, "Waning Crescent" ]
|
|
17
|
+
].freeze
|
|
18
|
+
|
|
19
|
+
def phase_at(date)
|
|
20
|
+
time = coerce_to_time(date)
|
|
21
|
+
fraction = cycle_position(time)
|
|
22
|
+
age = lunar_age(time)
|
|
23
|
+
illum = illumination_from_fraction(fraction)
|
|
24
|
+
name = classify_phase(fraction)
|
|
25
|
+
|
|
26
|
+
Phase.from_calculation(
|
|
27
|
+
name: name,
|
|
28
|
+
date: time.utc.to_date,
|
|
29
|
+
time: time.utc.strftime("%H:%M"),
|
|
30
|
+
illumination: illum,
|
|
31
|
+
lunar_age: age
|
|
32
|
+
)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def illumination(date)
|
|
36
|
+
fraction = cycle_position(coerce_to_time(date))
|
|
37
|
+
illumination_from_fraction(fraction)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def lunar_age(date)
|
|
41
|
+
time = coerce_to_time(date)
|
|
42
|
+
jd = to_julian_date(time)
|
|
43
|
+
days_since = jd - KNOWN_NEW_MOON_JD
|
|
44
|
+
days_since % SYNODIC_MONTH
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def cycle_position(date)
|
|
48
|
+
time = coerce_to_time(date)
|
|
49
|
+
jd = to_julian_date(time)
|
|
50
|
+
days_since = jd - KNOWN_NEW_MOON_JD
|
|
51
|
+
(days_since % SYNODIC_MONTH) / SYNODIC_MONTH
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
private
|
|
55
|
+
|
|
56
|
+
def coerce_to_time(input)
|
|
57
|
+
case input
|
|
58
|
+
when Time then input.utc
|
|
59
|
+
when DateTime then input.to_time.utc
|
|
60
|
+
when Date then Time.utc(input.year, input.month, input.day, 12)
|
|
61
|
+
when String then coerce_to_time(Date.parse(input))
|
|
62
|
+
else raise ArgumentError, "Expected Date, Time, DateTime, or String. Got #{input.class}"
|
|
63
|
+
end
|
|
64
|
+
rescue Date::Error => e
|
|
65
|
+
raise ArgumentError, "Cannot parse date string: #{input.inspect} (#{e.message})"
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def to_julian_date(time)
|
|
69
|
+
utc = time.utc
|
|
70
|
+
y = utc.year
|
|
71
|
+
m = utc.month
|
|
72
|
+
d = utc.day + (utc.hour + utc.min / 60.0 + utc.sec / 3600.0) / 24.0
|
|
73
|
+
|
|
74
|
+
if m <= 2
|
|
75
|
+
y -= 1
|
|
76
|
+
m += 12
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
a = (y / 100).floor
|
|
80
|
+
b = 2 - a + (a / 4).floor
|
|
81
|
+
|
|
82
|
+
(365.25 * (y + 4716)).floor + (30.6001 * (m + 1)).floor + d + b - 1524.5
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def illumination_from_fraction(fraction)
|
|
86
|
+
((1 - Math.cos(2 * Math::PI * fraction)) / 2.0 * 100).round(2)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def classify_phase(fraction)
|
|
90
|
+
PHASE_BOUNDARIES.reverse_each do |threshold, name|
|
|
91
|
+
return name if fraction >= threshold
|
|
92
|
+
end
|
|
93
|
+
PHASE_BOUNDARIES.first.last
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
@@ -36,7 +36,10 @@ module MoonPhaseTracker
|
|
|
36
36
|
symbol: phase_attributes[:symbol],
|
|
37
37
|
iso_date: phase_attributes[:date]&.iso8601,
|
|
38
38
|
utc_time: phase_attributes[:time]&.utc&.iso8601,
|
|
39
|
-
interpolated: phase_attributes[:interpolated]
|
|
39
|
+
interpolated: phase_attributes[:interpolated],
|
|
40
|
+
source: phase_attributes[:source],
|
|
41
|
+
illumination: phase_attributes[:illumination],
|
|
42
|
+
lunar_age: phase_attributes[:lunar_age]
|
|
40
43
|
}
|
|
41
44
|
end
|
|
42
45
|
end
|
|
@@ -18,10 +18,15 @@ module MoonPhaseTracker
|
|
|
18
18
|
nil
|
|
19
19
|
end
|
|
20
20
|
|
|
21
|
-
def self.parse_time(time_string)
|
|
21
|
+
def self.parse_time(time_string, date = nil)
|
|
22
22
|
return nil unless time_string
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
if date
|
|
25
|
+
hour, minute = time_string.split(":").map(&:to_i)
|
|
26
|
+
Time.utc(date.year, date.month, date.day, hour, minute)
|
|
27
|
+
else
|
|
28
|
+
Time.parse("#{time_string} UTC")
|
|
29
|
+
end
|
|
25
30
|
rescue ArgumentError
|
|
26
31
|
nil
|
|
27
32
|
end
|
|
@@ -9,14 +9,29 @@ module MoonPhaseTracker
|
|
|
9
9
|
class Phase
|
|
10
10
|
include Comparable
|
|
11
11
|
|
|
12
|
-
attr_reader :name, :date, :time, :phase_type, :interpolated
|
|
12
|
+
attr_reader :name, :date, :time, :phase_type, :interpolated,
|
|
13
|
+
:source, :illumination, :lunar_age
|
|
13
14
|
|
|
14
|
-
def initialize(phase_data, interpolated: false)
|
|
15
|
+
def initialize(phase_data, interpolated: false, source: :api, illumination: nil, lunar_age: nil)
|
|
15
16
|
@name = phase_data["phase"]
|
|
16
17
|
@phase_type = Mapper.map_phase_type(@name)
|
|
17
18
|
@date = Parser.build_date(phase_data)
|
|
18
|
-
@time = Parser.parse_time(phase_data["time"])
|
|
19
|
+
@time = Parser.parse_time(phase_data["time"], @date)
|
|
19
20
|
@interpolated = interpolated
|
|
21
|
+
@source = source
|
|
22
|
+
@illumination = illumination
|
|
23
|
+
@lunar_age = lunar_age
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.from_calculation(name:, date:, time:, illumination:, lunar_age:)
|
|
27
|
+
phase_data = {
|
|
28
|
+
"phase" => name,
|
|
29
|
+
"year" => date.year,
|
|
30
|
+
"month" => date.month,
|
|
31
|
+
"day" => date.day,
|
|
32
|
+
"time" => time
|
|
33
|
+
}
|
|
34
|
+
new(phase_data, source: :calculated, illumination: illumination, lunar_age: lunar_age)
|
|
20
35
|
end
|
|
21
36
|
|
|
22
37
|
def formatted_date
|
|
@@ -42,7 +57,10 @@ module MoonPhaseTracker
|
|
|
42
57
|
date: @date,
|
|
43
58
|
time: @time,
|
|
44
59
|
symbol: symbol,
|
|
45
|
-
interpolated: @interpolated
|
|
60
|
+
interpolated: @interpolated,
|
|
61
|
+
source: @source,
|
|
62
|
+
illumination: @illumination,
|
|
63
|
+
lunar_age: @lunar_age
|
|
46
64
|
}
|
|
47
65
|
|
|
48
66
|
Formatter.build_hash_representation(phase_attributes)
|
|
@@ -15,7 +15,7 @@ module MoonPhaseTracker
|
|
|
15
15
|
estimated_date = calculate_estimated_date(last_phase)
|
|
16
16
|
phase_data = build_estimated_phase_data(estimated_date, last_phase)
|
|
17
17
|
|
|
18
|
-
Phase.new(phase_data)
|
|
18
|
+
Phase.new(phase_data, source: :interpolated)
|
|
19
19
|
rescue Date::Error, ArgumentError => e
|
|
20
20
|
warn "Failed to estimate next cycle phase: #{e.class}"
|
|
21
21
|
nil
|
|
@@ -60,7 +60,7 @@ module MoonPhaseTracker
|
|
|
60
60
|
return nil unless intermediate_datetime
|
|
61
61
|
|
|
62
62
|
phase_data = build_phase_data(intermediate_datetime, config[:name])
|
|
63
|
-
Phase.new(phase_data, interpolated: true)
|
|
63
|
+
Phase.new(phase_data, interpolated: true, source: :interpolated)
|
|
64
64
|
rescue Date::Error, ArgumentError => e
|
|
65
65
|
warn "Failed to create intermediate phase: #{e.class}"
|
|
66
66
|
nil
|
|
@@ -43,6 +43,20 @@ module MoonPhaseTracker
|
|
|
43
43
|
phases_from_date(Date.today, 1).first
|
|
44
44
|
end
|
|
45
45
|
|
|
46
|
+
def phase_at(date)
|
|
47
|
+
parsed = @date_parser.parse(date)
|
|
48
|
+
calculator.phase_at(parsed)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def illumination(date)
|
|
52
|
+
parsed = @date_parser.parse(date)
|
|
53
|
+
calculator.illumination(parsed)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def current_phase
|
|
57
|
+
calculator.phase_at(Time.now.utc)
|
|
58
|
+
end
|
|
59
|
+
|
|
46
60
|
def current_month_phases
|
|
47
61
|
today = Date.today
|
|
48
62
|
phases_for_month(today.year, today.month)
|
|
@@ -77,6 +91,10 @@ module MoonPhaseTracker
|
|
|
77
91
|
|
|
78
92
|
private
|
|
79
93
|
|
|
94
|
+
def calculator
|
|
95
|
+
@calculator ||= LunarCalculator.new
|
|
96
|
+
end
|
|
97
|
+
|
|
80
98
|
MONTH_NAMES = %w[
|
|
81
99
|
January February March April May June
|
|
82
100
|
July August September October November December
|
data/lib/moon_phase_tracker.rb
CHANGED
|
@@ -5,6 +5,7 @@ require_relative "moon_phase_tracker/rate_limiter"
|
|
|
5
5
|
require_relative "moon_phase_tracker/client"
|
|
6
6
|
require_relative "moon_phase_tracker/phase"
|
|
7
7
|
require_relative "moon_phase_tracker/phase_calculator"
|
|
8
|
+
require_relative "moon_phase_tracker/lunar_calculator"
|
|
8
9
|
require_relative "moon_phase_tracker/tracker"
|
|
9
10
|
|
|
10
11
|
module MoonPhaseTracker
|
|
@@ -39,4 +40,20 @@ module MoonPhaseTracker
|
|
|
39
40
|
def self.all_phases_from_date(date, num_cycles = 3)
|
|
40
41
|
Tracker.new.all_phases_from_date(date, num_cycles)
|
|
41
42
|
end
|
|
43
|
+
|
|
44
|
+
def self.calculator
|
|
45
|
+
@calculator ||= LunarCalculator.new
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def self.phase_at(date)
|
|
49
|
+
calculator.phase_at(date)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def self.illumination(date)
|
|
53
|
+
calculator.illumination(date)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def self.current_phase
|
|
57
|
+
calculator.phase_at(Time.now.utc)
|
|
58
|
+
end
|
|
42
59
|
end
|