Class: Daneel::Scripts::VineSearch

Inherits:
Daneel::Script show all
Defined in:
lib/daneel/scripts/vine_search.rb

Instance Attribute Summary

Attributes inherited from Plugin

#robot

Instance Method Summary collapse

Methods inherited from Daneel::Script

#accepts?, files, inherited, list

Methods inherited from Plugin

#logger, requires_env

Constructor Details

#initialize(robot) ⇒ VineSearch

Returns a new instance of VineSearch.



9
10
11
12
# File 'lib/daneel/scripts/vine_search.rb', line 9

def initialize(robot)
  super
  @http = Net::HTTP::Persistent.new('daneel')
end

Instance Method Details

#find_vine_url_for(search) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/daneel/scripts/vine_search.rb', line 31

def find_vine_url_for(search)
  logger.debug "Searching for a vine of #{search}"
  uri = URI("http://search.twitter.com/search.json")
  query = CGI.escape("#{search} source:vine_for_ios")
  uri.query = "rpp=5&include_entities=true&q=#{query}"
  request = Net::HTTP::Get.new(uri.request_uri)
  response = @http.request uri, request
  logger.debug "GET #{uri}"
  results = JSON.parse(response.body)["results"]
  logger.debug "got back #{results.size} vines"
  return nil if results.empty?
  # Pull out the Vine display url of a random result
  results.sample["entities"]["urls"].first["expanded_url"]
rescue => e
  logger.error "#{e.class}: #{e.message}"
  room.say "Sorry, something went wrong when I looked for '#{query}'"
end

#helpObject



27
28
29
# File 'lib/daneel/scripts/vine_search.rb', line 27

def help
  {"find a vine of THING" => "shows you a vine that was tweeted mentioning THING"}
end

#receive(room, message, user) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/daneel/scripts/vine_search.rb', line 14

def receive(room, message, user)
  case message.command
  when /vine me (.+)$/, /^(?:find) (?:me )?(?:a |another )?(?:vine of )(.*)$/
    url = find_vine_url_for($1)
    if url
      room.say url
    else
      room.say "Sorry, Twitter didn't have any Vines about that."
    end
    message.done!
  end
end