ruby-mpns 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'htmlentities'
4
+
5
+ group :development do
6
+ gem "shoulda", ">= 0"
7
+ gem "rdoc", "~> 3.12"
8
+ gem "bundler", ">= 1.0.0"
9
+ gem "jeweler", "~> 1.8.3"
10
+ gem 'simplecov'
11
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ htmlentities (4.3.1)
6
+ jeweler (1.8.3)
7
+ bundler (~> 1.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rdoc
11
+ json (1.7.3)
12
+ multi_json (1.3.6)
13
+ rake (0.9.2.2)
14
+ rdoc (3.12)
15
+ json (~> 1.4)
16
+ shoulda (3.0.1)
17
+ shoulda-context (~> 1.0.0)
18
+ shoulda-matchers (~> 1.0.0)
19
+ shoulda-context (1.0.0)
20
+ shoulda-matchers (1.0.0)
21
+ simplecov (0.6.4)
22
+ multi_json (~> 1.0)
23
+ simplecov-html (~> 0.5.3)
24
+ simplecov-html (0.5.3)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ bundler (>= 1.0.0)
31
+ htmlentities
32
+ jeweler (~> 1.8.3)
33
+ rdoc (~> 3.12)
34
+ shoulda
35
+ simplecov
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 Nicolas VERINAUD
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # ruby-mpns
2
+
3
+ A ruby gem to communicate with Microsoft Push Notification Service.
4
+
5
+ ## Installing
6
+
7
+ From the commande line:
8
+
9
+ `gem install ruby-mpns`
10
+
11
+ Or in your gemfile:
12
+
13
+ `gem 'ruby-mpns'`
14
+
15
+ ## Usage
16
+
17
+ To use it juste use the singleton `MicrosoftPushNotificationService` like the following:
18
+
19
+ uri = "http://my-uri.com/to/the-windows-phone-i-am-targetting"
20
+
21
+ options = {
22
+ title: "Hello !",
23
+ content: "Hello Push Notification.",
24
+ params: {
25
+ any_data: 2,
26
+ another_key: "Hum..."
27
+ }
28
+ }
29
+
30
+ # response is an Net::HTTP object
31
+ reponse = MicrosoftPushNotificationService.send_notification uri, :toast, options
32
+
33
+ See `sample/sample_usage.rb` for more samples.
34
+
35
+ ## Parameters
36
+
37
+ ### Toast notification
38
+
39
+ The following notification parameters can be defined in the options hash for `:toast`:
40
+
41
+ * `title` - the title
42
+ * `content` - the content
43
+ * `params` - a hash that will be transformed into `key1=value1&key2=value2...`
44
+
45
+ ### Raw notification
46
+
47
+ You can pass whatever hash you want and an XML will be generated, like the following:
48
+
49
+ <root>
50
+ <key1>value1</key1>
51
+ <key2>value2</key2>
52
+ </root>
53
+
54
+ **Warning** Currently limited to one level, the hash must not contain another hash.
55
+
56
+ ### Tile notification
57
+
58
+ The following notification parameters can be defined in the options hash for `:tile`:
59
+
60
+ * `title` - the title
61
+ * `count` - the badge to display on the app tile
62
+ * `background_image` - the path to a local image embedded in the app or an image accessible via HTTP (.jpg or .png, 173x137px, max 80kb)
63
+ * `back_title` - the title when the tile is flipped
64
+ * `back_background_image` - the path to a local image embedded in the app or an image accessible via HTTP (.jpg or .png, 173x137px, max 80kb)
65
+ * `back_content` - the content when the tile is flipped
66
+
67
+ ## Reference
68
+
69
+ For general information about Push Notification on Windows Phone check the [MSDN](http://msdn.microsoft.com/en-us/library/hh202945\(v=vs.92\).aspx).
70
+
71
+ ## Todo
72
+
73
+ * Add unit tests
74
+ * Add support for multi-level hash for raw notifications
75
+ * Make the future gem rails compatible and add an ActiveRecord extension (acts_as_microsoft_pushable)
76
+
77
+ ## License
78
+
79
+ Copyright (c) 2012 [Nicolas VERINAUD](http://www.nverinaud.com). Released under the MIT license.
80
+
81
+
data/Rakefile ADDED
@@ -0,0 +1,46 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ gem.name = "ruby-mpns"
17
+ gem.homepage = "http://github.com/nverinaud/ruby-mpns"
18
+ gem.license = "MIT"
19
+ gem.summary = %Q{A ruby gem to communicate with Microsoft Push Notification Service.}
20
+ gem.description = %Q{This gem provides an easy way to send push notifications to Windows Phone devices using Microsoft Push Notification Service.}
21
+ gem.email = "n.verinaud@gmail.com"
22
+ gem.authors = ["Nicolas VERINAUD"]
23
+ gem.add_dependency 'htmlentities'
24
+ gem.required_ruby_version = '>= 1.9.2'
25
+ gem.files.exclude 'sample' # exclude sample directory
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ task :default => :test
37
+
38
+ require 'rdoc/task'
39
+ Rake::RDocTask.new do |rdoc|
40
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
+
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = "ruby-mpns #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
data/lib/ruby-mpns.rb ADDED
@@ -0,0 +1,159 @@
1
+ require "htmlentities"
2
+ require "net/http"
3
+ require "uri"
4
+
5
+ module MicrosoftPushNotificationService
6
+
7
+ def self.extended(base)
8
+ unless base.respond_to?(:device_uri)
9
+ base.class.class_eval do
10
+ attr_accessor :device_uri
11
+ end
12
+ end
13
+ end
14
+
15
+ def self.send_notification uri, type, options = {}
16
+ device = Object.new
17
+ device.extend MicrosoftPushNotificationService
18
+ device.device_uri = uri
19
+ device.send_notification type, options
20
+ end
21
+
22
+ def send_notification type, options = {}
23
+ type = safe_type_to_sym type
24
+ header = self.http_header_for_type type
25
+
26
+ notification = nil
27
+ notification_class = nil
28
+
29
+ if type == :tile
30
+ notification = tile_notification_with_options options
31
+ notification_class = "1"
32
+ elsif type == :toast
33
+ notification = toast_notification_with_options options
34
+ notification_class = "2"
35
+ else
36
+ notification = raw_notification_with_options options
37
+ notification_class = "3"
38
+ end
39
+
40
+ # HTTP connection
41
+ uri = URI.parse(self.device_uri)
42
+
43
+ http = Net::HTTP.new(uri.host, uri.port)
44
+ request = Net::HTTP::Post.new(uri.request_uri)
45
+ request.content_type = "text/xml"
46
+
47
+ if type.to_sym != :raw
48
+ request["X-WindowsPhone-Target"] = type.to_s
49
+ end
50
+ request["X-NotificationClass"] = notification_class
51
+ request.body = notification
52
+ request.content_length = notification.length
53
+
54
+ response = Net::HTTP.start(uri.host, uri.port) do |http|
55
+ http.request(request)
56
+ end
57
+
58
+ return response
59
+ end
60
+
61
+
62
+ protected
63
+
64
+ def safe_type_to_sym type
65
+ sym = nil
66
+
67
+ unless type.nil?
68
+ sym = type.to_sym
69
+ else
70
+ sym = :raw
71
+ end
72
+
73
+ if sym != :tile && sym != :toast
74
+ sym = :raw
75
+ end
76
+
77
+ return sym
78
+ end
79
+
80
+ def http_header_for_type type
81
+
82
+ if type == :toast || type == :tile
83
+ "X-WindowsPhone-Target:#{type.to_s}"
84
+ end
85
+ end
86
+
87
+ # Toast options :
88
+ # - title : string
89
+ # - content : string
90
+ # - params : hash
91
+ def toast_notification_with_options options = {}
92
+ coder = HTMLEntities.new
93
+
94
+ notification = '<?xml version="1.0" encoding="utf-8"?>'
95
+ notification << '<wp:Notification xmlns:wp="WPNotification">'
96
+ notification << '<wp:Toast>'
97
+ notification << '<wp:Text1>' << coder.encode(options[:title]) << '</wp:Text1>'
98
+ notification << '<wp:Text2>' << coder.encode(options[:content]) << '</wp:Text2>'
99
+ notification << '<wp:Param>' << coder.encode(format_params(options[:params])) << '</wp:Param>'
100
+ notification << '</wp:Toast>'
101
+ notification << '</wp:Notification>'
102
+ return notification
103
+ end
104
+
105
+ # Tile options :
106
+ # - title : string
107
+ # - background_image : string, path to local image embedded in the app or accessible via HTTP (.jpg or .png, 173x137px, max 80kb)
108
+ # - count : integer
109
+ # - back_title : string
110
+ # - back_background_image : string, path to local image embedded in the app or accessible via HTTP (.jpg or .png, 173x137px, max 80kb)
111
+ # - back_content : string
112
+ def tile_notification_with_options options = {}
113
+ coder = HTMLEntities.new
114
+
115
+ notification = '<?xml version="1.0" encoding="utf-8"?>'
116
+ notification << '<wp:Notification xmlns:wp="WPNotification">'
117
+ notification << '<wp:Tile '
118
+ notification << '<wp:BackgroundImage>' << coder.encode(options[:background_image]) << '</wp:BackgroundImage>'
119
+ notification << '<wp:Count>' << coder.encode(options[:count]) << '</wp:Count>'
120
+ notification << '<wp:Title>' << coder.encode(options[:title]) << '</wp:Title>'
121
+ notification << '<wp:BackBackgroundImage>' << coder.encode(options[:back_background_image]) << '</wp:BackBackgroundImage>'
122
+ notification << '<wp:BackTitle>' << coder.encode(options[:back_title]) << '</wp:BackTitle>'
123
+ notification << '<wp:BackContent>' << coder.encode(options[:back_content]) << '</wp:BackContent>'
124
+ notification << '</wp:Tile>'
125
+ notification << '</wp:Notification>'
126
+ return notification
127
+ end
128
+
129
+ # Raw options :
130
+ # - raw values send like: <key>value</key>
131
+ def raw_notification_with_options options = {}
132
+ coder = HTMLEntities.new
133
+
134
+ notification = '<?xml version="1.0" encoding="utf-8"?>'
135
+ notification << '<root>'
136
+ options.each do |key,value|
137
+ notification << '<' << coder.encode(key.to_s) << '>' << coder.encode(value.to_s) << '</' << coder.encode(key.to_s) << '>'
138
+ end
139
+ notification << '</root>'
140
+
141
+ puts notification
142
+ return notification
143
+ end
144
+
145
+ def format_params params = {}
146
+ p = "?"
147
+ length = params.length
148
+ count = 0
149
+ params.each do |key,value|
150
+ p << key.to_s << "=" << value.to_s
151
+ count += 1
152
+ if count < length
153
+ p << "&"
154
+ end
155
+ end
156
+ return p
157
+ end
158
+
159
+ end
@@ -0,0 +1,51 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/lib/microsoft_push_notification_service'
4
+
5
+ puts "=== Test ==="
6
+
7
+ uri = "<# Your Microsoft Push URI here #>"
8
+
9
+ #
10
+ # => TOAST
11
+ #
12
+ options = {
13
+ title: "Hi there",
14
+ content: "Testing <correct> encoding of special &éà chars.",
15
+ params: {
16
+ invoice_id: 2,
17
+ state: 10.5,
18
+ another: '"hey hey"'
19
+ }
20
+ }
21
+
22
+ response = MicrosoftPushNotificationService.send_notification uri, :toast, options
23
+ puts response
24
+
25
+
26
+ #
27
+ # => RAW
28
+ #
29
+ options = { # Only on level is currently supported for raw notification, feel free to improve !
30
+ anykey: "anyvalue",
31
+ otherkey: "whynot"
32
+ }
33
+
34
+ response = MicrosoftPushNotificationService.send_notification uri, :raw, options
35
+ puts response
36
+
37
+
38
+ #
39
+ # => TILE
40
+ #
41
+ options = {
42
+ title: "Hello World !",
43
+ background_image: "local/hello.png",
44
+ count: 12,
45
+ back_title: "! dlroW olleH",
46
+ back_background_image: "local/olleh.png",
47
+ back_content: "! hcuoA"
48
+ }
49
+
50
+ response = MicrosoftPushNotificationService.send_notification uri, :tile, options
51
+ puts response
data/test/helper.rb ADDED
@@ -0,0 +1,21 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
4
+ require 'rubygems'
5
+ require 'bundler'
6
+ begin
7
+ Bundler.setup(:default, :development)
8
+ rescue Bundler::BundlerError => e
9
+ $stderr.puts e.message
10
+ $stderr.puts "Run `bundle install` to install missing gems"
11
+ exit e.status_code
12
+ end
13
+ require 'test/unit'
14
+ require 'shoulda'
15
+
16
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
17
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
18
+ require 'ruby-mpns'
19
+
20
+ class Test::Unit::TestCase
21
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestRubyMpns < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,170 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-mpns
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nicolas VERINAUD
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: htmlentities
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: shoulda
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rdoc
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '3.12'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '3.12'
62
+ - !ruby/object:Gem::Dependency
63
+ name: bundler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: 1.0.0
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 1.0.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: jeweler
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 1.8.3
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 1.8.3
94
+ - !ruby/object:Gem::Dependency
95
+ name: simplecov
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: htmlentities
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ description: This gem provides an easy way to send push notifications to Windows Phone
127
+ devices using Microsoft Push Notification Service.
128
+ email: n.verinaud@gmail.com
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files:
132
+ - LICENSE
133
+ - README.md
134
+ files:
135
+ - Gemfile
136
+ - Gemfile.lock
137
+ - LICENSE
138
+ - README.md
139
+ - Rakefile
140
+ - VERSION
141
+ - lib/ruby-mpns.rb
142
+ - sample/sample_usage.rb
143
+ - test/helper.rb
144
+ - test/test_ruby-mpns.rb
145
+ homepage: http://github.com/nverinaud/ruby-mpns
146
+ licenses:
147
+ - MIT
148
+ post_install_message:
149
+ rdoc_options: []
150
+ require_paths:
151
+ - lib
152
+ required_ruby_version: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: 1.9.2
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
159
+ none: false
160
+ requirements:
161
+ - - ! '>='
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ requirements: []
165
+ rubyforge_project:
166
+ rubygems_version: 1.8.24
167
+ signing_key:
168
+ specification_version: 3
169
+ summary: A ruby gem to communicate with Microsoft Push Notification Service.
170
+ test_files: []