Class: Lita::Handlers::RetroText

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/handlers/retrotext.rb

Instance Method Summary collapse

Instance Method Details

#create(response) ⇒ Object

Creates the text



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# 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