Class: Snarl::SNP

Inherits:
Object
  • Object
show all
Includes:
Action, SNPProcedure
Defined in:
lib/snarl/snp/snp.rb,
lib/snarl/snp/error.rb,
lib/snarl/snp/action.rb,
lib/snarl/snp/config.rb,
lib/snarl/snp/autosnp.rb,
lib/snarl/snp/request.rb,
lib/snarl/snp/response.rb,
lib/snarl/snp/snp_procedure.rb

Defined Under Namespace

Modules: Action, AutoSNP, Error, SNPProcedure Classes: Config, Request, Response

Constant Summary collapse

DEFAULT_TIMEOUT =

default “timeout command” value. popup disappers in 10 seconds.

10
DEFAULT_TITLE =

default title for “non-title” notification

'Ruby-Snarl'

Constants included from Action

Action::NOTIFICATION_PARAM_ORDER

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SNPProcedure

#apply_yaml, #auto_add_class, #auto_notification, #auto_register, #auto_unregister, #autoexecute_with_config!, #load, #set_logger

Methods included from Action

#add_class, #hello, #notification, #register, #unregister, #version

Constructor Details

#initialize(host = nil, port = nil, verbose = false, &block) ⇒ SNP

Snarl::SNP.new(‘127.0.0.1’, 9887)



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/snarl/snp/snp.rb', line 15

def initialize(host=nil, port=nil, verbose=false, &block)
  @config = Config.new
  if host && YAML.load(host).kind_of?(Hash) then
    port.respond_to?(:debug) ? load(host, port, &block) : load(host, nil, &block)
  else
    self['host'] = host
    self['port'] = port
    self['verbose'] = verbose
    yield(self) if block_given?
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



26
27
28
# File 'lib/snarl/snp/snp.rb', line 26

def config
  @config
end

Class Method Details

.load(*args, &block) ⇒ Object



101
102
103
# File 'lib/snarl/snp/snp_procedure.rb', line 101

def self.load(*args, &block)
  self.new.load(*args, &block)
end

.open(host = nil, port = nil, verbose = false) {|client| ... } ⇒ Object

Snarl::SNP.open(host, port){|snp| snp.register … } “ensure block” is empty. TCPSocket is closed per access.

Yields:

  • (client)


112
113
114
115
116
# File 'lib/snarl/snp/snp.rb', line 112

def self.open(host=nil, port=nil, verbose=false, &block)
  client = new(host, port, verbose)
  yield(client) # socket always closed in TCPSocket#open{...}
  client
end

.ping(host = nil, port = nil) ⇒ Object



131
132
133
# File 'lib/snarl/snp/snp.rb', line 131

def self.ping(host=nil, port=nil)
  new(host, port).ping
end

.show_message(host, port, title = nil, text = nil, timeout = nil, icon = nil) ⇒ Object

send message only. app and class is “anonymous”.

Snarl::SNP.show_message(host, 9887, title, text, timeout, icon)
Snarl::SNP.show_message(host, title, text, timeout, icon)
Snarl::SNP.show_message(host, title, text)
Snarl::SNP.show_message(host, text)


123
124
125
126
127
128
129
# File 'lib/snarl/snp/snp.rb', line 123

def self.show_message(host, port, title=nil, text=nil, timeout=nil, icon=nil)
  # TODO: (host, title, text, 10)
  if port.kind_of?(String) && icon.nil? then
    port, title, text, timeout, icon = nil, port, title, text, timeout
  end
  new(host, port).notification(:title => title, :text => text, :timeout => timeout, :icon => icon)
end

Instance Method Details

#[](k) ⇒ Object



29
# File 'lib/snarl/snp/snp.rb', line 29

def [](k) ; @config[k] ; end

#[]=(k, v) ⇒ Object



28
# File 'lib/snarl/snp/snp.rb', line 28

def []=(k, v) ; @config[k] =v ; end

#add_classes(*classes) ⇒ Object

add_classes(‘type1’, ‘type2’, ‘type3’) add_classes([‘type1’, desc1], [‘type2’, desc2], [‘type3’, desc3]) add_classes(*array_of_pairs) returns [add_class_response1, add_class_response2, add_class_response3]



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

def add_classes(*classes)
  classes.map{|e| e.kind_of?(String) ? [e, nil] : e}.map{|p| add_class(*p)}
end

#icon(s) ⇒ Object

returns icon path if SNP knows. optional.



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

def icon(s)
  if self['iconset'] && self['iconset'].has_key?(s) then self['iconset'][s] else s end
end

#iconset(icons) ⇒ Object Also known as: icons

set icons pair. quite optional.

snp.iconset({:red => 'red.jpg'})
snp.notification('title', 'text', :red) #=> sends "icon=red.jpg"
snp.notification('title', 'text', 'blue') #=> sends "icon=blue"


93
# File 'lib/snarl/snp/snp.rb', line 93

def iconset(icons) ; self['iconset'] = icons ; end

#loggerObject



48
# File 'lib/snarl/snp/snp.rb', line 48

def logger ; self['logger'] ; end

#logger=(logger) ⇒ Object

set Logger object. It is used when sending request and getting response.



45
46
47
# File 'lib/snarl/snp/snp.rb', line 45

def logger=(logger)
  self['logger'] = (logger.kind_of?(Class) ? logger.new($stdout) : logger)
end

#pingObject



96
97
98
# File 'lib/snarl/snp/snp.rb', line 96

def ping
  notification(DEFAULT_TITLE, 'Ruby Snarl-SNP Ping Message', 3, nil)
end

#request(req) ⇒ Object

send Snarl::SNP::Request/Hash/String and get Snarl::SNP::Response fron Snarl. When the response “fatal” response, raise errors. When method verbose returns true, “casual” errors also raises.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/snarl/snp/snp.rb', line 53

def request(req)
  req = Request.new(req) if req.kind_of?(Hash)
  action = if req.kind_of?(Request) then req.action else '(string)' end
  debug("#{action}: #{req.inspect}")
  begin
    res = get_response(req)
    info("#{action}: #{res.inspect}")
  rescue Error::Casual => ex
    info("#{action}: (ignored) #{ex.message}")
    raise if verbose
    res = ex.response
  rescue Error::Fatal => ex
    error("#{action}: #{ex.message}")
    raise
  rescue Errno::ECONNREFUSED => ex
    error("#{ex.message} / #{self['host'].inspect}:#{self['port'].inspect}")
    raise
  end
  return res
end

#snarl_helloObject



102
103
104
# File 'lib/snarl/snp/snp.rb', line 102

def snarl_hello
  hello.infomation
end

#snarl_versionObject



106
107
108
# File 'lib/snarl/snp/snp.rb', line 106

def snarl_version
  version.infomation
end

#verboseObject



34
# File 'lib/snarl/snp/snp.rb', line 34

def verbose ; self['verbose'] ; end

#verbose=(v) ⇒ Object

When you set it true, all unimportant SNP errors raise. Default is false, Snarl::SNP::Error::Casual are disabled.



33
# File 'lib/snarl/snp/snp.rb', line 33

def verbose=(v) ; self['verbose'] = v ; end