-- This is a proprietary plugin, property of Andreas Bielawski, (c) 2015 <andi (dot) b (at) outlook (dot) de>
-- DO NOT USE WITHOUT PERMISSION

do

local BASE_URL = 'https://gender-api.com/get'

function get_gender_data (name)
  local apikey = cred_data.gender_apikey
  local url = BASE_URL..'?name='..name..'&key='..apikey
  local res,code  = https.request(url)
  if code ~= 200 then return "HTTP-FEHLER" end
  local data = json:decode(res)
  return data
end

function send_gender_data(data, receiver)
  if data.gender == "female" then
    gender = 'weiblich'
  end
  if data.gender == "male" then
    gender = 'männlich'
  end
  if data.gender == "unknown" then
    gender = 'unbekanntem Geschlechts'
  end
  local accuracy = data.accuracy
  local text = name..' ist zu '..accuracy..'% '..gender
  send_msg(receiver, text, ok_cb, false)
end

function run(msg, matches)
  name = matches[1]
  local data = get_gender_data(name)
  local receiver = get_receiver(msg)
  send_gender_data(data, receiver)
end

return {
  description = "Sendet Geschlecht", 
  usage = {"/geschlecht [Name]","/gender [Name]"},
  patterns = {"^/geschlecht (.*)$","^/gender (.*)$"},
  run = run 
}

end
