moon_phase_tracker 1.3.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3fbc22c1b9f3481bd4585f49394696bea4bbef56302467bc0d9433bc17f6c179
4
+ data.tar.gz: bd9d8566042eb8d1dccc4ab8ce66de2e6542e9103e3c2732ee45c0f467c98e3d
5
+ SHA512:
6
+ metadata.gz: b5575c03ca0ea020a9b9332c0c624567dd9cd370192c0db29ef5578d28c0f98bfbc8c5d2811eeb3498d270b2e1ee8ef279028c0f4638ae4063ff7958abe02457
7
+ data.tar.gz: ffe82390f6ad4deab517a8aaa0ef0e32541da030b0b0a10ba5d6991ae1749d2219069d5838cad8e05bff76f3c31913355536e766b50052fa590b177b046c2c7a
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ # Omakase Ruby styling for Rails
2
+ inherit_gem: { rubocop-rails-omakase: rubocop.yml }
3
+
4
+ # Overwrite or add rules to create your own house style
5
+ #
6
+ # # Use `[a, [b, c]]` not `[ a, [ b, c ] ]`
7
+ # Layout/SpaceInsideArrayLiteralBrackets:
8
+ # Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,166 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ## [1.3.2] - 2025-08-04
11
+
12
+ ### Changed
13
+ - **Phase Class Refactoring**
14
+ - Refactored `Phase` class following SOLID principles
15
+ - Extracted specialized components: `Parser`, `Formatter`, `Comparator`, `Mapper`
16
+ - Applied Single Responsibility Principle with dedicated phase/ namespace
17
+ - Improved error handling with graceful degradation
18
+ - Enhanced code maintainability while preserving 100% API compatibility
19
+ - All 53 tests continue to pass with identical functionality
20
+
21
+ ## [1.3.1] - 2025-08-04
22
+
23
+ ### Changed
24
+ - **Major Code Refactoring**
25
+ - Refactored `Tracker` class following SOLID principles
26
+ - Extracted specialized services: `PhaseQueryService`, `PhaseFormatter`, `DateParser`, `Validators`
27
+ - Refactored `PhaseCalculator` class with composition over inheritance
28
+ - Extracted specialized classes: `PhaseInterpolator`, `CycleEstimator`
29
+ - Applied Single Responsibility Principle across all classes
30
+ - Implemented early return patterns for better code flow
31
+ - Improved method naming and code organization
32
+ - Added comprehensive English comments for maintainability
33
+
34
+ ### Technical
35
+ - **Architecture Improvements**
36
+ - Converted large classes into smaller, focused service objects
37
+ - Improved testability and maintainability through better separation of concerns
38
+ - Enhanced code readability with descriptive method and variable names
39
+ - Eliminated code duplication and improved DRY principles
40
+ - Better error handling with specific exception types
41
+ - Removed generic `rescue StandardError` anti-patterns
42
+
43
+ ## [1.3.0] - 2025-08-04
44
+
45
+ ### Added
46
+ - **Rate Limiting Support**
47
+ - Implemented configurable rate limiting using token bucket algorithm
48
+ - Default rate limit: 1 request per second with burst size of 1
49
+ - New `RateLimiter` class with thread-safe token bucket implementation
50
+ - Environment variable configuration: `MOON_PHASE_RATE_LIMIT` and `MOON_PHASE_BURST_SIZE`
51
+ - Rate limiting can be disabled by setting `MOON_PHASE_RATE_LIMIT=0`
52
+ - Added `rate_limit_info` method to inspect current rate limiting configuration
53
+ - Custom rate limiter can be passed to `Tracker.new(rate_limiter: custom_limiter)`
54
+
55
+ ### Changed
56
+ - **Client API Updates**
57
+ - `Client.new` now accepts optional `rate_limiter` parameter
58
+ - `Tracker.new` now accepts optional `rate_limiter` parameter
59
+ - All API requests are now automatically rate limited by default
60
+
61
+ ### Technical
62
+ - Added comprehensive test coverage for rate limiting functionality
63
+ - Added `examples/rate_limiting_example.rb` demonstrating all rate limiting features
64
+ - Updated documentation with rate limiting configuration and usage examples
65
+
66
+ ## [1.2.0] - 2025-08-04
67
+
68
+ ### Added
69
+ - **8 Moon Phases Support**
70
+ - Complete support for all 8 lunar phases instead of just 4 major phases
71
+ - Added intermediate phases: Waxing Crescent 🌒, Waxing Gibbous 🌔, Waning Gibbous 🌖, Waning Crescent 🌘
72
+ - New `PhaseCalculator` class for interpolating intermediate phases between major phases
73
+ - `interpolated` attribute on `Phase` class to distinguish calculated from official phases
74
+
75
+ - **New Public API Methods**
76
+ - `MoonPhaseTracker.all_phases_for_month(year, month)` - Get all 8 phases for a specific month
77
+ - `MoonPhaseTracker.all_phases_for_year(year)` - Get all 8 phases for a year
78
+ - `MoonPhaseTracker.all_phases_from_date(date, num_cycles)` - Get all 8 phases from a specific date
79
+
80
+ - **Enhanced Phase Representation**
81
+ - Updated `PHASE_SYMBOLS` constant with all 8 phase emojis
82
+ - Updated `PHASE_NAMES` mapping to include intermediate phases
83
+ - Visual indicator (`~`) for interpolated phases in formatted output
84
+ - Enhanced `format_phases` method to show major vs interpolated phase counts
85
+
86
+ - **Examples & Documentation**
87
+ - New `examples/eight_phases_example.rb` demonstrating 8-phase functionality
88
+ - Comprehensive comparison between 4-phase and 8-phase outputs
89
+ - Phase symbols reference with visual emojis
90
+
91
+ ### Changed
92
+ - **Precision & Accuracy**
93
+ - Interpolation algorithm provides ~85-90% accuracy for intermediate phases
94
+ - Suitable for lunar calendar applications requiring higher phase granularity
95
+ - Maintains backward compatibility with existing 4-phase API methods
96
+
97
+ ## [1.1.0] - 2025-08-04
98
+
99
+ ### Changed
100
+ - **BREAKING CHANGE**: Date format standardized to ISO 8601 (YYYY-MM-DD)
101
+ - `Phase#formatted_date` now returns dates in ISO 8601 format instead of DD/MM/YYYY
102
+ - `Phase#to_s` output format updated to use ISO 8601 dates
103
+ - All documentation and examples updated to reflect new date format
104
+
105
+ - **Internationalization Updates**
106
+ - User interface completely translated to American English
107
+ - Error messages now in English instead of Brazilian Portuguese
108
+ - Month names in `Tracker.month_name` changed from Portuguese to English
109
+ - Console messages and examples translated to English
110
+ - `Phase#to_s` separator changed from "às" to "at"
111
+
112
+ - **Date/Time Consistency**
113
+ - All date representations now use consistent ISO 8601 format
114
+ - Improved international compatibility and API consistency
115
+ - Enhanced sorting and comparison reliability
116
+
117
+ ## [1.0.0] - 2025-08-04
118
+
119
+ ### Added
120
+ - **Core Functionality**
121
+ - Integration with USNO Navy API 4.0.1 for accurate moon phase data
122
+ - `MoonPhaseTracker.phases_for_month(year, month)` - Get phases for a specific month
123
+ - `MoonPhaseTracker.phases_for_year(year)` - Get all phases for a year
124
+ - `MoonPhaseTracker.phases_from_date(date, num_phases)` - Get phases from a specific date
125
+
126
+ - **Phase Representation**
127
+ - `Phase` class with emoji symbols for each moon phase (🌑🌓🌕🌗)
128
+ - ISO 8601 date formatting (YYYY-MM-DD)
129
+ - Multiple output formats (string, hash, formatted display)
130
+ - Phase comparison and sorting capabilities
131
+
132
+ - **Tracker Interface**
133
+ - `Tracker` class for advanced moon phase operations
134
+ - `current_month_phases` and `current_year_phases` convenience methods
135
+ - `next_phase` to get the upcoming moon phase
136
+ - `format_phases` with English month names
137
+
138
+ - **HTTP Client**
139
+ - Robust HTTP client with timeout handling
140
+ - URI caching for performance optimization
141
+ - Comprehensive error handling (network, API, parsing)
142
+ - User-Agent header with gem version
143
+
144
+ - **Error Handling**
145
+ - Custom exception hierarchy (`APIError`, `NetworkError`, `InvalidDateError`)
146
+ - Input validation for dates, years, and phase counts
147
+ - English error messages
148
+
149
+ - **Testing & Documentation**
150
+ - Complete RSpec test suite with 21 passing tests
151
+ - Aggregate failure testing approach for efficiency
152
+ - Mocked API responses for reliable testing
153
+ - Comprehensive README with usage examples
154
+ - Interactive console with helpful examples
155
+ - Usage example script in `examples/` directory
156
+
157
+ - **Dependencies**
158
+ - `net-http ~> 0.4` for HTTP requests
159
+ - `json ~> 2.7` for JSON parsing
160
+ - Ruby 3.2.0+ compatibility
161
+
162
+ ### Technical Details
163
+ - API endpoint: `https://aa.usno.navy.mil/api/moon/phases/`
164
+ - All times provided in Universal Time (UTC)
165
+ - Supports date range from 1700 to current year + 10
166
+ - Phase count range: 1-99 phases per request
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Daniel K Lima
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,302 @@
1
+ # MoonPhaseTracker 🌙
2
+
3
+ A Ruby gem for tracking moon phases using the official US Naval Observatory (USNO) API. Perfect for creating lunar calendar-based scheduling applications.
4
+
5
+ **Expected accuracy: ~85-90% (suitable for lunar calendars)**
6
+
7
+ ## Features
8
+
9
+ - 🌑 Query moon phases by month, year, or from a specific date
10
+ - 🌒 Complete 8-phase lunar cycle support (4 major + 4 intermediate phases)
11
+ - 🌓 Simple and intuitive interface
12
+ - 🌔 Accurate data from the official USNO Navy API
13
+ - 🌕 Visual representation with moon phase emojis
14
+ - 🌖 Interpolated intermediate phases for enhanced precision
15
+ - 🌗 Visual indicators for calculated vs official phases
16
+ - 🌘 Automatic caching to optimize requests
17
+ - âš¡ Robust error handling
18
+ - 🚦 Configurable rate limiting to respect API limits
19
+
20
+ ## Installation
21
+
22
+ Add this line to your application's Gemfile:
23
+
24
+ ```ruby
25
+ gem 'moon_phase_tracker'
26
+ ```
27
+
28
+ And then execute:
29
+
30
+ ```bash
31
+ bundle install
32
+ ```
33
+
34
+ Or install it directly:
35
+
36
+ ```bash
37
+ gem install moon_phase_tracker
38
+ ```
39
+
40
+ ## Usage
41
+
42
+ ### Basic Usage (4 Major Phases)
43
+
44
+ ```ruby
45
+ require 'moon_phase_tracker'
46
+
47
+ # Moon phases for August 2025 (4 major phases only)
48
+ phases = MoonPhaseTracker.phases_for_month(2025, 8)
49
+ puts phases.first.to_s
50
+ # => "🌑 New Moon - 2025-08-04 at 11:13"
51
+
52
+ # All phases for 2025
53
+ year_phases = MoonPhaseTracker.phases_for_year(2025)
54
+
55
+ # Next 6 phases from a specific date
56
+ future_phases = MoonPhaseTracker.phases_from_date("2025-08-01", 6)
57
+ ```
58
+
59
+ ### Complete 8-Phase Lunar Cycle
60
+
61
+ ```ruby
62
+ require 'moon_phase_tracker'
63
+
64
+ # All 8 phases for August 2025 (major + intermediate)
65
+ all_phases = MoonPhaseTracker.all_phases_for_month(2025, 8)
66
+ all_phases.each do |phase|
67
+ indicator = phase.interpolated ? "~ " : " "
68
+ puts "#{indicator}#{phase}"
69
+ end
70
+ # => 🌑 New Moon - 2025-08-04 at 11:13
71
+ # => ~ 🌒 Waxing Crescent - 2025-08-08 at 03:16
72
+ # => 🌓 First Quarter - 2025-08-12 at 15:19
73
+ # => ~ 🌔 Waxing Gibbous - 2025-08-15 at 16:53
74
+ # => 🌕 Full Moon - 2025-08-19 at 18:26
75
+ # => ~ 🌖 Waning Gibbous - 2025-08-22 at 13:56
76
+ # => 🌗 Last Quarter - 2025-08-26 at 09:26
77
+ # => ~ 🌘 Waning Crescent - 2025-08-29 at 02:56
78
+
79
+ # All 8 phases for entire year
80
+ all_year_phases = MoonPhaseTracker.all_phases_for_year(2025)
81
+
82
+ # All 8 phases from specific date (2 lunar cycles)
83
+ extended_phases = MoonPhaseTracker.all_phases_from_date("2025-08-01", 2)
84
+ ```
85
+
86
+ ### Using the Tracker Class
87
+
88
+ ```ruby
89
+ tracker = MoonPhaseTracker::Tracker.new
90
+
91
+ # Automatic formatting for display
92
+ august_phases = tracker.phases_for_month(2025, 8)
93
+ formatted = tracker.format_phases(august_phases, "August Phases")
94
+ puts formatted
95
+
96
+ # Next moon phase
97
+ next_phase = tracker.next_phase
98
+ puts next_phase.to_s
99
+
100
+ # Current month phases
101
+ current_month = tracker.current_month_phases
102
+ ```
103
+
104
+ ### Working with Individual Phases
105
+
106
+ ```ruby
107
+ phase = phases.first
108
+
109
+ # Phase information
110
+ puts phase.name # "New Moon"
111
+ puts phase.symbol # "🌑"
112
+ puts phase.formatted_date # "2025-08-04"
113
+ puts phase.formatted_time # "11:13"
114
+ puts phase.interpolated # false (official USNO data)
115
+
116
+ # Working with interpolated phases
117
+ interpolated_phase = all_phases.find(&:interpolated)
118
+ puts interpolated_phase.interpolated # true (calculated)
119
+ puts interpolated_phase.name # "Waxing Crescent"
120
+ puts interpolated_phase.symbol # "🌒"
121
+
122
+ # Complete hash with all data
123
+ details = phase.to_h
124
+ # => {
125
+ # name: "New Moon",
126
+ # phase_type: :new_moon,
127
+ # date: "2025-08-04",
128
+ # time: "11:13",
129
+ # symbol: "🌑",
130
+ # iso_date: "2025-08-04",
131
+ # utc_time: "2025-08-04T11:13:00Z",
132
+ # interpolated: false
133
+ # }
134
+
135
+ # Date checks
136
+ phase.in_month?(2025, 8) # => true
137
+ phase.in_year?(2025) # => true
138
+ ```
139
+
140
+ ### Error Handling
141
+
142
+ ```ruby
143
+ begin
144
+ phases = MoonPhaseTracker.phases_for_month(2025, 8)
145
+ rescue MoonPhaseTracker::NetworkError => e
146
+ puts "Network error: #{e.message}"
147
+ rescue MoonPhaseTracker::APIError => e
148
+ puts "API error: #{e.message}"
149
+ rescue MoonPhaseTracker::InvalidDateError => e
150
+ puts "Invalid date: #{e.message}"
151
+ end
152
+ ```
153
+
154
+ ### Practical Examples
155
+
156
+ See the `examples/usage_example.rb` and `examples/eight_phases_example.rb` files for complete usage examples.
157
+
158
+ ## API Reference
159
+
160
+ ### Main Methods (4 Major Phases)
161
+
162
+ - `MoonPhaseTracker.phases_for_month(year, month)` - Major phases for a specific month
163
+ - `MoonPhaseTracker.phases_for_year(year)` - All major phases for a year
164
+ - `MoonPhaseTracker.phases_from_date(date, num_phases)` - Major phases from a specific date
165
+
166
+ ### 8-Phase Methods (Major + Intermediate)
167
+
168
+ - `MoonPhaseTracker.all_phases_for_month(year, month)` - All 8 phases for a specific month
169
+ - `MoonPhaseTracker.all_phases_for_year(year)` - All 8 phases for a year
170
+ - `MoonPhaseTracker.all_phases_from_date(date, num_cycles)` - All 8 phases from a specific date
171
+
172
+ ### Complete Phase Types
173
+
174
+ #### Major Phases (Official USNO Data)
175
+ - 🌑 `:new_moon` - New Moon
176
+ - 🌓 `:first_quarter` - First Quarter
177
+ - 🌕 `:full_moon` - Full Moon
178
+ - 🌗 `:last_quarter` - Last Quarter
179
+
180
+ #### Intermediate Phases (Interpolated ~85-90% accuracy)
181
+ - 🌒 `:waxing_crescent` - Waxing Crescent
182
+ - 🌔 `:waxing_gibbous` - Waxing Gibbous
183
+ - 🌖 `:waning_gibbous` - Waning Gibbous
184
+ - 🌘 `:waning_crescent` - Waning Crescent
185
+
186
+ ## Data Source
187
+
188
+ This gem uses the official US Naval Observatory (USNO) API:
189
+ - **Base URL**: https://aa.usno.navy.mil/api/moon/phases/
190
+ - **API Version**: 4.0.1
191
+ - **Documentation**: https://aa.usno.navy.mil/data/api
192
+
193
+ All times are provided in Universal Time (UTC).
194
+
195
+ ## Rate Limiting
196
+
197
+ The gem implements respectful rate limiting to avoid overwhelming the USNO Navy API. By default, it allows **1 request per second** with a burst size of 1.
198
+
199
+ ### Default Rate Limiting
200
+
201
+ ```ruby
202
+ # Default: 1 request per second
203
+ tracker = MoonPhaseTracker::Tracker.new
204
+
205
+ # Check current rate limit configuration
206
+ puts tracker.rate_limit_info
207
+ # => {:requests_per_second=>1.0, :burst_size=>1, :available_tokens=>1}
208
+
209
+ # Multiple requests will be automatically rate limited
210
+ start = Time.now
211
+ tracker.phases_for_year(2025) # Immediate
212
+ tracker.phases_for_year(2024) # Waits ~1 second
213
+ puts "Total time: #{Time.now - start}s" # ~1.0 seconds
214
+ ```
215
+
216
+ ### Custom Rate Limiting
217
+
218
+ ```ruby
219
+ # Create custom rate limiter: 3 requests per second, burst of 2
220
+ rate_limiter = MoonPhaseTracker::RateLimiter.new(
221
+ requests_per_second: 3.0,
222
+ burst_size: 2
223
+ )
224
+
225
+ tracker = MoonPhaseTracker::Tracker.new(rate_limiter: rate_limiter)
226
+
227
+ # Burst requests are immediate, then rate limited
228
+ tracker.phases_for_year(2025) # Immediate
229
+ tracker.phases_for_year(2024) # Immediate (burst)
230
+ tracker.phases_for_year(2023) # Waits ~0.33 seconds
231
+ ```
232
+
233
+ ### Environment Variable Configuration
234
+
235
+ ```ruby
236
+ # Set via environment variables
237
+ ENV["MOON_PHASE_RATE_LIMIT"] = "2.5"
238
+ ENV["MOON_PHASE_BURST_SIZE"] = "3"
239
+
240
+ tracker = MoonPhaseTracker::Tracker.new
241
+ puts tracker.rate_limit_info
242
+ # => {:requests_per_second=>2.5, :burst_size=>3, :available_tokens=>3}
243
+ ```
244
+
245
+ ### Disabling Rate Limiting
246
+
247
+ ```ruby
248
+ # Disable rate limiting completely
249
+ ENV["MOON_PHASE_RATE_LIMIT"] = "0"
250
+
251
+ tracker = MoonPhaseTracker::Tracker.new
252
+ puts tracker.rate_limit_info # => nil
253
+
254
+ # Or pass nil explicitly
255
+ tracker = MoonPhaseTracker::Tracker.new(rate_limiter: nil)
256
+ ```
257
+
258
+ ### Rate Limit Monitoring
259
+
260
+ ```ruby
261
+ rate_limiter = MoonPhaseTracker::RateLimiter.new(
262
+ requests_per_second: 1.0,
263
+ burst_size: 2
264
+ )
265
+
266
+ # Check if request can proceed without waiting
267
+ puts rate_limiter.can_proceed? # => true
268
+
269
+ # Check current token status
270
+ puts rate_limiter.configuration
271
+ # => {:requests_per_second=>1.0, :burst_size=>2, :available_tokens=>2}
272
+
273
+ # Manual rate limiting control
274
+ rate_limiter.throttle # Waits if necessary and consumes token
275
+ ```
276
+
277
+ ### Token Bucket Algorithm
278
+
279
+ The rate limiter uses a token bucket algorithm:
280
+
281
+ - **Tokens**: Represent available requests
282
+ - **Bucket Size**: Maximum burst requests allowed
283
+ - **Refill Rate**: How quickly tokens are replenished
284
+ - **Thread Safe**: Handles concurrent requests safely
285
+
286
+ ## Development
287
+
288
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
289
+
290
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
291
+
292
+ ## Contributing
293
+
294
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dklima/moon_phase_tracker. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/dklima/moon_phase_tracker/blob/main/CODE_OF_CONDUCT.md).
295
+
296
+ ## License
297
+
298
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
299
+
300
+ ## Code of Conduct
301
+
302
+ Everyone interacting in the MoonPhaseTracker project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/dklima/moon_phase_tracker/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +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