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.1.3'

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.



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

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.



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

def app_name
  @app_name
end

#messageObject (readonly)

Returns the value of attribute message.



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

def message
  @message
end

#passwordObject (readonly)

Returns the value of attribute password.



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

def password
  @password
end

#target_hostObject (readonly)

Returns the value of attribute target_host.



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

def target_host
  @target_host
end

#target_portObject (readonly)

Returns the value of attribute target_port.



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

def target_port
  @target_port
end

Class Method Details

.notify(params) ⇒ Object

instant notification



124
125
126
127
128
129
130
131
132
# File 'lib/ruby_gntp.rb', line 124

def self.notify(params)
  growl = GNTP.new(params[:app_name])
  notification = params
  notification[:name] = params[:app_name] || "Ruby/GNTP notification"
  growl.register(:notifications => [
    :name => notification[:name]
  ])
  growl.notify(notification)
end

Instance Method Details

#notify(params) ⇒ Object

notify



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ruby_gntp.rb', line 95

def notify(params)
  name   = params[:name]
  raise TooFewParametersError, "Notification need 'name', 'title' parameters" unless name || title

  title  = params[:title]
  text   = params[:text]
  icon   = params[:icon] || get_notification_icon(name)
  sticky = params[:sticky]

  @binaries = []

  @message = notify_header(app_name, name, title, text, sticky, icon)
  @message << output_origin_headers

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

  @message << "\r\n"

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

#register(params) ⇒ Object

register



56
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
# File 'lib/ruby_gntp.rb', line 56

def register(params)
  @notifications = params[:notifications]
  raise TooFewParametersError, "Need least one 'notification' for register" unless @notifications

  @app_icon = params[:app_icon]
  @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-Enabled: #{enabled ? 'True' : 'False'}\r\n"
    @message << "Notification-Display-Name: #{disp_name}\r\n"
    @message << "#{handle_icon(icon, 'Notification')}\r\n"    if icon
  end

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

  @message << "\r\n"

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