rlivsey / chav88

pull data from 118118 feed and display a random quote

This URL has Read+Write access

chav88 / application.rb
100644 36 lines (25 sloc) 0.784 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
require 'cgi'
require 'net/http'
 
class ChavSpeak
  
  def load
    @cached ||= Merb::Cache[:default].fetch("chavspeak", :interval => 300) do
      response = Net::HTTP.post_form URI.parse('http://www.text118118.com/data/livefeed_test.aspx'), {"timePerQuestion"=>"50"}
      response.body
    end
    @data = CGI.unescape(@cached.split('&').last.split('=').last).split("|").collect{ |t| t.split(";;") }.collect
  end
  
  def random
    @data[rand(@data.size)]
  end
  
end
 
 
class Chav < Merb::Controller
 
  def _template_location(action, type = nil, controller = controller_name)
    controller == "layout" ? "layout.#{action}.#{type}" : "#{action}.#{type}"
  end
 
  def index
    
    c = ChavSpeak.new
    c.load
    @question, @answer = c.random
    
    render
  end
  
end