Class: Notifier::Adapters::GNTP
- Inherits:
-
Object
- Object
- Notifier::Adapters::GNTP
- Defined in:
- lib/notifier/adapters/gntp.rb
Instance Attribute Summary collapse
-
#application_name ⇒ Object
Returns the value of attribute application_name.
-
#host ⇒ Object
Returns the value of attribute host.
-
#password ⇒ Object
Returns the value of attribute password.
-
#port ⇒ Object
Returns the value of attribute port.
Instance Method Summary collapse
- #bool(boolean) ⇒ Object
- #fetch_icon(path) ⇒ Object
-
#initialize(options = {}) ⇒ GNTP
constructor
A new instance of GNTP.
- #line_break(number = 1) ⇒ Object
- #notify(options) ⇒ Object
- #register(name) ⇒ Object
- #write(*contents) ⇒ Object
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( = {}) if .kind_of?(String) = {name: } end @application_name = .fetch(:name, "GNTP/Ruby") @host = .fetch(:host, "127.0.0.1") @port = .fetch(:port, 23053) @password = .fetch(:password, nil) end |
Instance Attribute Details
#application_name ⇒ Object
Returns the value of attribute application_name.
4 5 6 |
# File 'lib/notifier/adapters/gntp.rb', line 4 def application_name @application_name end |
#host ⇒ Object
Returns the value of attribute host.
5 6 7 |
# File 'lib/notifier/adapters/gntp.rb', line 5 def host @host end |
#password ⇒ Object
Returns the value of attribute password.
7 8 9 |
# File 'lib/notifier/adapters/gntp.rb', line 7 def password @password end |
#port ⇒ Object
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() name = .fetch(:name, "notification") register(name) icon = fetch_icon([:icon]) write "GNTP/1.0 NOTIFY NONE", "Application-Name: #{application_name}", "Notification-Name: #{name}", "Notification-Title: #{[:title]}", "Notification-Text: #{[:message]}", "Notification-Icon: x-growl-resource://#{icon[:identifier]}", "Notification-Sticky: #{bool [: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) = [*contents, line_break(2)].join(line_break) socket.write() "".tap do |response| while chunk = socket.gets break if chunk == line_break response << chunk end socket.close end end |