Class: GNTP

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_gntp.rb

Constant Summary collapse

RUBY_GNTP_NAME =
'ruby_gntp'
RUBY_GNTP_VERSION =
'0.3.4'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_name = 'Ruby/GNTP', host = 'localhost', password = '', port = 23053) ⇒ GNTP

Returns a new instance of GNTP.



47
48
49
50
51
52
# File 'lib/ruby_gntp.rb', line 47

def initialize(app_name = 'Ruby/GNTP', host = 'localhost', password = '', port = 23053)
  @app_name     = app_name
  @target_host  = host
  @target_port  = port
  @password     = password
end

Instance Attribute Details

#app_nameObject (readonly)

Returns the value of attribute app_name.



41
42
43
# File 'lib/ruby_gntp.rb', line 41

def app_name
  @app_name
end

#messageObject (readonly)

Returns the value of attribute message.



42
43
44
# File 'lib/ruby_gntp.rb', line 42

def message
  @message
end

#target_hostObject (readonly)

Returns the value of attribute target_host.



41
42
43
# File 'lib/ruby_gntp.rb', line 41

def target_host
  @target_host
end

#target_portObject (readonly)

Returns the value of attribute target_port.



41
42
43
# File 'lib/ruby_gntp.rb', line 41

def target_port
  @target_port
end

Class Method Details

.notify(params, &callback) ⇒ Object

instant notification



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/ruby_gntp.rb', line 133

def self.notify(params, &callback)
  host    = params[:host]
  passwd  = params[:passwd]

  growl = GNTP.new(params[:app_name], host, passwd)

  notification = params
  notification[:name] = params[:app_name] || "Ruby/GNTP notification"
  growl.register(:notifications => [
    :name => notification[:name]
  ])
  growl.notify(notification, &callback)
end

Instance Method Details

#notify(params, &callback) ⇒ Object

notify



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ruby_gntp.rb', line 98

def notify(params, &callback)
  name   = params[:name]
  title  = params[:title]
  text   = params[:text]
  icon   = params[:icon] || get_notification_icon(name)
  sticky = params[:sticky]
  callback_context = params[:callback_context]
  callback_context_type = params[:callback_context_type]

  raise TooFewParametersError, "Notification need 'name', 'title' parameters" unless name || title

  @binaries = []

  message = notify_header(app_name, name, title, text, sticky, icon)
  message << output_origin_headers
  if callback || callback_context
    message << "Notification-Callback-Context: #{callback_context || '(none)'}\r\n"
    message << "Notification-Callback-Context-Type: #{callback_context_type || '(none)'}\r\n"
  end

  @binaries.each {|binary|
    message << output_binary(binary)
  }

  message << "\r\n"

  unless (ret = send_and_recieve(message, callback))
    raise "Notify failed"
  end
end

#register(params) ⇒ Object

register



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ruby_gntp.rb', line 57

def register(params)
  @notifications = params[:notifications]
  @app_icon = params[:app_icon]

  raise TooFewParametersError, "Need least one 'notification' for register" unless @notifications

  @binaries = []

  message = register_header(@app_name, @app_icon)
  message << output_origin_headers

  message << "Notifications-Count: #{@notifications.size}\r\n"
  message << "\r\n"

  @notifications.each do |notification|
    name      = notification[:name]
    disp_name = notification[:disp_name] || name
    enabled   = notification[:enabled] || true
    icon      = notification[:icon]

    message << "Notification-Name: #{name}\r\n"
    message << "Notification-Display-Name: #{disp_name}\r\n"
    message << "Notification-Enabled: #{enabled ? 'True' : 'False'}\r\n"
    message << "#{handle_icon(icon, 'Notification')}\r\n"    if icon
    message << "\r\n"
  end

  @binaries.each {|binary|
    message << output_binary(binary)
    message << "\r\n"
  }


  unless (ret = send_and_recieve(message))
    raise "Register failed"
  end
end