Class: Sanjose::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



23
24
25
26
# File 'lib/sanjose/client.rb', line 23

def initialize
  @gcm_gateway_uri = GCM_GATEWAY_URI
  @gcm_api_key = ENV['GCM_API_KEY']
end

Instance Attribute Details

#gcm_api_keyObject

Returns the value of attribute gcm_api_key.



21
22
23
# File 'lib/sanjose/client.rb', line 21

def gcm_api_key
  @gcm_api_key
end

#gcm_gateway_uriObject

Returns the value of attribute gcm_gateway_uri.



21
22
23
# File 'lib/sanjose/client.rb', line 21

def gcm_gateway_uri
  @gcm_gateway_uri
end

Instance Method Details

#push(notification, retries = 5) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
# File 'lib/sanjose/client.rb', line 28

def push(notification, retries = 5)
  return if not notification or notification.sent?
  
  attempt = 0
  try_again = true
  muticast_ids = []
  results = {}
  unsent_reg_ids = notification.devices
  backoff = BACKOFF_INITIAL_DELAY
  
  while try_again do
    attempt += 1
    multicast_result = push_no_retry(notification, 
      connection_options_for_endpoint(:gateway), unsent_reg_ids)
    
    if multicast_result
      muticast_ids << multicast_result.multicast_id
      unsent_reg_ids = update_status(unsent_reg_ids, results, multicast_result)
    end
    
    try_again = !unsent_reg_ids.empty? and attempt <= retries
    
    if try_again
      sleep_time = backoff / 2 + rand(backoff)
      sleep(sleep_time / 1000)
      if 2 * backoff < MAX_BACKOFF_DELAY
        backoff *= 2
      end
    end       
  end    
  
  # calculate summary
  success = 0
  failure = 0
  canonical_ids = 0
  
  results.values.each do |result|
    if result.message_id
      success += 1
      if result.canonical_reg_id
        canonical_ids += 1
      end
    else
      failure += 1
    end
  end
    
  # build a new object with the overall result
  multicast_id = muticast_ids.pop(0)
  builder = MulticastResult.new(
    :success => success, 
    :failure => failure, 
    :canonical_ids => canonical_ids,
    :multicast_id => multicast_id)
  builder.retry_multicast_ids = muticast_ids
  
  # add results, in the same order as the input
  notification.devices.each do |device|
    builder.add_result(results[device])
  end
  
  builder
end