Skip to content

Commit

Permalink
Add ability to use multi-level hash in raw notification
Browse files Browse the repository at this point in the history
  • Loading branch information
dmedvinsky committed Jan 11, 2013
1 parent 5f289ce commit 4c7e68b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
12 changes: 7 additions & 5 deletions lib/ruby-mpns.rb
Expand Up @@ -106,14 +106,16 @@ def toast_notification_with_options(options = {})
def raw_notification_with_options options = {}
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
xml.root { build_hash(xml, options) }
[xml.target!, '3']
end

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

def format_params params = {}
return '' if params.nil?
query = params.collect { |k, v| k.to_s + '=' + v.to_s } * '&'
Expand Down
46 changes: 31 additions & 15 deletions test/test_ruby-mpns.rb
Expand Up @@ -49,14 +49,14 @@ class TestRubyMpns < Test::Unit::TestCase
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: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

Expand All @@ -66,11 +66,11 @@ class TestRubyMpns < Test::Unit::TestCase
{ 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:Toast>' +
'<wp:Text1>title</wp:Text1>' +
'<wp:Text2>content</wp:Text2>' +
'<wp:Param>?</wp:Param>' +
'</wp:Toast>' +
'</wp:Notification>'
end

Expand All @@ -80,8 +80,24 @@ class TestRubyMpns < Test::Unit::TestCase
{ key1: 'val1', key2: 'val2' })
assert_equal xml, '<?xml version="1.0" encoding="UTF-8"?>' +
'<root>' +
'<key1>val1</key1>' +
'<key2>val2</key2>' +
'<key1>val1</key1>' +
'<key2>val2</key2>' +
'</root>'
end

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

0 comments on commit 4c7e68b

Please sign in to comment.