Class: Pictor::Client

Inherits:
Object
  • Object
show all
Includes:
Blather::DSL
Defined in:
lib/pictor/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#jidObject

Returns the value of attribute jid.



14
15
16
# File 'lib/pictor/client.rb', line 14

def jid
  @jid
end

#keyObject

Returns the value of attribute key.



14
15
16
# File 'lib/pictor/client.rb', line 14

def key
  @key
end

#nickObject

Returns the value of attribute nick.



14
15
16
# File 'lib/pictor/client.rb', line 14

def nick
  @nick
end

#passObject

Returns the value of attribute pass.



14
15
16
# File 'lib/pictor/client.rb', line 14

def pass
  @pass
end

#roomObject

Returns the value of attribute room.



14
15
16
# File 'lib/pictor/client.rb', line 14

def room
  @room
end

Instance Method Details

#runObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/pictor/client.rb', line 16

def run
  setup @jid, @pass

  # Join the MUC Chat room after connecting.
  when_ready do
    puts "Connected!"
    p = Blather::Stanza::Presence.new
    p.from = @jid
    p.to = "#{@room}/#{@nick}"
    p << "<x xmlns='http://jabber.org/protocol/muc'/>"
    client.write p
  end

  message :groupchat?, :body => /^Pictor:/ do |m|
    puts "From: #{m.from}"
    rxp = Regexp.new('Pictor: (.*)', 'i').match(m.body)
    query = rxp[1].blank? ? 'unicorn' : rxp[1]
    puts "Searching: #{query}"
    http = EventMachine::HttpRequest.new('http://ajax.googleapis.com/ajax/services/search/images').get(:query => {'key' => @key, 'v' => '1.0', 'q' => query}, :timeout => 10)
    http.errback { puts 'Search Failed' }
    http.callback {
      r = Yajl::Parser.parse(http.response)
      srand
      url = r['responseData']['results'].sample['unescapedUrl'] + "#.png"
      puts "Returning: #{url}"
      m = Blather::Stanza::Message.new
      m.to = @room
      m.type = :groupchat
      m.body = url
      client.write m
    }
  end

  client.run
end