-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoon_phase_for_date.rb
48 lines (38 loc) · 1.06 KB
/
moon_phase_for_date.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
require 'faraday'
class MoonPhaseForDate
URL = "https://everydaycalculation.com/moon-phase.php?ajax=1"
def self.call(date:)
new(date).call
end
def initialize(date)
date = Date.parse(date)
@year = date.year
@month = date.month
@day = date.day
end
def call
data = {:c=>"g", :d=>@day, :m=>@month, :y=>@year}
response = Faraday.post(URL) do |req|
req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
req.body = URI.encode_www_form(data)
end
translate_reponse(response.body)
end
private
def translate_reponse(str)
match = str.match(/<b>.*<\/b>/)
phase = match[0].sub("<b>", '').sub("</b>", '')
case phase
when 'New Moon' then "new"
when 'Waxing (Young) Crescent' then 'crescent'
when 'First Quarter' then 'first quarter'
when 'Waxing Gibbous' then 'gibbous'
when 'Full Moon' then 'full'
when 'Waning Gibbous' then 'disseminating'
when 'Last Quarter' then 'last quarter'
when 'Waning (Old) Crescent' then 'balsamic'
else
phase
end
end
end