module SunTimes
Constants
- API_URL
- STYLE_TITLE
Public Instance Methods
Source
# File lib/sun_times.rb, line 13 def fetch(lat, lon, fetch_json: RabbotHttp.method(:fetch_json)) url = "#{API_URL}?lat=#{lat}&lng=#{lon}&formatted=0" data = fetch_json.call(url) return nil unless data["status"] == "OK" results = data["results"] || {} { sunrise: results["sunrise"], sunset: results["sunset"], day_length: results["day_length"] } rescue StandardError nil end
Source
# File lib/sun_times.rb, line 36 def format_day_length(seconds) total = seconds.to_i hours = total / 3600 minutes = (total % 3600) / 60 "#{hours}h #{minutes}m" end
Source
# File lib/sun_times.rb, line 43 def format_report(place:, times:) sunrise = format_time_utc(times[:sunrise]) sunset = format_time_utc(times[:sunset]) return nil unless sunrise && sunset header = IrcFormat.report_header(tag: "SUN", title: place.to_s.strip, style: STYLE_TITLE) body = "Sunrise โ #{sunrise} ยท Sunset โ #{sunset}" length = format_day_length(times[:day_length]) footer = IrcFormat.report_footer("day length #{length}") [header, body, footer].join("\n") end
Source
# File lib/sun_times.rb, line 28 def format_time_utc(iso_string) return nil if iso_string.nil? || iso_string.to_s.strip.empty? Time.parse(iso_string.to_s).utc.strftime("%H:%M UTC") rescue StandardError nil end
Source
# File lib/sun_times.rb, line 55 def report_for_coords(lat, lon, place:, fetch_json: RabbotHttp.method(:fetch_json)) times = fetch(lat, lon, fetch_json: fetch_json) return "Could not fetch sun times for #{place}" unless times format_report(place: place, times: times) || "Could not format sun times for #{place}" end