Class: Notifier::Adapters::GNTP

Inherits:
Object
  • Object
show all
Defined in:
lib/notifier/adapters/gntp.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ GNTP

Returns a new instance of GNTP.


9
10
11
12
13
14
15
16
17
18
# File 'lib/notifier/adapters/gntp.rb', line 9

def initialize(options = {})
  if options.kind_of?(String)
    options = {name: options}
  end

  @application_name = options.fetch(:name, "GNTP/Ruby")
  @host = options.fetch(:host, "127.0.0.1")
  @port = options.fetch(:port, 23053)
  @password = options.fetch(:password, nil)
end

Instance Attribute Details

#application_nameObject

Returns the value of attribute application_name


4
5
6
# File 'lib/notifier/adapters/gntp.rb', line 4

def application_name
  @application_name
end

#hostObject

Returns the value of attribute host


5
6
7
# File 'lib/notifier/adapters/gntp.rb', line 5

def host
  @host
end

#passwordObject

Returns the value of attribute password


7
8
9
# File 'lib/notifier/adapters/gntp.rb', line 7

def password
  @password
end

#portObject

Returns the value of attribute port


6
7
8
# File 'lib/notifier/adapters/gntp.rb', line 6

def port
  @port
end

Instance Method Details

#bool(boolean) ⇒ Object


73
74
75
# File 'lib/notifier/adapters/gntp.rb', line 73

def bool(boolean)
  {true => "Yes", false => "No"}[boolean]
end

#fetch_icon(path) ⇒ Object


63
64
65
66
67
68
69
70
71
# File 'lib/notifier/adapters/gntp.rb', line 63

def fetch_icon(path)
  contents = File.open(path, "rb") {|f| f.read }

  {
    identifier: Digest::MD5.hexdigest(contents),
    contents: contents,
    size: contents.bytesize
  }
end

#line_break(number = 1) ⇒ Object


20
21
22
# File 'lib/notifier/adapters/gntp.rb', line 20

def line_break(number = 1)
  "\r\n" * number
end

#notify(options) ⇒ Object


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/notifier/adapters/gntp.rb', line 43

def notify(options)
  name = options.fetch(:name, "notification")
  register(name)

  icon = fetch_icon(options[:icon])

  write "GNTP/1.0 NOTIFY NONE",
        "Application-Name: #{application_name}",
        "Notification-Name: #{name}",
        "Notification-Title: #{options[:title]}",
        "Notification-Text: #{options[:message]}",
        "Notification-Icon: x-growl-resource://#{icon[:identifier]}",
        "Notification-Sticky: #{bool options[:sticky]}",
        nil,
        "Identifier: #{icon[:identifier]}",
        "Length: #{icon[:size]}",
        nil,
        icon[:contents]
end

#register(name) ⇒ Object


77
78
79
80
81
82
83
84
# File 'lib/notifier/adapters/gntp.rb', line 77

def register(name)
  write "GNTP/1.0 REGISTER NONE",
        "Application-Name: #{application_name}",
        "Notifications-count: 1",
        nil,
        "Notification-Name: #{name}",
        "Notification-Enabled: True"
end

#write(*contents) ⇒ Object


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/notifier/adapters/gntp.rb', line 24

def write(*contents)
  contents.map! do |content|
    content.force_encoding("utf-8") rescue content
  end

  socket = TCPSocket.open(host, port)
  message = [*contents, line_break(2)].join(line_break)
  socket.write(message)

  "".tap do |response|
    while chunk = socket.gets
      break if chunk == line_break
      response << chunk
    end

    socket.close
  end
end