Class: Lita::Handlers::Snoo

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

Instance Method Summary collapse

Instance Method Details

#ambient_url(response) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/lita/handlers/snoo.rb', line 15

def ambient_url(response)
  url = response.matches.first.first.split("#").first
  hostname = URI.parse(url).hostname
  return if hostname.nil?
  if config.domains.any? { |d| hostname.include? d }
    post = api_search("url:'#{url}'")
    if response.message.command?
      response.reply post ? post : "No reddit posts found for #{url}"
    else
      response.reply post if post
    end
  end
end

#subreddit(response) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/lita/handlers/snoo.rb', line 37

def subreddit(response)
  subreddit = response.matches.first.first
  arg = response.matches.first[1]
  if arg.length > 0
    if /^\d+$/ =~ arg
      n = arg.to_i
      if n.between?(1, 25)
        response.reply api_subreddit(subreddit, n)
      else
        response.reply "Please specify a number between 1 and 25"
      end
    else
      response.reply api_subreddit_search(subreddit, arg)
    end
  else
    response.reply api_subreddit(subreddit, rand(1..25))
  end
end

#url(response) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/lita/handlers/snoo.rb', line 29

def url(response)
  url = response.matches.first.first.split("#").first
  unless config.domains.any? { |d| URI.parse(url).hostname.include? d }
    post = api_search("url:'#{url}'")
    response.reply post ? post : "No reddit posts found for #{url}"
  end
end