Class: Rowl::Registration

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(application, notifications, host = "localhost", password = nil) ⇒ Registration

Returns a new instance of Registration.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rowl.rb', line 21

def initialize(application, notifications, host="localhost", password=nil)      
  @defaults = []
  @application = application
  @notifications = []
  @password = password
  @host = host

  # Register notifications
  notifications.each do |notification|
    add_notification_type(notification[:name], notification[:enabled])
  end
  
  send_registration
end

Instance Attribute Details

#applicationObject

Returns the value of attribute application.



19
20
21
# File 'lib/rowl.rb', line 19

def application
  @application
end

#hostObject

Returns the value of attribute host.



19
20
21
# File 'lib/rowl.rb', line 19

def host
  @host
end

#notificationsObject

Returns the value of attribute notifications.



19
20
21
# File 'lib/rowl.rb', line 19

def notifications
  @notifications
end

#passwordObject

Returns the value of attribute password.



19
20
21
# File 'lib/rowl.rb', line 19

def password
  @password
end

Instance Method Details

#add_notification_type(notification, enabled) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/rowl.rb', line 41

def add_notification_type(notification, enabled)
  if enabled.nil?
    enabled = true
  end
  @notifications << notification
  if enabled
    @defaults << @notifications.length - 1
  end
end

#app_nameObject



36
37
38
39
# File 'lib/rowl.rb', line 36

def app_name
  chars = ("a".."z").to_a + ("1".."9").to_a 
  Array.new(8, '').collect{chars[rand(chars.size)]}.join
end

#payloadObject



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
91
92
93
94
95
96
97
98
# File 'lib/rowl.rb', line 56

def payload
  length = 0
  data = []
  data_format = ""

  packet = [
    GROWL_PROTOCOL_VERSION, 
    GROWL_TYPE_REGISTRATION
  ]

  packet << @application.length
  packet << @notifications.length
  packet << @defaults.length

  data << @application
  data_format = "a#{@application.length}"

  @notifications.each do |notify|
    data << notify.length
    data << notify
    data_format << "na#{notify.length}"
  end

  @defaults.each do |notify|
    data << notify
    data_format << "C"
  end

  data = data.pack(data_format)

  packet << data
  packet = packet.pack(REGISTRATION_FORMAT)
      
  if @password
    checksum = Digest::MD5.digest(packet+@password)
  else
    checksum = Digest::MD5.digest(packet)
  end

  packet << checksum

  return packet
end

#send_registrationObject



51
52
53
54
# File 'lib/rowl.rb', line 51

def send_registration
  socket = UDPSocket.open
  socket.send(payload, 0, @host, 9887)
end