|
1 | | -require File.dirname(__FILE__) + '/virtual_session/base' |
| 1 | +require File.dirname(__FILE__) + '/session/base' |
| 2 | +require File.dirname(__FILE__) + '/notification' |
2 | 3 |
|
3 | 4 | module Pntfr |
4 | 5 |
|
5 | 6 | class Notifier |
6 | | - def self.to session |
7 | | - if Platforms.android? session.platform |
8 | | - Pntfr::VirtualSession::Android.new(session) |
9 | | - elsif Platforms.ios? session.platform |
10 | | - Pntfr::VirtualSession::Ios.new(session) |
| 7 | + def self.to devices, credentials=nil |
| 8 | + notif= Notifier.new(credentials) |
| 9 | + notif.update_devices(devices) |
| 10 | + notif |
| 11 | + end |
| 12 | + |
| 13 | + attr_reader :andr_responses |
| 14 | + attr_reader :ios_responses |
| 15 | + |
| 16 | + # |
| 17 | + # +credentials+ is a hash with 2 keys :andr and :ios, each one with its |
| 18 | + # given credentials: |
| 19 | + # - :andr => a string with the notification_key |
| 20 | + # - :ios => a hash of the form { |
| 21 | + # host: 'test-host', |
| 22 | + # pem: 'test-pem', |
| 23 | + # port: 'test-port', |
| 24 | + # pass: 'test-password', |
| 25 | + # } |
| 26 | + # |
| 27 | + def initialize credentials=nil |
| 28 | + validate_credentials(credentials) |
| 29 | + @credentials= credentials || {} |
| 30 | + |
| 31 | + @andr_responses=[] |
| 32 | + @ios_responses= [] |
| 33 | + end |
| 34 | + |
| 35 | + def update_devices(devices) |
| 36 | + devices= [devices] unless devices.kind_of?(Array) |
| 37 | + |
| 38 | + # the list of ANDROID push_id to send the notification to |
| 39 | + @andr_ids= [] |
| 40 | + @ios_devices= [] |
| 41 | + devices.each do |device| |
| 42 | + if Platforms.android? device.platform |
| 43 | + @andr_ids << device.push_id |
| 44 | + elsif Platforms.ios? device.platform |
| 45 | + @ios_devices << device |
| 46 | + end |
| 47 | + end |
| 48 | + self |
| 49 | + end |
| 50 | + |
| 51 | + def msg content, custom=nil |
| 52 | + @msg= Notification.new(content, custom) |
| 53 | + self |
| 54 | + end |
| 55 | + |
| 56 | + def notify |
| 57 | + if any_android_device? |
| 58 | + @andr_responses << andr_session.notify(@andr_ids, @msg.to_android) |
| 59 | + end |
| 60 | + if any_ios_device? |
| 61 | + @ios_responses << ios_session.notify(@ios_devices, @msg.to_ios) |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + def any_android_device? |
| 66 | + @andr_ids.any? |
| 67 | + end |
| 68 | + |
| 69 | + def any_ios_device? |
| 70 | + @ios_devices.any? |
| 71 | + end |
| 72 | + |
| 73 | + #-------------------------------------------- |
| 74 | + private |
| 75 | + #-------------------------------------------- |
| 76 | + def andr_session |
| 77 | + @andr_session||= Pntfr::Session::Android.new(@credentials[:andr]) |
| 78 | + end |
| 79 | + def ios_session |
| 80 | + @ios_session||= Pntfr::Session::Ios.new(@credentials[:ios]) |
| 81 | + end |
| 82 | + def validate_credentials(credentials) |
| 83 | + return if credentials.nil? |
| 84 | + if !credentials.is_a?(Hash) |
| 85 | + raise ArgumentError.new('Credentials should be a Hash with either :andr or :ios keys!') |
| 86 | + end |
| 87 | + if !(credentials.has_key?(:andr) or credentials.has_key?(:ios)) |
| 88 | + raise ArgumentError.new('Either :andr or :ios service credentials should have been provided!') |
11 | 89 | end |
12 | 90 | end |
| 91 | + |
13 | 92 | end |
14 | 93 |
|
15 | 94 | end |
0 commit comments