Module: Rhoconnect::PingJob

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

Class Method Summary collapse

Class Method Details

.enqueue(params) ⇒ Object

Enqueue a ping job



8
9
10
# File 'lib/rhoconnect/jobs/ping_job.rb', line 8

def self.enqueue(params)
  Resque.enqueue(PingJob, params)
end

.perform(params) ⇒ Object

Perform a ping for all clients registered to a user



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rhoconnect/jobs/ping_job.rb', line 13

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



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
91
92
93
94
95
96
# File 'lib/rhoconnect/jobs/ping_job.rb', line 29

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
    client_objects = []
    clients.members.each do |client_id|
        client_obj = Client.load(client_id,{:source_name => '*'})
        client_objects << client_obj
    end
    sorted_clients = client_objects.sort! { |a,b|  a.last_sync <=> b.last_sync }
    sorted_clients.reverse_each do |client|
      params.merge!(
        'device_port' => client.device_port,
        'device_pin' => client.device_pin,
        'phone_id' => client.phone_id,
        'device_app_id' => client.device_app_id,
        'device_app_version' => client.device_app_version
      )
      send_push = false
      if client.device_type and client.device_type.size > 0
        if client.phone_id and client.phone_id.size > 0
          combined_phone_id = client.phone_id
          if client.device_app_id and client.device_app_id.size > 0
              combined_phone_id = combined_phone_id + client.device_app_id
          end
          unless phone_ids.include? combined_phone_id
            phone_ids << combined_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.rho__id.to_s}'..."
          next
        end
        if send_push
          type = client.device_push_type || client.device_type
          klass = nil
          begin
            klass = Object.const_get(camelize(type.downcase))
          rescue Exception => e
            log "Dropping ping request for unsupported platform '#{type}'"
          end
          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.rho__id.to_s}' because it's already in user's device pin or phone_id list."
        end
      else
        log "Skipping ping for non-registered client_id '#{client.rho__id.to_s}'..."
      end
    end
  else
    log "Skipping ping for unknown user '#{user_id}' or '#{user_id}' has no registered clients..."
  end
  errors
end