Class: CeilingCat::Plugin::Notifo

Inherits:
Base
  • Object
show all
Defined in:
lib/ceiling_cat/plugins/notifo.rb

Instance Attribute Summary

Attributes inherited from Base

#event

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#body, #body_without_command, #body_without_nick, #body_without_nick_or_command, #commands, #handle, #initialize, #pluralize, public?, #reply, #room, store, #store, #user, #words

Constructor Details

This class inherits a constructor from CeilingCat::Plugin::Base

Class Method Details

.active?Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
# File 'lib/ceiling_cat/plugins/notifo.rb', line 36

def self.active?
  if store["notifo_credentials"] && store["notifo_credentials"][:username].present? && store["notifo_credentials"][:api_secret].present?
    true
  else
    false
  end
end

.add_users(users) ⇒ Object



56
57
58
59
# File 'lib/ceiling_cat/plugins/notifo.rb', line 56

def self.add_users(users)
  store["notifo_users"] ||= []
  store["notifo_users"] = (Array(store["notifo_users"]) + Array(users)).uniq
end

.commandsObject



8
9
10
11
12
13
# File 'lib/ceiling_cat/plugins/notifo.rb', line 8

def self.commands
  [{:command => "notifo", :description => "Send a message with Notifo - '!notifo user: Hey, get in here!'. 'user:' is optional, and will go to everyone if not passed.", :method => "deliver"},
   {:command => "add notifo users", :description => "Add users to get Notifos - '!add notifo users username1 username2'.", :method => "add_users"},
   {:command => "remove notifo users", :description => "Add users to get Notifos - '!remove notifo users username1 username2'.", :method => "remove_users"},
   {:command => "list notifo users", :description => "List users who get Notifos - '!list notifo users'.", :method => "list_users"}]
end

.deliver(user, message) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/ceiling_cat/plugins/notifo.rb', line 26

def self.deliver(user,message)
  if active?
    HTTParty.post("https://api.notifo.com/v1/send_notification",
                    :body => { :to => user, :msg => message },
                    :basic_auth => {:username => store["notifo_credentials"][:username], :password => store["notifo_credentials"][:api_secret]})
  else
    raise NotifoNotConfiguredError
  end
end

.descriptionObject



52
53
54
# File 'lib/ceiling_cat/plugins/notifo.rb', line 52

def self.description
  "Sends messages to users with Notifo"
end

.nameObject



48
49
50
# File 'lib/ceiling_cat/plugins/notifo.rb', line 48

def self.name
  "Notifo"
end

.remove_users(users) ⇒ Object



61
62
63
64
# File 'lib/ceiling_cat/plugins/notifo.rb', line 61

def self.remove_users(users)
  store["notifo_users"] ||= []
  store["notifo_users"] = (Array(store["notifo_users"]) - Array(users)).uniq
end

.set_credentials(username, api_secret) ⇒ Object



66
67
68
# File 'lib/ceiling_cat/plugins/notifo.rb', line 66

def self.set_credentials(username, api_secret)
  store["notifo_credentials"] = {:username => username, :api_secret => api_secret}
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/ceiling_cat/plugins/notifo.rb', line 44

def active?
  self.class.active?
end

#add_usersObject



70
71
72
73
74
# File 'lib/ceiling_cat/plugins/notifo.rb', line 70

def add_users
  users = body_without_nick_or_command("add notifo users").split(" ")
  self.class.add_users(users)
  reply "#{users.join(" ")} added to notifo alerts."
end

#deliver(full_message = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/ceiling_cat/plugins/notifo.rb', line 15

def deliver(full_message=nil)
  body_parts = (full_message || body_without_nick_or_command("notifo")).scan(/^((\w+):)?(.+)$/)[0]
  message = body_parts[2].strip
  user = body_parts[1]

  users = user ? Array(user.strip) : Array(store["notifo_users"])
  users.each do |user|
    CeilingCat::Plugin::Notifo.deliver(user,message)
  end
end

#list_usersObject



82
83
84
85
86
87
# File 'lib/ceiling_cat/plugins/notifo.rb', line 82

def list_users
  messages = []
  messages << "Notifo Users"
  Array(store["notifo_users"]).each{|user| messages << "-- #{user}"}
  reply messages
end

#remove_usersObject



76
77
78
79
80
# File 'lib/ceiling_cat/plugins/notifo.rb', line 76

def remove_users
  users = body_without_nick_or_command("remove notifo users").split(" ")
  self.class.remove_users(users)
  reply "#{users.join(" ")} removed from notifo alerts."
end