Class: Pntfr::Notifier

Inherits:
BaseService show all
Defined in:
lib/pntfr/notifier.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credentials = nil) ⇒ Notifier

credentials is a hash with 2 keys :andr and :ios, each one with its given credentials:

  • :andr => a string with the notification_key

  • :ios => a hash of the form

    host: 'test-host',
    pem: 'test-pem',
    port: 'test-port',
    pass: 'test-password',
    



28
29
30
31
32
33
# File 'lib/pntfr/notifier.rb', line 28

def initialize credentials=nil
  super

  @andr_responses=[]
  @ios_responses= []
end

Instance Attribute Details

#andr_responsesObject (readonly)

Returns the value of attribute andr_responses.



14
15
16
# File 'lib/pntfr/notifier.rb', line 14

def andr_responses
  @andr_responses
end

#ios_responsesObject (readonly)

Returns the value of attribute ios_responses.



15
16
17
# File 'lib/pntfr/notifier.rb', line 15

def ios_responses
  @ios_responses
end

Class Method Details

.to(devices, credentials = nil) ⇒ Object



8
9
10
11
12
# File 'lib/pntfr/notifier.rb', line 8

def self.to devices, credentials=nil
  notif= Notifier.new(credentials)
  notif.update_devices(devices)
  notif
end

Instance Method Details

#any_android_device?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/pntfr/notifier.rb', line 65

def any_android_device?
  @andr_ids.any?
end

#any_ios_device?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/pntfr/notifier.rb', line 69

def any_ios_device?
  @ios_devices.any?
end

#msg(content, custom = nil) ⇒ Object



51
52
53
54
# File 'lib/pntfr/notifier.rb', line 51

def msg content, custom=nil
  @msg= Notification.new(content, custom)
  self
end

#notifyObject



56
57
58
59
60
61
62
63
# File 'lib/pntfr/notifier.rb', line 56

def notify
  if any_android_device?
    @andr_responses << andr_session.notify(@andr_ids, @msg.to_android)
  end
  if any_ios_device?
    @ios_responses << ios_session.notify(@ios_devices, @msg.to_ios)
  end
end

#update_devices(devices) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/pntfr/notifier.rb', line 35

def update_devices(devices)
  devices= [devices] unless devices.kind_of?(Array)

  # the list of ANDROID push_id to send the notification to
  @andr_ids= []
  @ios_devices= []
  devices.each do |device|
    if Platforms.android? device.platform
      @andr_ids << device.push_id
    elsif Platforms.ios? device.platform
      @ios_devices << device
    end
  end
  self
end