Class: Pubsub::Notification

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Notification

Returns a new instance of Notification.



10
11
12
13
14
15
16
17
18
# File 'lib/pubsub.rb', line 10

def initialize(data)
  #data must be an hash object
  #example {data: {message: "testing one two three", image: "htp://example.com/xyz/abc.png"}}
  
  if data.nil? 
    raise "Data packet is missing."
  end
  @json_data = JSON.pretty_generate(data.merge(to: "/topics/#{PUBSUB_TOPIC}"))
end

Instance Attribute Details

#json_dataObject

Returns the value of attribute json_data.



8
9
10
# File 'lib/pubsub.rb', line 8

def json_data
  @json_data
end

Instance Method Details

#broadcastObject



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

def broadcast
  begin
    req = Curl::Easy.http_post("https://gcm-http.googleapis.com/gcm/send", @json_data) do |curl|
      curl.headers["Content-Type"] = "application/json"
      curl.headers["Authorization"] = "key=#{GCM_API_KEY}"
    end
  rescue Exception => e
    puts ("======================")
    puts  e.message
    #error log here
    raise e.message
  end
  #call below function if you need to log notification response 
  # record_notification_logs(req)

  return JSON.parse req.body_str
end

#record_notification_logs(req) ⇒ Object



38
39
40
41
# File 'lib/pubsub.rb', line 38

def record_notification_logs(req)
  #put your code here to record notification response
  puts  JSON.parse req.body_str
end