Module: NotifyPush::Receiver

Defined in:
lib/notify-push.rb

Overview


MODULE->RECEIVER —————————–


Class Method Summary collapse

Class Method Details

.ensure_compatibilityObject



117
118
119
# File 'lib/notify-push.rb', line 117

def self.ensure_compatibility
  OS.mac? or "The notify-push receiver only supports OS X."
end

.ensure_dependenciesObject


ENSURANCES ———————————




113
114
115
# File 'lib/notify-push.rb', line 113

def self.ensure_dependencies
  system "command -v terminal-notifier >/dev/null 2>&1" or raise "'terminal-notifier' cannot be found."
end

.notify(title: "notify-push", subtitle: nil, message:) ⇒ Object


NOTIFY ————————————-




124
125
126
127
128
129
130
131
132
133
134
# File 'lib/notify-push.rb', line 124

def self.notify(title: "notify-push", subtitle: nil, message:)

  args = [
    "-message", message,
    "-title",   title
  ]

  args.concat ["-subtitle", subtitle] unless subtitle.blank?

  system "terminal-notifier", *args
end

.pid_lockObject


PID —————————————-




104
105
106
107
108
# File 'lib/notify-push.rb', line 104

def self.pid_lock
  require "pidfile"

  PidFile.new
end

.startObject


START ————————————–




139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/notify-push.rb', line 139

def self.start()

  ensure_compatibility
  ensure_dependencies
  pid_lock

  require "pusher-client"

  socket = PusherClient::Socket.new configuration.pusher.key, {
    secure: true
  }

  # Subscribe to main channel
  socket.subscribe(CHANNEL_NAME)

  # Bind to the main channel event 
  socket[CHANNEL_NAME].bind("notification") do |data|

    begin
      data = JSON.parse(data, symbolize_names: true)
      
      notify **data

    rescue => exception
      puts "Warning: Failed to process notification."
      puts exception
    ensure
      puts "----------"
      puts data
    end
  end

  # Bind to the error event
  socket.bind("pusher:error") do |data|
    puts "----------"
    puts "Warning: Pusher Error"
    puts data
  end

  # Connect
  socket.connect

  0
end