Module: Snarl::SNP::Action

Included in:
Snarl::SNP
Defined in:
lib/snarl/snp/action.rb

Constant Summary collapse

NOTIFICATION_PARAM_ORDER =
[:title, :text, :icon, :timeout, :class, :action, :app]

Instance Method Summary collapse

Instance Method Details

#add_class(classid, classtitle = nil) ⇒ Object

Sends a SNP command “add_class” to Snarl. For adding classid class and its classtitle nickname.

snp.add_class('green')
snp.add_class('red', 'failure popup')
classid

classname ID on the registered application

classtitle

display alias for classname, optional

Returns SNP::Response object. Before adding a class, you should register the application.

Snarl sends back a casual error when classid is already added. It is treated as SNP::SNPError::Casual::SNP_ERROR_CLASS_ALREADY_EXISTS.



34
35
36
37
38
39
40
# File 'lib/snarl/snp/action.rb', line 34

def add_class(classid, classtitle=nil)
  # TODO: add_class(app=nil, classid, classtitle=nil)
  # type=SNP#?version=1.0#?action=add_class#?class=t returns (107) Bad Packet
  raise "registering is required. #{self}#register(appname)  before #add_class" unless self['app']
  cmds = {:action => 'add_class', :app => self['app'], :class => classid.to_s, :title => classtitle}
  request(Request.new(cmds))
end

#helloObject

Sends SNP command “hello”.

irb> Snarl::SNP.new('127.0.0.1').hello
SNP/1.1/0/OK/Snarl R2.21


79
80
81
# File 'lib/snarl/snp/action.rb', line 79

def hello
  request(Request.new({:action => 'hello'}))
end

#notification(*keyhash) ⇒ Object Also known as: notify

Sends SNP command “notification” to Snarl. For making a popup message itself.

snp.notification('title', 'text', 'icon.jpg', 10, 'classA') # 10 is timeout Integer
snp.notification(:title => 't', :text => 't', :icon => 'i.jpg', :timeout => 10, :class => 'claA')
title

title of popup. String.

text

text body of popup. String. linebreaks should be only “n”. “r” confuses Snarl.

icon

icon image path of popup. String/#to_s. path on Snarl machine or http URL. bmp/jpg/png/gif.

timeout

display seconds of popup. Integer. if nil, DEFAULT_TIMEOUT 10. if 0, popup never closes automatically.

class

classid of popup. String. It should have been added by “add_class” method when you use.

notification(title, text, icon=nil, timeout=nil, classid=nil) or notification(keyword-hash).

snp.notification('title', 'text', 10)
snp.notification('title', 'text')
snp.notification('text') # title == DEFAULT_APP == 'Ruby-Snarl'


54
55
56
57
58
# File 'lib/snarl/snp/action.rb', line 54

def notification(*keyhash)
  # TODO: priority
  cmds = normalize_notification_params(keyhash)
  request(Request.new(cmds))
end

#register(app = nil) ⇒ Object Also known as: app=

Sends a SNP command “register” to Snarl. For registering app to Snarl setting window.

snp.register('Ruby-Snarl')
app

an application name. Snarl uses it as an application ID.

Snarl::SNP keeps app for add_class method and notification method. app default is SNP::DEFAULT_APP, ‘Ruby-Snarl’. Returns SNP::Response object.

Snarl sends back a casual error when app is already registered. It is treated as SNP::SNPError::Casual::SNP_ERROR_ALREADY_REGISTERED.



16
17
18
19
20
21
# File 'lib/snarl/snp/action.rb', line 16

def register(app = nil)
  # when self['app'] == nil/unset and register(nil), SNARL receives SNP_ERROR_BAD_PACKET
  self['app'] = app if app
  cmds = {:action => 'register', :app => self['app']}
  request(Request.new(cmds))
end

#unregister(app = nil) ⇒ Object

Sends SNP command “unregister” to Snarl. For removing app from Snarl setting window.

snp.unregister('Ruby-Snarl')

After this, Snarl users can not edit the settings for app ‘s popup. If you allow users to edit settings, do not send unregister. Without sending unregister, the applications are always reseted when Snarl restarts.

Snarl sends back a casual error when app is not registered. It is treated as SNP::SNPError::Casual::SNP_ERROR_NOT_REGISTERED.



69
70
71
72
73
74
# File 'lib/snarl/snp/action.rb', line 69

def unregister(app=nil)
  app = app || self['app']
  raise "#{self}#unregister requires appname." unless app
  cmds = {:action => 'unregister', :app => app}
  request(Request.new(cmds))
end

#versionObject

Sends SNP command “version”.

irb> Snarl::SNP.new('127.0.0.1').version
SNP/1.1/0/OK/40.15


86
87
88
# File 'lib/snarl/snp/action.rb', line 86

def version
  request(Request.new({:action => 'version'}))
end