wpns 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/wpns.rb +2 -0
- data/lib/wpns/notifications.rb +66 -0
- data/lib/wpns/version.rb +3 -0
- metadata +47 -0
data/lib/wpns.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'uri'
|
3
|
+
|
4
|
+
module Wpns
|
5
|
+
extend self
|
6
|
+
BASEBATCH = { :tile => 1, :toast => 2, :raw => 3 }
|
7
|
+
BATCHADDS = { :delay450 => 10, :delay900 => 20 }
|
8
|
+
WP_TARGETS = { :toast => "toast", :tile => "token" }
|
9
|
+
|
10
|
+
# Send a WP7 notification to the given URI.
|
11
|
+
#
|
12
|
+
# @param [String] the channel uri that the WP7 device generated
|
13
|
+
# @param [Hash] the options for the following message types
|
14
|
+
# Toast :title => a bold message
|
15
|
+
# :message => the small message
|
16
|
+
# :param => a string parameter that is passed to the app
|
17
|
+
# Tile :image => a new image for the tile
|
18
|
+
# :count => a number to show on the tile
|
19
|
+
# :title => the new title of the tile
|
20
|
+
# :back_image => an image for the back of the tile
|
21
|
+
# :back_title => a title on the back of the tile
|
22
|
+
# :back_content => some content (text) for the back
|
23
|
+
# Raw :message => the full XML message body
|
24
|
+
def send_notification(uri, params, type = :raw, delay = nil)
|
25
|
+
msg = build_message params, type
|
26
|
+
notification_class = calculate_delay type, delay
|
27
|
+
|
28
|
+
uri = URI.parse uri
|
29
|
+
http = Net::HTTP.new uri.host, uri.port
|
30
|
+
headers = { 'Content-Type' => 'text/html',
|
31
|
+
'Content-Length' => msg.bytesize.to_s,
|
32
|
+
'X-NotificationClass' => notification_class.to_s }
|
33
|
+
headers['X-WindowsPhone-Target'] = WP_TARGETS[type] unless type == :raw
|
34
|
+
|
35
|
+
http.post(uri.path, msg, headers)
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
def calculate_delay(type, delay)
|
40
|
+
BASEBATCH[type] + (BATCHADDS[delay] || 0)
|
41
|
+
end
|
42
|
+
|
43
|
+
def build_message(params, type)
|
44
|
+
wp_type = type.to_s.capitalize
|
45
|
+
unless type == :raw
|
46
|
+
msg_body="<?xml version='1.0' encoding='utf-8'?><wp:Notification xmlns:wp='WPNotification'><wp:#{wp_type}>"
|
47
|
+
case type
|
48
|
+
when :toast
|
49
|
+
msg_body << "<wp:Text1>#{params[:title]}</wp:Text1>" +
|
50
|
+
"<wp:Text2>#{params[:message]}</wp:Text2>" +
|
51
|
+
"<wp:Param>#{params[:param]}</wp:Param>"
|
52
|
+
when :tile
|
53
|
+
msg_body << "<wp:BackgroundImage>#{params[:image]}</wp:BackgroundImage>" +
|
54
|
+
"<wp:Count>#{params[:count].to_s}</wp:Count>" +
|
55
|
+
"<wp:Title>#{params[:title]}</wp:Title>" +
|
56
|
+
"<wp:BackBackgroundImage>#{params[:back_image]}</wp:BackBackgroundImage>" +
|
57
|
+
"<wp:BackTitle>#{params[:back_title]}</wp:BackTitle>" +
|
58
|
+
"<wp:BackContent>#{params[:back_content]}</wp:BackContent>"
|
59
|
+
end
|
60
|
+
msg_body << "</wp:#{wp_type}></wp:Notification>"
|
61
|
+
else
|
62
|
+
msg_body = params[:message]
|
63
|
+
end
|
64
|
+
msg_body
|
65
|
+
end
|
66
|
+
end
|
data/lib/wpns/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wpns
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tim Felgentreff
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-19 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: ! '[description]'
|
15
|
+
email: timfelgentreff@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/wpns.rb
|
21
|
+
- lib/wpns/notifications.rb
|
22
|
+
- lib/wpns/version.rb
|
23
|
+
homepage: https://github.com/timfel/wpns
|
24
|
+
licenses: []
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project: ! '[none]'
|
43
|
+
rubygems_version: 1.8.11
|
44
|
+
signing_key:
|
45
|
+
specification_version: 3
|
46
|
+
summary: ! '[summary]'
|
47
|
+
test_files: []
|