p8push 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 699edc5b807fc80b18f22160cc0bf227bd9a48a8
4
+ data.tar.gz: 4b4041f2fab807bfe8bea8f5383327aee004aa5a
5
+ SHA512:
6
+ metadata.gz: 1499fbe7a93026ae31ff037b62254f9d8c6970da1efd9a2e28c3b1d3df5438cc1b0e290b4314259bca5a4ef9faeed8cae556a95cabb86538c66d90e5759c800a
7
+ data.tar.gz: dd9617bf78cfbdb9d746307c0888ebfc6c0fb6c15fda24ef80396ce80d6816deaa0ae21e38249482b96fb0d91acafcc4db73a482aab2a4748ef0705dacf150eb
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2012–2015 Mattt Thompson (http://mattt.me/)
4
+ Copyright (c) 2017 Andrew Arrow
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,22 @@
1
+ # p8push
2
+ ruby gem for apple push notifications using only the new p8 format not the older pem format
3
+
4
+ add to Gemfile: `gem 'p8push', git: 'https://github.com/andrewarrow/p8push.git'`
5
+
6
+ ```
7
+ export APN_PRIVATE_KEY=/path/APNsAuthKey_ABCDE12345.p8
8
+ export APN_TEAM_ID=XYZDE99911
9
+ export APN_KEY_ID=ABCDE12345
10
+ export APN_BUNDLE_ID=com.bundle.id
11
+ ```
12
+
13
+ ```
14
+ APN = P8push::Client.development
15
+ token = 'GETREALTOKENFROMADEVICE'
16
+ notification = P8push::Notification.new(device: token)
17
+ notification.alert = 'Hello, World!'
18
+ notification.topic = 'com.some.other.id' # if you do not want default ENV['APN_BUNDLE_ID'] one
19
+ APN.push(notification)
20
+ ```
21
+
22
+ The gem with pem format this came from is https://github.com/nomad/houston
@@ -0,0 +1,74 @@
1
+ require 'openssl'
2
+ require 'jwt'
3
+ require 'net-http2'
4
+
5
+ module P8push
6
+
7
+ APPLE_PRODUCTION_JWT_URI = 'https://api.push.apple.com'
8
+ APPLE_DEVELOPMENT_JWT_URI = 'https://api.development.push.apple.com'
9
+
10
+ class Client
11
+ attr_accessor :jwt_uri
12
+ class << self
13
+ def development
14
+ client = self.new
15
+ client.jwt_uri = APPLE_DEVELOPMENT_JWT_URI
16
+ client
17
+ end
18
+
19
+ def production
20
+ client = self.new
21
+ client.jwt_uri = APPLE_PRODUCTION_JWT_URI
22
+ client
23
+ end
24
+ end
25
+
26
+ def initialize
27
+ @private_key = File.read(ENV['APN_PRIVATE_KEY'])
28
+ @team_id = ENV['APN_TEAM_ID']
29
+ @key_id = ENV['APN_KEY_ID']
30
+ @timeout = Float(ENV['APN_TIMEOUT'] || 2.0)
31
+ end
32
+
33
+ def jwt_http2_post(topic, payload, token)
34
+ ec_key = OpenSSL::PKey::EC.new(@private_key)
35
+ jwt_token = JWT.encode({iss: @team_id, iat: Time.now.to_i}, ec_key, 'ES256', {kid: @key_id})
36
+
37
+ client = NetHttp2::Client.new(@jwt_uri)
38
+ h = {}
39
+ h['content-type'] = 'application/json'
40
+ h['apns-expiration'] = '0'
41
+ h['apns-priority'] = '10'
42
+ h['apns-topic'] = topic
43
+ h['authorization'] = "bearer #{jwt_token}"
44
+ res = client.call(:post, '/3/device/'+token, body: payload.to_json, timeout: @timeout,
45
+ headers: h)
46
+ client.close
47
+ return nil if res.status.to_i == 200
48
+ res.body
49
+ end
50
+
51
+ def push(*notifications)
52
+ return if notifications.empty?
53
+
54
+ notifications.flatten!
55
+
56
+ notifications.each_with_index do |notification, index|
57
+ next unless notification.kind_of?(Notification)
58
+ next if notification.sent?
59
+ next unless notification.valid?
60
+
61
+ notification.id = index
62
+
63
+ err = jwt_http2_post(notification.topic, notification.payload, notification.token)
64
+ if err == nil
65
+ notification.mark_as_sent!
66
+ else
67
+ puts err
68
+ notification.apns_error_code = err
69
+ notification.mark_as_unsent!
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,121 @@
1
+ require 'json'
2
+
3
+ module P8push
4
+ class Notification
5
+ class APNSError < RuntimeError
6
+ CODES = {
7
+ 0 => 'No errors encountered',
8
+ 1 => 'Processing error',
9
+ 2 => 'Missing device token',
10
+ 3 => 'Missing topic',
11
+ 4 => 'Missing payload',
12
+ 5 => 'Invalid token size',
13
+ 6 => 'Invalid topic size',
14
+ 7 => 'Invalid payload size',
15
+ 8 => 'Invalid token',
16
+ 10 => 'Shutdown',
17
+ 255 => 'Unknown error'
18
+ }
19
+
20
+ attr_reader :code
21
+
22
+ def initialize(code)
23
+ raise ArgumentError unless CODES.include?(code)
24
+ super(CODES[code])
25
+ @code = code
26
+ end
27
+ end
28
+
29
+ MAXIMUM_PAYLOAD_SIZE = 2048
30
+
31
+ attr_accessor :topic, :token, :alert, :badge, :sound, :category, :content_available, :mutable_content,
32
+ :custom_data, :id, :expiry, :priority
33
+ attr_reader :sent_at
34
+ attr_writer :apns_error_code
35
+
36
+ alias :device :token
37
+ alias :device= :token=
38
+
39
+ def initialize(options = {})
40
+ @token = options.delete(:token) || options.delete(:device)
41
+ @alert = options.delete(:alert)
42
+ @topic = options.delete(:topic) || ENV['APN_BUNDLE_ID']
43
+ @badge = options.delete(:badge)
44
+ @sound = options.delete(:sound)
45
+ @category = options.delete(:category)
46
+ @expiry = options.delete(:expiry)
47
+ @id = options.delete(:id)
48
+ @priority = options.delete(:priority)
49
+ @content_available = options.delete(:content_available)
50
+ @mutable_content = options.delete(:mutable_content)
51
+
52
+ @custom_data = options
53
+ end
54
+
55
+ def payload
56
+ json = {}.merge(@custom_data || {}).inject({}) { |h, (k, v)| h[k.to_s] = v; h }
57
+
58
+ json['aps'] ||= {}
59
+ json['aps']['alert'] = @alert if @alert
60
+ json['aps']['badge'] = @badge.to_i rescue 0 if @badge
61
+ json['aps']['sound'] = @sound if @sound
62
+ json['aps']['category'] = @category if @category
63
+ json['aps']['content-available'] = 1 if @content_available
64
+ json['aps']['mutable-content'] = 1 if @mutable_content
65
+
66
+ json
67
+ end
68
+
69
+ def message
70
+ data = [device_token_item,
71
+ payload_item,
72
+ identifier_item,
73
+ expiration_item,
74
+ priority_item].compact.join
75
+ [2, data.bytes.count, data].pack('cNa*')
76
+ end
77
+
78
+ def mark_as_sent!
79
+ @sent_at = Time.now
80
+ end
81
+
82
+ def mark_as_unsent!
83
+ @sent_at = nil
84
+ end
85
+
86
+ def sent?
87
+ !!@sent_at
88
+ end
89
+
90
+ def valid?
91
+ payload.to_json.bytesize <= MAXIMUM_PAYLOAD_SIZE
92
+ end
93
+
94
+ def error
95
+ APNSError.new(@apns_error_code) if @apns_error_code && @apns_error_code.nonzero?
96
+ end
97
+
98
+ private
99
+
100
+ def device_token_item
101
+ [1, 32, @token.gsub(/[<\s>]/, '')].pack('cnH64')
102
+ end
103
+
104
+ def payload_item
105
+ json = payload.to_json
106
+ [2, json.bytes.count, json].pack('cna*')
107
+ end
108
+
109
+ def identifier_item
110
+ [3, 4, @id].pack('cnN') unless @id.nil?
111
+ end
112
+
113
+ def expiration_item
114
+ [4, 4, @expiry.to_i].pack('cnN') unless @expiry.nil?
115
+ end
116
+
117
+ def priority_item
118
+ [5, 1, @priority].pack('cnc') unless @priority.nil?
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,3 @@
1
+ module P8push
2
+ VERSION = '1.0.1'
3
+ end
data/lib/p8push.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'p8push/version'
2
+ require 'p8push/client'
3
+ require 'p8push/notification'
data/p8push-1.0.1.gem ADDED
Binary file
data/p8push.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'p8push/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'p8push'
7
+ s.authors = ['Andrew Arrow','Mattt Thompson']
8
+ s.email = 'oneone@gmail.com'
9
+ s.license = 'MIT'
10
+ s.homepage = 'https://higher.team'
11
+ s.version = P8push::VERSION
12
+ s.platform = Gem::Platform::RUBY
13
+ s.summary = 'Send Apple Push Notifications'
14
+ s.description = 'apple push notifications using only the new p8 format not the older pem format'
15
+
16
+ s.add_dependency 'commander', '~> 4.4'
17
+ s.add_dependency 'json', '~> 0'
18
+ s.add_dependency 'net-http2', '~> 0'
19
+
20
+ s.add_development_dependency 'rspec', '~> 3.5'
21
+ s.add_development_dependency 'rake', '~> 0'
22
+ s.add_development_dependency 'simplecov', '~> 0'
23
+
24
+ s.files = Dir['./**/*'].reject { |file| file =~ /\.\/(bin|log|pkg|script|spec|test|vendor)/ }
25
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
26
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
27
+ s.require_paths = ['lib']
28
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: p8push
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Arrow
8
+ - Mattt Thompson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2017-06-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: commander
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '4.4'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '4.4'
28
+ - !ruby/object:Gem::Dependency
29
+ name: json
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: net-http2
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rspec
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '3.5'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '3.5'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rake
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: simplecov
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ description: apple push notifications using only the new p8 format not the older pem
99
+ format
100
+ email: oneone@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - "./Gemfile"
106
+ - "./LICENSE"
107
+ - "./README.md"
108
+ - "./lib/p8push.rb"
109
+ - "./lib/p8push/client.rb"
110
+ - "./lib/p8push/notification.rb"
111
+ - "./lib/p8push/version.rb"
112
+ - "./p8push-1.0.1.gem"
113
+ - "./p8push.gemspec"
114
+ homepage: https://higher.team
115
+ licenses:
116
+ - MIT
117
+ metadata: {}
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 2.6.8
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: Send Apple Push Notifications
138
+ test_files: []