Module: NotifyPush::Receiver

Defined in:
lib/notify-push/receiver.rb

Overview


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


Class Method Summary collapse

Class Method Details

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


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




31
32
33
34
35
36
37
38
39
40
# File 'lib/notify-push/receiver.rb', line 31

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

  message = "#{subtitle} - #{message}" if subtitle

  Notifier.notify({
    title:   title,
    message: message,
    sound:   sound
  })
end

.on_at_exitObject



15
16
17
# File 'lib/notify-push/receiver.rb', line 15

def self.on_at_exit
  notify title: "notify-push", message: "Receiver: Exiting.", sound: :Submarine
end

.pid_lockObject


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




22
23
24
25
26
# File 'lib/notify-push/receiver.rb', line 22

def self.pid_lock
  require "pidfile"

  PidFile.new
end

.startObject


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




45
46
47
48
49
50
51
52
53
54
55
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
99
100
101
102
103
104
# File 'lib/notify-push/receiver.rb', line 45

def self.start()

  pid_lock

  ::NotifyPush.acting_as = self

  require "pusher-client"

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

  # Subscribe to main channel
  # ------------------------------------------
  socket.subscribe(CHANNEL_NAME)

  # Bind to: Main Channel Notification
  # ------------------------------------------
  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 "-> Received Push Notification at: #{Time.now.to_s}"
      puts "--------------------------------------------------"
      ap data
    end
  end

  # Bind to: Pusher Errors
  # ------------------------------------------
  socket.bind("pusher:error") do |data|
    puts "----------"
    puts "Warning: Pusher Error"
    puts data
  end
 
  # Bind to: Pusher Connection Established
  # ------------------------------------------
  socket.bind("pusher:connection_established") do |data|
    self.notify({
      title:   "notify-push",
      message: "Receiver: Started & connected.",
      sound: :default
    })
  end

  # Connect the Socket
  # ------------------------------------------
  socket.connect

  0
end