class Lita::Handlers::Diabetter

Public Instance Methods

convert(response) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/lita/handlers/diabetter.rb, line 193
def dcct_to_mgdl(n)
  (n.to_f * 28.7) - 46.7
end
estimate_a1c(response) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/lita/handlers/diabetter.rb, line 173
def mmol_to_mgdl(n)
  (n.to_f * @@conversion_ratio).round
end