Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 01ef3aeef23458881a5fe15776e9bd146bb25ff8 @jpoz committed Jul 7, 2009
Showing with 172 additions and 0 deletions.
  1. +22 −0 MIT-LICENSE
  2. +6 −0 README.textile
  3. +53 −0 Rakefile
  4. +30 −0 apns.gemspec
  5. +61 −0 lib/apns.rb
  6. BIN pkg/apns-0.0.1.gem
22 MIT-LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2009 James Pozdena
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
6 README.textile
@@ -0,0 +1,6 @@
+h2. Apple Push Notification Service
+
+h3. Example:
+
+APNS.pem = '/path/to/pem/file' # openssl pkcs12 -in mycert.p12 -out client-cert.pem -nodes -clcerts
+APNS.send_notification('123234423asdflasdfsdf123123123123123', {:aps => {:alert => 'Hello iPhone!', :badge => 1, :sound => 'default'}})
53 Rakefile
@@ -0,0 +1,53 @@
+require 'rubygems'
+require 'rake/gempackagetask'
+require 'rubygems/specification'
+require 'date'
+require 'spec/rake/spectask'
+
+GEM = 'apns'
+GEM_NAME = 'apns'
+GEM_VERSION = '0.0.1'
+AUTHORS = ['James Pozdena']
+EMAIL = "jpoz@jpoz.net"
+HOMEPAGE = "http://github.com/jpoz/apns"
+SUMMARY = "Simple Apple push notification service gem"
+
+spec = Gem::Specification.new do |s|
+ s.name = GEM
+ s.version = GEM_VERSION
+ s.platform = Gem::Platform::RUBY
+ s.has_rdoc = true
+ s.extra_rdoc_files = ["MIT-LICENSE"]
+ s.summary = SUMMARY
+ s.description = s.summary
+ s.authors = AUTHORS
+ s.email = EMAIL
+ s.homepage = HOMEPAGE
+ s.require_path = 'lib'
+ s.autorequire = GEM
+ s.files = %w(MIT-LICENSE README.textile Rakefile) + Dir.glob("{lib}/**/*")
+end
+
+task :default => :spec
+
+desc "Run specs"
+Spec::Rake::SpecTask.new do |t|
+ t.spec_files = FileList['spec/**/*_spec.rb']
+ t.spec_opts = %w(-fs --color)
+end
+
+Rake::GemPackageTask.new(spec) do |pkg|
+ pkg.gem_spec = spec
+end
+
+desc "install the gem locally"
+task :install => [:package] do
+ sh %{sudo gem install pkg/#{GEM}-#{GEM_VERSION}}
+end
+
+desc "create a gemspec file"
+task :make_spec do
+ File.open("#{GEM}.gemspec", "w") do |file|
+ file.puts spec.to_ruby
+ end
+end
30 apns.gemspec
@@ -0,0 +1,30 @@
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{apns}
+ s.version = "0.0.1"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["James Pozdena"]
+ s.autorequire = %q{apns}
+ s.date = %q{2009-07-07}
+ s.description = %q{Simple Apple push notification service gem}
+ s.email = %q{jpoz@jpoz.net}
+ s.extra_rdoc_files = ["MIT-LICENSE"]
+ s.files = ["MIT-LICENSE", "README.textile", "Rakefile", "lib/apns.rb"]
+ s.has_rdoc = true
+ s.homepage = %q{http://github.com/jpoz/apns}
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.1}
+ s.summary = %q{Simple Apple push notification service gem}
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 2
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ else
+ end
+ else
+ end
+end
61 lib/apns.rb
@@ -0,0 +1,61 @@
+require 'rubygems'
+require 'json'
+
+class Hash
+ include JSON
+end
+
+module APNS
+ require 'socket'
+ require 'openssl'
+
+ @host = 'gateway.sandbox.push.apple.com'
+ @port = 2195
+ # openssl pkcs12 -in mycert.p12 -out client-cert.pem -nodes -clcerts
+ @pem = nil # this should be the path of the pem file not the contentes
+ @pass = nil
+
+ class << self
+ attr_accessor :host, :pem, :port, :pass
+ end
+
+ def self.send_notification(device_token, message_hash)
+ sock, ssl = self.open_connection
+ ssl.write(self.packaged_notification(device_token, message_hash))
+
+ ssl.close
+ sock.close
+ end
+
+ private
+
+ def self.packaged_notification(device_token, message_hash)
+ pt = self.packaged_token(device_token)
+ pm = self.packaged_message(message_hash)
+ [0, 0, 32, pt, 0, pm.size, pm].pack("ccca*cca*")
+ end
+
+ def self.packaged_token(device_token)
+ [device_token.gsub(/[\s|<|>]/,'')].pack('H*')
+ end
+
+ def self.packaged_message(message_hash)
+ message_hash.to_json
+ end
+
+ def self.open_connection
+ raise "The path to your pem file is not set. (APNS.pem = /path/to/cert.pem)" unless self.pem
+ raise "The path to your pem file does not exist!" unless File.exist?(self.pem)
+
+ context = OpenSSL::SSL::SSLContext.new
+ context.cert = OpenSSL::X509::Certificate.new(File.read(self.pem))
+ context.key = OpenSSL::PKey::RSA.new(File.read(self.pem), self.pass)
+
+ sock = TCPSocket.new(self.host, self.port)
+ ssl = OpenSSL::SSL::SSLSocket.new(sock,context)
+ ssl.connect
+
+ return sock, ssl
+ end
+
+end
BIN pkg/apns-0.0.1.gem
Binary file not shown.

0 comments on commit 01ef3ae

Please sign in to comment.