Class: Sanjose::Notification

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Notification

Returns a new instance of Notification.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sanjose/notification.rb', line 9

def initialize(options = {})
  # Required field
  # A string array with the list of devices (registration IDs) receiving 
  # the message. It must contain at least 1 and at most 1000 registration IDs.
  if !options[:devices]
    @devices = []
  else
    @devices = options[:devices]
  end
  
  # Optional fields
  # An arbitraru string (such as "Updates Available") that is used to 
  # collapse a group of like messages when the device is offline, so 
  # that only the last message gets sent to the client.
  @collapse_key = options[:collapse_key] 
  
  # If included, indicates that the message should not be sent immediately 
  # if the device is idle. 
  @delay_when_idle = options[:delay_when_idle]
  
  # How long (in seconds) the message should be kept on GCM storage if the 
  # device is offline. 
  @time_to_live = options[:time_to_live]
  
  # If included, allows developers to test their request without actually 
  # sending a message. Optional. The default value is false, and must be a 
  # JSON boolean.      
  @dry_run = options[:dry_run]
  
  # A string containing the package name of your application. When set, 
  # messages will only be sent to registration IDs that match the package 
  # name. 
  @restricted_package_name = options[:restricted_package_name]
  
  # A JSON object whose fields represents the key-value pairs of the message's 
  # payload data. There is no limit on the number of key/value pairs, though 
  # there is a limit on the total size of the message (4kb).
  @data = options[:data]
end

Instance Attribute Details

#collapse_keyObject

Returns the value of attribute collapse_key.



5
6
7
# File 'lib/sanjose/notification.rb', line 5

def collapse_key
  @collapse_key
end

#dataObject

Returns the value of attribute data.



5
6
7
# File 'lib/sanjose/notification.rb', line 5

def data
  @data
end

#delay_when_idleObject

Returns the value of attribute delay_when_idle.



5
6
7
# File 'lib/sanjose/notification.rb', line 5

def delay_when_idle
  @delay_when_idle
end

#devicesObject

Returns the value of attribute devices.



5
6
7
# File 'lib/sanjose/notification.rb', line 5

def devices
  @devices
end

#dry_runObject

Returns the value of attribute dry_run.



5
6
7
# File 'lib/sanjose/notification.rb', line 5

def dry_run
  @dry_run
end

#restricted_package_nameObject

Returns the value of attribute restricted_package_name.



5
6
7
# File 'lib/sanjose/notification.rb', line 5

def restricted_package_name
  @restricted_package_name
end

#sent_atObject (readonly)

Returns the value of attribute sent_at.



7
8
9
# File 'lib/sanjose/notification.rb', line 7

def sent_at
  @sent_at
end

#time_to_liveObject

Returns the value of attribute time_to_live.



5
6
7
# File 'lib/sanjose/notification.rb', line 5

def time_to_live
  @time_to_live
end

Instance Method Details

#add_device(device) ⇒ Object



49
50
51
# File 'lib/sanjose/notification.rb', line 49

def add_device(device)
  @devices << device
end

#device_sizeObject



53
54
55
# File 'lib/sanjose/notification.rb', line 53

def device_size
  @devices.length
end

#mark_as_sent!Object



96
97
98
# File 'lib/sanjose/notification.rb', line 96

def mark_as_sent!
  @sent_at = Time.now
end

#messageObject



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
# File 'lib/sanjose/notification.rb', line 57

def message
  # plan-text format
  # 'registration_id=APA91bH35MqYza3xfc2hfag6Rr8VQPSOmi2nrUOPABlFwowfVMZNHaBGBpx-zQ7nuv9qzCEosepUMPKyOrVn0UncZMa__E2sWuM2Q53fjJ5loqIY1QKCza3MkxAu1rvyhhzJP3meEqpmv-kjBuRTeWe_ysRUICupE-awrK1eiStmmm2Y_VBBSs4'
  # json format
  # '{"registration_ids":["APA91bH35MqYza3xfc2hfag6Rr8VQPSOmi2nrUOPABlFwowfVMZNHaBGBpx-zQ7nuv9qzCEosepUMPKyOrVn0UncZMa__E2sWuM2Q53fjJ5loqIY1QKCza3MkxAu1rvyhhzJP3meEqpmv-kjBuRTeWe_ysRUICupE-awrK1eiStmmm2Y_VBBSs4","APA91bHAX-Owu0Ulm5XubHs7BYy6a4O87Gk8-2JOW3eJ2TauVzWsB0d4yCxjzxMXl40elIIwG3E0Lpvd1_5xGldZm64UO4fqC0RNpvGWKUSr7hMNhavRS8w2NS2XUwYsluysVaXZU0rvmq0zSCH6JHGZiqd0qlUsmg"]}'
  
  json = {}
  if @devices.empty?
    raise ArgumentError 'devices cannot be empty'
  else
    json['registration_ids'] = @devices
  end
  
  if @collapse_key
    json['collapse_key'] = @collapse_key
  end
  if @delay_when_idle
    json['delay_when_idle'] = @delay_when_idle
  end
  
  if @time_to_live
    json['time_to_live'] = @time_to_live
  end
  
  if @data
    json['data'] = @data
  end
  
  if @restricted_package_name
    json['restricted_package_name'] = @restricted_package_name
  end
  
  if @dry_run
    json['dry_run'] = @dry_run
  end
  
  json.to_json
end

#sent?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/sanjose/notification.rb', line 100

def sent?
  !!@sent_at
end