class Ruboty::Handlers::Zoi

Constants

ZOI_DATA_URI

Public Instance Methods

zoi(message) click to toggle source
# File lib/ruboty/zoi.rb, line 17
def zoi(message)
  keyword = message.match_data[1]
  if keyword == "list"
    message.reply(fetch_data.map {|z| z["word"] }.uniq.sort.join(", "))
  else
    message.reply(find_zoi_by_keyword(keyword)["image"])
  end
end

Private Instance Methods

fetch_data() click to toggle source
# File lib/ruboty/zoi.rb, line 36
def fetch_data
  return @fetched_data if @fetched_data
  zoi_data = open(ZOI_DATA_URI).read
  zoi_data = zoi_data.
    match(/this.items = (.+?);/m)[1].
    gsub(/(word|image|src):/, "'\\1':").
    gsub("'", '"')
  @fetched_data = JSON.parse(zoi_data)
end
find_zoi_by_keyword(keyword) click to toggle source
# File lib/ruboty/zoi.rb, line 27
def find_zoi_by_keyword(keyword)
  data = fetch_data
  if keyword
    entry = data.select { |z| z["word"].include?(keyword) }.sample
  end

  entry || data.sample
end