class Ruboty::Handlers::Mtgcard

Constants

API_URL
DEFAULT_MTGCARD_LANGUAGE
DEFAULT_MTGCARD_MESSAGE_WHEN_NOT_FOUND
LANGUAGE
MESSAGE_WHEN_NOT_FOUND

Public Instance Methods

mtgcard(message) click to toggle source
# File lib/ruboty/handlers/mtgcard.rb, line 24
def mtgcard(message)
  image_url = nil

  begin
    res = open "#{api_url}&name=#{URI.escape(message[:keyword])}"
  rescue => e
    return message.reply e
  end

  cards = JSON.parse(res.read)['cards']
  return message.reply MESSAGE_WHEN_NOT_FOUND if cards.length == 0

  if LANGUAGE == 'english'
    while image_url.nil? || image_url.length == 0
      image_url = cards.sample['imageUrl']
    end
  else
    while image_url.nil? || image_url.length == 0
      card = cards.sample['foreignNames'].find { |foreig_name| foreig_name['language'] == LANGUAGE.capitalize }
      image_url = card['imageUrl']
    end
  end

  message.reply image_url
end

Private Instance Methods

api_url() click to toggle source
# File lib/ruboty/handlers/mtgcard.rb, line 52
def api_url
  url = API_URL
  url += "language=#{LANGUAGE}" if LANGUAGE != 'english'
  url
end