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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/lita/handlers/diabetter.rb', line 34

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

#convert_mgdl(response) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/lita/handlers/diabetter.rb', line 64

def convert_mgdl(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 + '" from mg/dL to mmol/L')
    response.reply("#{input} mg/dL is #{mgdl_to_mmol(input).to_s} mmol/L")
  end
end

#convert_mmol(response) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/lita/handlers/diabetter.rb', line 56

def convert_mmol(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 + '" from mmol/L to mg/dL')
    response.reply("#{input} mmol/L is #{mmol_to_mgdl(input).to_s} mg/dL")
  end
end

#dcct_to_ifcc(n) ⇒ Object



185
186
187
# File 'lib/lita/handlers/diabetter.rb', line 185

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

#dcct_to_mgdl(n) ⇒ Object



193
194
195
# File 'lib/lita/handlers/diabetter.rb', line 193

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

#estimate_a1c(response) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/lita/handlers/diabetter.rb', line 72

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



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/lita/handlers/diabetter.rb', line 127

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)
  mmol = mgdl_to_mmol(mgdl).round(1)

  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 + mmol.to_s + ' mmol/L'
  response.reply(reply)
end

#estimate_average_from_dcct(response) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/lita/handlers/diabetter.rb', line 153

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

  mgdl = dcct_to_mgdl(dcct)
  mmol = mgdl_to_mmol(mgdl).round(1)

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

#ifcc_to_dcct(n) ⇒ Object



189
190
191
# File 'lib/lita/handlers/diabetter.rb', line 189

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

#ifcc_to_mgdl(n) ⇒ Object



197
198
199
# File 'lib/lita/handlers/diabetter.rb', line 197

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

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



117
118
119
120
121
122
123
124
125
# File 'lib/lita/handlers/diabetter.rb', line 117

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



177
178
179
# File 'lib/lita/handlers/diabetter.rb', line 177

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

#mgdl_to_ifcc(n) ⇒ Object



181
182
183
# File 'lib/lita/handlers/diabetter.rb', line 181

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

#mgdl_to_mmol(n) ⇒ Object



169
170
171
# File 'lib/lita/handlers/diabetter.rb', line 169

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

#mmol_to_mgdl(n) ⇒ Object



173
174
175
# File 'lib/lita/handlers/diabetter.rb', line 173

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