class Lita::Handlers::RetroText

Public Instance Methods

create(response) click to toggle source

Creates the text

# File lib/lita/handlers/retrotext.rb, line 34
def create(response)
  top = response.matches.first[0].tr("_", " ")
  middle = response.matches.first[1].tr("_", " ")
  bottom = response.matches.first[2].tr("_", " ")
  server = rand(9) + 1
  uri_prefix = "http://photofunia.com"
  uri_string = "#{uri_prefix}/categories/all_effects/retro-wave?server=#{server}"

  res = nil
  tries = 0

  loop do
      uri = URI.parse(uri_string)
      res = Net::HTTP.post_form(
          uri,
          "current-category" => "all_effects",
          "bcg" => rand(4) + 1,
          "txt" => rand(3) + 1,
          "text1" => top,
          "text2" => middle,
          "text3" => bottom
      )
      uri_string = uri_prefix + res['location'] if res['location']
      unless res.kind_of? Net::HTTPRedirection
        res.ensure_success
        break
      end
      if tries == 10
        puts "Timing out after 10 tries"
        break
      end
      tries += 1
    end

  url = extract_url(res.body)
  response.reply("#{url}")
end
extract_url(body) click to toggle source
# File lib/lita/handlers/retrotext.rb, line 72
def extract_url(body)
  doc = Nokogiri::HTML(body)
  a = doc.xpath("//a")
  url = ""
  for i in a do
    text = i.text.delete(" ").strip
    if text == "Large"
      url = i['href'].split("?")[0]
    end
  end

  url
end