forked from mscoutermarsh/mscoutermarsh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
whoop.rb
59 lines (50 loc) · 1.7 KB
/
whoop.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
49
50
51
52
53
54
55
56
57
58
59
# fetch("https://api-7.whoop.com/users/84081/cycles?end=2020-06-16T06:59:59.999Z&start=2020-06-14T07:00:00.000Z", {
# "headers": {
# "accept": "application/json, text/plain, */*",
# "accept-language": "en-US,en;q=0.9",
# "authorization": "bearer +lJJsLMSMYV9w8fm7G4n/p2v2MT2zZvvD2honMNI3qjnrRU532lpWxFuk4STY5sC",
# "cache-control": "no-cache",
# "pragma": "no-cache",
# "sec-fetch-dest": "empty",
# "sec-fetch-mode": "cors",
# "sec-fetch-site": "same-site"
# },
# "referrer": "https://app.whoop.com/athlete/84081/1d/today",
# "referrerPolicy": "no-referrer-when-downgrade",
# "body": null,
# "method": "GET",
# "mode": "cors"
# });
require "httparty"
require "pry"
require 'markdown-tables'
module Whoop
def self.get_duration_hrs_and_mins(duration)
hours = duration / (1000 * 60 * 60)
minutes = duration / (1000 * 60) % 60
"#{hours}h #{minutes}m"
rescue
""
end
def self.stats
now = (DateTime.now).iso8601.gsub("+00:00", "Z")
week_ago = (DateTime.now - 6).iso8601.gsub("+00:00", "Z")
puts now
puts week_ago
response = HTTParty.get("https://api-7.whoop.com/users/84081/cycles?end=#{now}&start=#{week_ago}",{
headers: {"authorization" => "bearer #{ENV["WHOOP_KEY"]}" }
})
labels = ["Day", "Hours of sleep", "Resting heart rate"]
data = []
response.each do |day|
if day["sleep"]
sleep = day["sleep"]["sleeps"].sum { |s| s["qualityDuration"] }
sleep = get_duration_hrs_and_mins(sleep)
data << [day["days"].first, sleep, day["recovery"]["restingHeartRate"]]
end
end
MarkdownTables.make_table(labels, data, is_rows: true)
rescue
return "*Whoop API call failed, oh no!*"
end
end