module MicrosoftPushNotificationService
Public Class Methods
extended(base)
click to toggle source
# File lib/ruby-mpns.rb, line 7 def self.extended(base) unless base.respond_to?(:device_uri) base.class.class_eval do attr_accessor :device_uri end end end
send_notification(uri, type, options = {})
click to toggle source
# File lib/ruby-mpns.rb, line 15 def self.send_notification(uri, type, options = {}) device = Object.new device.extend MicrosoftPushNotificationService device.device_uri = uri device.send_notification type, options end
Public Instance Methods
send_notification(type, options = {})
click to toggle source
# File lib/ruby-mpns.rb, line 22 def send_notification(type, options = {}) type = safe_type_to_sym(type) notification, notification_class = build_notification(type, options) uri = URI.parse(self.device_uri) request = Net::HTTP::Post.new(uri.request_uri) request.content_type = 'text/xml' request['X-WindowsPhone-Target'] = windowsphone_target_header_for_type(type) request['X-NotificationClass'] = notification_class request.body = notification request.content_length = notification.length Net::HTTP.start(uri.host, uri.port) { |http| http.request request } end
Protected Instance Methods
build_hash(xml, options)
click to toggle source
# File lib/ruby-mpns.rb, line 121 def build_hash(xml, options) options.each do |k, v| xml.tag!(k.to_s) { v.is_a?(Hash) ? build_hash(xml, v) : xml.text!(v.to_s) } end end
build_notification(type, options = {})
click to toggle source
# File lib/ruby-mpns.rb, line 65 def build_notification(type, options = {}) notification_builder = notification_builder_for_type(type) send(notification_builder, options) end
format_params(params = {})
click to toggle source
# File lib/ruby-mpns.rb, line 127 def format_params(params = {}) return '' if params.nil? query = params.collect { |k, v| k.to_s + '=' + v.to_s } * '&' '?' + query end
notification_builder_for_type(type)
click to toggle source
# File lib/ruby-mpns.rb, line 54 def notification_builder_for_type(type) case type when :tile :tile_notification_with_options when :toast :toast_notification_with_options else :raw_notification_with_options end end
raw_notification_with_options(options = {})
click to toggle source
Raw options :
- raw values send like: <key>value</key>
# File lib/ruby-mpns.rb, line 114 def raw_notification_with_options(options = {}) xml = Builder::XmlMarkup.new xml.instruct! xml.root { build_hash(xml, options) } [xml.target!, '3'] end
safe_type_to_sym(type)
click to toggle source
# File lib/ruby-mpns.rb, line 40 def safe_type_to_sym(type) sym = type.to_sym unless type.nil? sym = :raw unless [:tile, :toast].include?(sym) sym end
tile_notification_with_options(options = {})
click to toggle source
Tile options :
- title : string - background_image : string, path to local image embedded in the app or accessible via HTTP (.jpg or .png, 173x137px, max 80kb) - count : integer - back_title : string - back_background_image : string, path to local image embedded in the app or accessible via HTTP (.jpg or .png, 173x137px, max 80kb) - back_content : string - (optional) navigation_uri : string, the exact navigation URI for the tile to update, only needed if you wish to update a secondary tile
# File lib/ruby-mpns.rb, line 78 def tile_notification_with_options(options = {}) uri = options[:navigation_uri] xml = Builder::XmlMarkup.new xml.instruct! xml.tag!('wp:Notification', 'xmlns:wp' => 'WPNotification') do xml.tag!('wp:Tile', uri.nil? ? {} : {'Id' => uri}) do xml.tag!('wp:BackgroundImage') { xml.text!(options[:background_image] || '') } xml.tag!('wp:Count') { xml.text!(options[:count].to_s || '') } xml.tag!('wp:Title') { xml.text!(options[:title] || '') } xml.tag!('wp:BackBackgroundImage') { xml.text!(options[:back_background_image] || '') } xml.tag!('wp:BackTitle') { xml.text!(options[:back_title] || '') } xml.tag!('wp:BackContent') { xml.text!(options[:back_content] || '') } end end [xml.target!, '1'] end
toast_notification_with_options(options = {})
click to toggle source
Toast options :
- title : string - content : string - params : hash
# File lib/ruby-mpns.rb, line 99 def toast_notification_with_options(options = {}) xml = Builder::XmlMarkup.new xml.instruct! xml.tag!('wp:Notification', 'xmlns:wp' => 'WPNotification') do xml.tag!('wp:Toast') do xml.tag!('wp:Text1') { xml.text!(options[:title] || '') } xml.tag!('wp:Text2') { xml.text!(options[:content] || '') } xml.tag!('wp:Param') { xml.text!(format_params(options[:params])) } end end [xml.target!, '2'] end
windowsphone_target_header_for_type(type)
click to toggle source
# File lib/ruby-mpns.rb, line 46 def windowsphone_target_header_for_type(type) if (type == :tile) type = :token end type.to_s if [:token, :toast].include?(type.to_sym) end