Class: DexcomShareApi::Glucose

Inherits:
Object
  • Object
show all
Defined in:
lib/dexcom_share_api/glucose.rb

Instance Method Summary collapse

Constructor Details

#initialize(raw_glucose_data) ⇒ Glucose

"WT"=>"Date(1709620791000)",
"ST"=>"Date(1709620791000)",
"DT"=>"Date(1709620791000-0500)",
"Value"=>164,
"Trend"=>"Flat",



14
15
16
# File 'lib/dexcom_share_api/glucose.rb', line 14

def initialize(raw_glucose_data)
  @raw_glucose_data = raw_glucose_data
end

Instance Method Details

#inspectObject



28
29
30
31
32
33
34
35
# File 'lib/dexcom_share_api/glucose.rb', line 28

def inspect
  out = "#<#{self.class}:#{object_id}"
  to_h.each do |key, value|
    out << " @#{key}=#{value}"
  end
  out << ">"
  out
end

#mgdlObject



72
73
74
# File 'lib/dexcom_share_api/glucose.rb', line 72

def mgdl
  @raw_glucose_data["Value"].to_f
end

#mmolObject



68
69
70
# File 'lib/dexcom_share_api/glucose.rb', line 68

def mmol
  mgdl_to_mmol(mgdl)
end

#timestampObject

This is parsing out an epoch time from “Date(1709620791000)”. It’s messy.



63
64
65
66
# File 'lib/dexcom_share_api/glucose.rb', line 63

def timestamp
  raw_timestamp = @raw_glucose_data["ST"].match(/\d+/)[0]
  DateTime.strptime(raw_timestamp, '%Q').iso8601
end

#to_hObject



18
19
20
21
22
23
24
25
26
# File 'lib/dexcom_share_api/glucose.rb', line 18

def to_h
  {
    trend:,
    trend_arrow:,
    timestamp:,
    mmol:,
    mgdl:,
  }
end

#trendObject



37
38
39
# File 'lib/dexcom_share_api/glucose.rb', line 37

def trend
  @raw_glucose_data["Trend"].split(/(?=[A-Z])/).join("-").downcase
end

#trend_arrowObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dexcom_share_api/glucose.rb', line 41

def trend_arrow
  case trend
  when "double-up"
    "↑↑"
  when "single-up"
    "↑"
  when "forty-five-up"
    "↗"
  when "flat"
    "→"
  when "forty-five-down"
    "↘"
  when "single-down"
    "↓"
  when "double-down"
    "↓↓"
  else
    raise StandardError, "Unhandled trend `#{trend}`. This is a bug!"
  end
end