push_notifier 0.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: 1d3bdeb9a9a259e3a2df038ff6426e2dff737376
4
+ data.tar.gz: 9070bda9b11422e516833e0d8b50ea717c9c4530
5
+ SHA512:
6
+ metadata.gz: 0a29aa0f004c560ca484b3ab060910294f896e6b6f995f73f142a9a990b5385ae97a082e6ea59fab2d9349d684454e48b4db71f9afbd590c8020f38f39a51995
7
+ data.tar.gz: e37810287597d17316cce45e0701fb0c3b2e13696ba9174190d88c4d1863ffa9791ea867465d7a83cbe8d40599afbb5176a9e261361c671485e7f9fadad3320a
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,20 @@
1
+ language: ruby
2
+ cache: bundler
3
+
4
+ before_install:
5
+ - gem update --system
6
+ - gem --version
7
+
8
+ rvm:
9
+ - jruby
10
+ - 2.0.0
11
+ - 2.1.0
12
+
13
+ script: 'bundle exec rspec'
14
+
15
+ notifications:
16
+ email:
17
+ recipients:
18
+ - surajshirvankar@gmail.com
19
+ on_failure: change
20
+ on_success: never
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in push_notifier.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,11 @@
1
+ guard 'rspec', cmd: 'bundle exec rspec' do
2
+ # watch /lib/ files
3
+ watch(%r{^lib/(.+).rb$}) do |m|
4
+ "spec/#{m[1]}_spec.rb"
5
+ end
6
+
7
+ # watch /spec/ files
8
+ watch(%r{^spec/(.+).rb$}) do |m|
9
+ "spec/#{m[1]}.rb"
10
+ end
11
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Suraj Shirvankar
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # PushNotifier
2
+ [![Build Status](https://travis-ci.org/h0lyalg0rithm/PushNotifier.svg)](https://travis-ci.org/h0lyalg0rithm/PushNotifier)
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'push_notifier'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install push_notifier
19
+
20
+ ## Usage
21
+
22
+ TODO: Write usage instructions here
23
+
24
+ ## Contributing
25
+
26
+ 1. Fork it ( https://github.com/h0lyalg0rithm/push_notifier/fork )
27
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
28
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
29
+ 4. Push to the branch (`git push origin my-new-feature`)
30
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec) do |task|
5
+ task.rspec_opts bundl= ['--color', '--format', 'nested']
6
+ end
7
+
8
+ task :default => :spec
@@ -0,0 +1,42 @@
1
+ require 'uri'
2
+
3
+ module PushNotifier
4
+ class APNS
5
+ attr_reader :push_uri
6
+ attr_accessor :certificate, :passphrase
7
+
8
+ DEVELOPMENT_URI = 'tcp://gateway.sandbox.push.apple.com:2195'
9
+
10
+ PRODUCTION_URI = 'tcp://gateway.push.apple.com:2195'
11
+
12
+ def self.set_mode(environment)
13
+ case environment
14
+ when :production
15
+ uri = PRODUCTION_URI
16
+ else
17
+ uri = DEVELOPMENT_URI
18
+ end
19
+ @apns = self.new(uri)
20
+ end
21
+
22
+ def initialize(push_uri)
23
+ @push_uri = URI(push_uri)
24
+ end
25
+
26
+ def send_notification(notification)
27
+ bytes = ''
28
+ self.setup_connection do
29
+ payload = notification.payload
30
+ @connection.write(payload)
31
+ end
32
+ end
33
+
34
+ def setup_connection
35
+ @connection = Connection.new(@push_uri, @certificate, @passphrase)
36
+ @connection.open
37
+ yield if block_given?
38
+ @connection.close
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,39 @@
1
+ require 'socket'
2
+ require 'openssl'
3
+
4
+ module PushNotifier
5
+ class APNS
6
+ class Connection
7
+
8
+ attr_reader :uri, :certificate, :passphrase, :ssl, :socket
9
+
10
+ def initialize(uri, certificate, passphrase)
11
+ @uri = uri
12
+ @certificate = File.read(certificate)
13
+ @passphrase = passphrase
14
+ end
15
+
16
+ def open
17
+ @socket = TCPSocket.new(@uri.host, @uri.port)
18
+
19
+ ssl_context = OpenSSL::SSL::SSLContext.new
20
+
21
+ ssl_context.cert = OpenSSL::X509::Certificate.new @certificate
22
+ ssl_context.key = OpenSSL::PKey::RSA.new @certificate, @passphrase
23
+
24
+ @ssl = OpenSSL::SSL::SSLSocket.new @socket,ssl_context
25
+
26
+ @ssl.connect
27
+
28
+ end
29
+ def write(data)
30
+ @ssl.write(data)
31
+ end
32
+ def close
33
+ @ssl.close
34
+ @socket.close
35
+ @ssl, @socket = nil, nil
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,49 @@
1
+ require 'json'
2
+
3
+ module PushNotifier
4
+ class APNS
5
+ class Notification
6
+ attr_accessor :options, :token
7
+ def initialize(token, message, options={})
8
+ default_options
9
+ dynamic_accessor options
10
+ @token = clean_token token
11
+ @alert = message
12
+ @custom_data = options.reject { |x,y| allowed_attrs.include? x }
13
+ end
14
+
15
+ def default_options
16
+ @alert = ''
17
+ end
18
+
19
+ def clean_token(token)
20
+ [token.gsub!(/[<|>|\s]/,'')].pack('H*')
21
+ end
22
+
23
+ def data_message
24
+ aps = allowed_attrs.inject({}) do |acc,x|
25
+ acc[x.to_s] = self.send(x) unless self.send(x).nil?
26
+ acc
27
+ end
28
+ custom_data = aps.delete('custom_data')
29
+ custom_data[:aps] = aps
30
+ custom_data.to_json.gsub(/\\u([\da-fA-F]{4})/) {|m| [$1].pack("H*").unpack("n*").pack("U*")}
31
+ end
32
+
33
+ def allowed_attrs
34
+ [:alert, :badge, :category, :content_available, :sound, :custom_data, :priority]
35
+ end
36
+
37
+ def dynamic_accessor(option)
38
+ allowed_attrs.each do |x|
39
+ singleton_class.class_eval do; attr_accessor "#{x}"; end
40
+ self.send("#{x}=",option[x])
41
+ end
42
+ end
43
+ def payload
44
+ dm = data_message
45
+ [0, 0, 32, @token, 0, dm.bytesize, dm].pack("ccca*cca*")
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ require 'push_notifier/apns/apns'
2
+ require 'push_notifier/apns/connection'
3
+ require 'push_notifier/apns/notification'
@@ -0,0 +1,3 @@
1
+ module PushNotifier
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,7 @@
1
+ require 'push_notifier/version'
2
+ require 'push_notifier/apns'
3
+
4
+ module PushNotifier
5
+ # Your code goes here...
6
+
7
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'push_notifier/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'push_notifier'
8
+ spec.version = PushNotifier::VERSION
9
+ spec.authors = ['Suraj Shirvankar']
10
+ spec.email = ['surajshirvankar@gmail.com']
11
+ spec.summary = ""
12
+ spec.description = ""
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'rspec'
24
+ spec.add_development_dependency 'rspec-nc'
25
+ spec.add_development_dependency 'guard'
26
+ spec.add_development_dependency 'guard-rspec'
27
+ spec.add_development_dependency 'pry'
28
+ spec.add_development_dependency 'pry-remote'
29
+ spec.add_development_dependency 'pry-nav'
30
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe PushNotifier::APNS::Connection do
4
+ it 'stores initial content' do
5
+ allow(File).to receive(:read).and_return('2')
6
+ allow(Kernel).to receive(:URI).and_return('1')
7
+ @apns = PushNotifier::APNS::Connection.new('1','2','3')
8
+ expect(@apns.uri).to eq '1'
9
+ expect(@apns.certificate).to eq '2'
10
+ expect(@apns.passphrase).to eq '3'
11
+ end
12
+ describe 'connecting' do
13
+ before do
14
+ allow(File).to receive(:read).and_return('2')
15
+ @apns = PushNotifier::APNS::Connection.new(URI('http://apple.com:2915'),'2','3')
16
+ server = double('server').as_null_object
17
+ cert = double('cert')
18
+ key = double('key')
19
+ ssl = double('ssl').as_null_object
20
+ allow(TCPSocket).to receive(:new).and_return server
21
+ allow(OpenSSL::X509::Certificate).to receive(:new).and_return cert
22
+ allow(OpenSSL::SSL::SSLSocket).to receive(:new).and_return ssl
23
+ allow(OpenSSL::SSL::SSLSocket).to receive(:connect).and_return ssl
24
+ allow(OpenSSL::PKey::RSA).to receive(:new).and_return key
25
+ end
26
+ it 'opens new socket' do
27
+ # expect(@apns.open?).to eq true
28
+ end
29
+ it 'should close' do
30
+ @apns.open
31
+ @apns.close
32
+ expect(@apns.ssl).to eq nil
33
+ expect(@apns.socket).to eq nil
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe PushNotifier::APNS::Notification do
4
+ it 'sets default value ' do
5
+ @apns = PushNotifier::APNS::Notification.new '1234','message'
6
+ expect(@apns.alert).to eq 'message'
7
+ end
8
+ it 'cleans device token' do
9
+ @apns = PushNotifier::APNS::Notification.new '<12345>', 'message'
10
+ expect(@apns.token).to eq ["12345"].pack("H*")
11
+ end
12
+ it 'saves other info as custom data' do
13
+ @apns = PushNotifier::APNS::Notification.new '1234', 'message', data: 'test', sound: true
14
+ expect(@apns.custom_data[:data]).to eq 'test'
15
+ expect(@apns.custom_data[:sound]).to eq nil
16
+ end
17
+ it 'creats message data' do
18
+ @apns = PushNotifier::APNS::Notification.new '1234', 'message', data: 'test', sound: true
19
+ expect(JSON.parse(@apns.data_message)["aps"]["sound"]).to eq true
20
+ end
21
+ end
22
+
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ # API
4
+ # APNS = PushNotfier::APNS.set_mode :production
5
+ #
6
+ # @notification = PushNotifier::APNS::Notification.new token, message
7
+ #
8
+ # APNS.send(@notification)
9
+ # @notification.errors
10
+
11
+
12
+ describe PushNotifier::APNS do
13
+ it 'test works' do
14
+ expect(1).to eq 1
15
+ end
16
+
17
+ it 'should exist' do
18
+ expect(defined?(PushNotifier::APNS)).not_to eq nil
19
+ end
20
+ it 'have initial urls' do
21
+ expect(PushNotifier::APNS::DEVELOPMENT_URI).to match('sandbox')
22
+ # expect(PushNotifier::APNS::DEVELOPMENT_FEEDBACK_URI).to match('feedback')
23
+ # expect(PushNotifier::APNS::PRODUCTION_FEEDBACK_URI).to match('feedback')
24
+ expect(PushNotifier::APNS::PRODUCTION_URI).to match('push')
25
+ end
26
+
27
+ describe 'sets mode' do
28
+ it 'to production' do
29
+ @apns = PushNotifier::APNS.set_mode :production
30
+ expect(@apns.push_uri).to eq URI(PushNotifier::APNS::PRODUCTION_URI)
31
+ end
32
+ it 'to development' do
33
+ @apns = PushNotifier::APNS.set_mode :development
34
+ expect(@apns.push_uri).to eq URI(PushNotifier::APNS::DEVELOPMENT_URI)
35
+ end
36
+ it 'to development as default' do
37
+ @apns = PushNotifier::APNS.set_mode :development
38
+ expect(@apns.push_uri).to eq URI(PushNotifier::APNS::DEVELOPMENT_URI)
39
+ end
40
+ it 'return apns instance' do
41
+ @apns = PushNotifier::APNS.set_mode :test
42
+ expect(@apns).to be_a_kind_of PushNotifier::APNS
43
+ end
44
+ it 'should respond to send_notification' do
45
+ @apns = PushNotifier::APNS.set_mode :test
46
+ expect(@apns).to respond_to :send_notification
47
+ expect(@apns).not_to respond_to :send_crap
48
+ end
49
+ end
50
+
51
+ describe 'sends notification' do
52
+ let(:apns){ PushNotifier::APNS.set_mode :production }
53
+ it 'in production' do
54
+ # expect(@apns.send.response).to eq :success
55
+ end
56
+ end
57
+
58
+ end
59
+
60
+
@@ -0,0 +1,8 @@
1
+ require 'pry'
2
+ require 'push_notifier'
3
+ RSpec.configure do |config|
4
+
5
+ #add this line inside the block
6
+ config.expose_current_running_example_as :example
7
+
8
+ end
metadata ADDED
@@ -0,0 +1,192 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: push_notifier
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Suraj Shirvankar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-nc
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry-remote
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry-nav
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: ''
140
+ email:
141
+ - surajshirvankar@gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - ".travis.yml"
148
+ - Gemfile
149
+ - Guardfile
150
+ - LICENSE.txt
151
+ - README.md
152
+ - Rakefile
153
+ - lib/push_notifier.rb
154
+ - lib/push_notifier/apns.rb
155
+ - lib/push_notifier/apns/apns.rb
156
+ - lib/push_notifier/apns/connection.rb
157
+ - lib/push_notifier/apns/notification.rb
158
+ - lib/push_notifier/version.rb
159
+ - push_notifier.gemspec
160
+ - spec/connection_spec.rb
161
+ - spec/notification_spec.rb
162
+ - spec/push_notifier_spec.rb
163
+ - spec/spec_helper.rb
164
+ homepage: ''
165
+ licenses:
166
+ - MIT
167
+ metadata: {}
168
+ post_install_message:
169
+ rdoc_options: []
170
+ require_paths:
171
+ - lib
172
+ required_ruby_version: !ruby/object:Gem::Requirement
173
+ requirements:
174
+ - - ">="
175
+ - !ruby/object:Gem::Version
176
+ version: '0'
177
+ required_rubygems_version: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - ">="
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ requirements: []
183
+ rubyforge_project:
184
+ rubygems_version: 2.4.5
185
+ signing_key:
186
+ specification_version: 4
187
+ summary: ''
188
+ test_files:
189
+ - spec/connection_spec.rb
190
+ - spec/notification_spec.rb
191
+ - spec/push_notifier_spec.rb
192
+ - spec/spec_helper.rb