Class: Lita::Handlers::Diabetter

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/handlers/diabetter.rb

Constant Summary collapse

@@conversion_ratio =
18.0182
@@lower =

Lower limit for mmol/L - mg/dL cutoff

25.0
@@upper =

Upper limit for mmol/L - mg/dL cutoff

50.0

Instance Method Summary collapse

Instance Method Details

#convert(response) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/lita/handlers/diabetter.rb', line 21

def convert(response)
  if response.message.body.match(URI.regexp(%w(http https))).nil?
    input = response.matches[0][0]
    Lita.logger.debug('Converting BG for input "' + input + '"')

    if input.to_f < @@lower
      response.reply("#{input} mmol/L is #{mmol_to_mgdl(input).to_s} mg/dL")
    elsif input.to_f >= @@lower && input.to_f < @@upper
      mmol = mgdl_to_mmol(input).to_s
      mgdl = mmol_to_mgdl(input).to_s

      result = "*I'm not sure if you gave me mmol/L or mg/dL, so I'll give you both.*\n"
      result += "#{input} mg/dL is **#{mmol} mmol/L**\n"
      result += "#{input} mmol/L is **#{mgdl} mg/dL**"

      response.reply(result)
    else
      response.reply("#{input} mg/dL is #{mgdl_to_mmol(input).to_s} mmol/L")
    end
  end
end

#dcct_to_ifcc(n) ⇒ Object



139
140
141
# File 'lib/lita/handlers/diabetter.rb', line 139

def dcct_to_ifcc(n)
  (n.to_f - 2.15) * 10.929
end

#dcct_to_mgdl(n) ⇒ Object



147
148
149
# File 'lib/lita/handlers/diabetter.rb', line 147

def dcct_to_mgdl(n)
  (n.to_f * 28.7) - 46.7
end

#estimate_a1c(response) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/lita/handlers/diabetter.rb', line 43

def estimate_a1c(response)
  input = response.matches[0][0]
  Lita.logger.debug('Estimating a1c for input "' + input + '"')


  if input.to_f < @@lower
    mmol = input.to_f.round(1)
    mgdl = mmol_to_mgdl(mmol)

    type = 'mmol'
  elsif input.to_f >= @@lower && input.to_f < @@upper
    mmol = input.to_f.round(1)
    mgdl_from_mmol = mmol_to_mgdl(input.to_f)
    mgdl = input.to_f

    dcct_alt = mgdl_to_dcct(mgdl_from_mmol)
    ifcc_alt = dcct_to_ifcc(dcct_alt).round(0).to_s
    dcct_alt = dcct.round(1).to_s

    type = 'unknown'
  else
    mgdl = input.to_i
    mmol = mgdl_to_mmol(mgdl)

    type = 'mgdl'
  end

  dcct = mgdl_to_dcct(mgdl)
  ifcc = dcct_to_ifcc(dcct).round(0).to_s
  dcct = dcct.round(1).to_s


  if type == 'unknown'
    reply = "*I'm not sure if you entered mmol/L or mg/dL, so I'll give you both*\n"
    reply += make_average_sentence('mmol', mmol, dcct_alt, ifcc_alt) + "\n"
    reply += make_average_sentence('mgdl', mgdl, dcct, ifcc)
  elsif type=='mmol'
    reply = make_average_sentence('mmol', mmol, dcct, ifcc)
  else
    reply = make_average_sentence('mgdl', mgdl, dcct, ifcc)
  end

  response.reply(reply)
end

#estimate_average_from_a1c(response) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/lita/handlers/diabetter.rb', line 98

def estimate_average_from_a1c(response)
  input = response.matches[0][0]
  Lita.logger.debug('Converting a1c to BG for input "' + input + '"')
  a1c = input.to_f
  dcct = 0
  ifcc = 0

  if input.index('.') == nil
    ifcc = a1c.round(0)
    dcct = ifcc_to_dcct(a1c).round(1)
  else
    dcct = a1c.round(1)
    ifcc = dcct_to_ifcc(a1c).round
  end

  mgdl = dcct_to_mgdl(dcct)

  reply = 'an A1C of ' + dcct.to_s + '% (DCCT) or '
  reply = reply + ifcc.to_s + ' mmol/mol (IFCC)'
  reply = reply + ' is about '
  reply = reply + mgdl.round.to_s + ' mg/dL or '
  reply = reply + mgdl_to_mmol(mgdl).round(1).to_s + ' mmol/L'
  response.reply(reply)
end

#ifcc_to_dcct(n) ⇒ Object



143
144
145
# File 'lib/lita/handlers/diabetter.rb', line 143

def ifcc_to_dcct(n)
  (n.to_f / 10.929) + 2.15
end

#ifcc_to_mgdl(n) ⇒ Object



151
152
153
# File 'lib/lita/handlers/diabetter.rb', line 151

def ifcc_to_mgdl(n)
  dcct_to_mgdl((n / 10.929) + 2.5)
end

#make_average_sentence(type, value, dcct, ifcc) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/lita/handlers/diabetter.rb', line 88

def make_average_sentence(type, value, dcct, ifcc)
  if type == 'mmol'
    string = "An average of **#{value} mmol/L** is about "
  elsif type == 'mgdl'
    string = "An average of **#{value} mg/dL** is about "
  end

  string += "**#{dcct}%** (DCCT) or **#{ifcc} mmol/mol** (IFCC)"
end

#mgdl_to_dcct(n) ⇒ Object



131
132
133
# File 'lib/lita/handlers/diabetter.rb', line 131

def mgdl_to_dcct(n)
  ((n.to_i + 46.7) / 28.7)
end

#mgdl_to_ifcc(n) ⇒ Object



135
136
137
# File 'lib/lita/handlers/diabetter.rb', line 135

def mgdl_to_ifcc(n)
  ((mgdl_to_dcct(n) - 2.15) * 10.929)
end

#mgdl_to_mmol(n) ⇒ Object



123
124
125
# File 'lib/lita/handlers/diabetter.rb', line 123

def mgdl_to_mmol(n)
  (n.to_i / @@conversion_ratio).round(1)
end

#mmol_to_mgdl(n) ⇒ Object



127
128
129
# File 'lib/lita/handlers/diabetter.rb', line 127

def mmol_to_mgdl(n)
  (n.to_f * @@conversion_ratio).round
end