Module: Rhoconnect::PingJob

Defined in:
lib/rhoconnect/jobs/ping_job.rb

Class Method Summary collapse

Class Method Details

.perform(params) ⇒ Object

Perform a ping for all clients registered to a user



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rhoconnect/jobs/ping_job.rb', line 8

def self.perform(params)
  device_pins = []
  phone_ids = []
  user_ids = params['user_id']
  user_ids = [user_ids] unless user_ids.is_a? Array
  errors = []
  user_ids.each do |user|
    user_errors = ping_by_user user, params, device_pins, phone_ids
    errors = errors + user_errors if user_errors
  end
  if errors.size != 0
    joined_err = errors.join '\n'
    raise joined_err
  end
end

.ping_by_user(user_id, params, device_pins, phone_ids) ⇒ Object



24
25
26
27
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
# File 'lib/rhoconnect/jobs/ping_job.rb', line 24

def self.ping_by_user(user_id, params, device_pins, phone_ids)
  # all errors are recorded here
  errors = []
  user = User.load(user_id)
  clients = user.clients if user
  if clients
    clients.members.each do |client_id|
      client = Client.load(client_id,{:source_name => '*'})
      params.merge!(
        'device_port' => client.device_port, 
        'device_pin' => client.device_pin, 
        'phone_id' => client.phone_id
      )
      send_push = false
      if client.device_type and client.device_type.size > 0
        if client.phone_id and client.phone_id.size > 0
          unless phone_ids.include? client.phone_id   
            phone_ids << client.phone_id
            send_push = true
          end
        elsif client.device_pin and client.device_pin.size > 0
          unless device_pins.include? client.device_pin   
            device_pins << client.device_pin
            send_push = true
          end
        else
          log "Skipping ping for non-registered client_id '#{client_id}'..."
          next
        end
        if send_push
          type = client.device_push_type || client.device_type
          klass = Object.const_get(camelize(type.downcase))
          if klass
            params['vibrate'] = params['vibrate'].to_s
            begin
              klass.ping(params) 
            rescue Exception => e
              errors << e
            end
          end
        else
          log "Dropping ping request for client_id '#{client_id}' because it's already in user's device pin or phone_id list."
        end
      else
        log "Skipping ping for non-registered client_id '#{client_id}'..."
      end
    end
  else
    log "Skipping ping for unknown user '#{user_id}' or '#{user_id}' has no registered clients..."
  end
  errors
end