Skip to content

Commit

Permalink
Use XML builder
Browse files Browse the repository at this point in the history
WIP to add nested tags in raw notifications.
  • Loading branch information
dmedvinsky committed Jan 11, 2013
1 parent 803b37d commit 5f289ce
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 60 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source "http://rubygems.org"

gem 'htmlentities'
gem 'builder'

group :development do
gem "shoulda", ">= 0"
Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
GEM
remote: http://rubygems.org/
specs:
builder (3.0.4)
git (1.2.5)
htmlentities (4.3.1)
jeweler (1.8.3)
bundler (~> 1.0)
git (>= 1.2.5)
Expand All @@ -27,8 +27,8 @@ PLATFORMS
ruby

DEPENDENCIES
builder
bundler (>= 1.0.0)
htmlentities
jeweler (~> 1.8.3)
rdoc (~> 3.12)
shoulda
Expand Down
98 changes: 42 additions & 56 deletions lib/ruby-mpns.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'htmlentities'
require 'builder'
require 'net/http'
require 'uri'

Expand Down Expand Up @@ -59,24 +59,6 @@ def build_notification(type, options = {})
send(notification_builder, options)
end

# Toast options :
# - title : string
# - content : string
# - params : hash
def toast_notification_with_options options = {}
coder = HTMLEntities.new

notification = '<?xml version="1.0" encoding="utf-8"?>'
notification << '<wp:Notification xmlns:wp="WPNotification">'
notification << '<wp:Toast>'
notification << '<wp:Text1>' << coder.encode(options[:title]) << '</wp:Text1>'
notification << '<wp:Text2>' << coder.encode(options[:content]) << '</wp:Text2>'
notification << '<wp:Param>' << coder.encode(format_params(options[:params])) << '</wp:Param>'
notification << '</wp:Toast>'
notification << '</wp:Notification>'
return notification, '2'
end

# 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)
Expand All @@ -86,52 +68,56 @@ def toast_notification_with_options options = {}
# - 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
def tile_notification_with_options options = {}
coder = HTMLEntities.new

navigation_uri = options[:navigation_uri]
tile_id = navigation_uri.nil? ? "" : 'Id="' + coder.encode(navigation_uri) + '"'

notification = '<?xml version="1.0" encoding="utf-8"?>'
notification << '<wp:Notification xmlns:wp="WPNotification">'
notification << '<wp:Tile' << tile_id << '>'
notification << '<wp:BackgroundImage>' << coder.encode(options[:background_image]) << '</wp:BackgroundImage>'
notification << '<wp:Count>' << coder.encode(options[:count]) << '</wp:Count>'
notification << '<wp:Title>' << coder.encode(options[:title]) << '</wp:Title>'
notification << '<wp:BackBackgroundImage>' << coder.encode(options[:back_background_image]) << '</wp:BackBackgroundImage>'
notification << '<wp:BackTitle>' << coder.encode(options[:back_title]) << '</wp:BackTitle>'
notification << '<wp:BackContent>' << coder.encode(options[:back_content]) << '</wp:BackContent>'
notification << '</wp:Tile>'
notification << '</wp:Notification>'
return notification, '1'
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 options :
# - title : string
# - content : string
# - params : hash
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

# Raw options :
# - raw values send like: <key>value</key>
def raw_notification_with_options options = {}
coder = HTMLEntities.new

notification = '<?xml version="1.0" encoding="utf-8"?>'
notification << '<root>'
options.each do |key,value|
notification << '<' << coder.encode(key.to_s) << '>' << coder.encode(value.to_s) << '</' << coder.encode(key.to_s) << '>'
xml = Builder::XmlMarkup.new
xml.instruct!
xml.root do
options.each do |k, v|
xml.tag!(k.to_s) { xml.text!(v.to_s) }
end
end
notification << '</root>'

return notification, '3'
[xml.target!, '3']
end

def format_params params = {}
p = "?"
length = params.length
count = 0
params.each do |key,value|
p << key.to_s << "=" << value.to_s
count += 1
if count < length
p << "&"
end
end
return p
return '' if params.nil?
query = params.collect { |k, v| k.to_s + '=' + v.to_s } * '&'
'?' + query
end

end
56 changes: 55 additions & 1 deletion test/test_ruby-mpns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,63 @@ class TestRubyMpns < Test::Unit::TestCase
mpns = Object.new.extend MicrosoftPushNotificationService
_, cls = mpns.send(:build_notification, :tile)
assert_equal cls, '1'
_, cls = mpns.send(:build_notification, :toast, {title: 'title', content: 'content', params: {}})
_, cls = mpns.send(:build_notification, :toast)
assert_equal cls, '2'
_, cls = mpns.send(:build_notification, :raw)
assert_equal cls, '3'
end

should 'format params like a boss' do
mpns = Object.new.extend MicrosoftPushNotificationService
q = mpns.send(:format_params, { a: 1, b: 2 })
assert_equal q, '?a=1&b=2'
q = mpns.send(:format_params, { phone: '+0987 654321', email: '[email protected]' })
assert_equal q, '?phone=+0987 654321&[email protected]'
end

should 'make tile XML' do
mpns = Object.new.extend MicrosoftPushNotificationService
xml, _ = mpns.send(:tile_notification_with_options,
{ title: 'title', count: 1337,
background_image: 'bg', back_title: 'bktitle',
back_background_image: 'bkbg',
back_content: 'bkcontent',
navigation_uri: 'uri' })
assert_equal xml, '<?xml version="1.0" encoding="UTF-8"?>' +
'<wp:Notification xmlns:wp="WPNotification">' +
'<wp:Tile Id="uri">' +
'<wp:BackgroundImage>bg</wp:BackgroundImage>' +
'<wp:Count>1337</wp:Count>' +
'<wp:Title>title</wp:Title>' +
'<wp:BackBackgroundImage>bkbg</wp:BackBackgroundImage>' +
'<wp:BackTitle>bktitle</wp:BackTitle>' +
'<wp:BackContent>bkcontent</wp:BackContent>' +
'</wp:Tile>' +
'</wp:Notification>'
end

should 'make toast XML' do
mpns = Object.new.extend MicrosoftPushNotificationService
xml, _ = mpns.send(:toast_notification_with_options,
{ title: 'title', content: 'content', params: {} })
assert_equal xml, '<?xml version="1.0" encoding="UTF-8"?>' +
'<wp:Notification xmlns:wp="WPNotification">' +
'<wp:Toast>' +
'<wp:Text1>title</wp:Text1>' +
'<wp:Text2>content</wp:Text2>' +
'<wp:Param>?</wp:Param>' +
'</wp:Toast>' +
'</wp:Notification>'
end

should 'make raw XML' do
mpns = Object.new.extend MicrosoftPushNotificationService
xml, _ = mpns.send(:raw_notification_with_options,
{ key1: 'val1', key2: 'val2' })
assert_equal xml, '<?xml version="1.0" encoding="UTF-8"?>' +
'<root>' +
'<key1>val1</key1>' +
'<key2>val2</key2>' +
'</root>'
end
end

0 comments on commit 5f289ce

Please sign in to comment.