pushmeup 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rvmrc ADDED
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
+ # Only full ruby name is supported here, for short names use:
8
+ # echo "rvm use 1.9.3" > .rvmrc
9
+ environment_id="ruby-1.9.3-p125@pushmeup"
10
+
11
+ # Uncomment the following lines if you want to verify rvm version per project
12
+ # rvmrc_rvm_version="1.11.5 (master)" # 1.10.1 seams as a safe start
13
+ # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
+ # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
+ # return 1
16
+ # }
17
+
18
+ # First we attempt to load the desired environment directly from the environment
19
+ # file. This is very fast and efficient compared to running through the entire
20
+ # CLI and selector. If you want feedback on which environment was used then
21
+ # insert the word 'use' after --create as this triggers verbose mode.
22
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
23
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
24
+ then
25
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
26
+ [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
27
+ \. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
28
+ else
29
+ # If the environment file has not yet been created, use the RVM CLI to select.
30
+ rvm --create "$environment_id" || {
31
+ echo "Failed to create RVM environment '${environment_id}'."
32
+ return 1
33
+ }
34
+ fi
35
+
36
+ # If you use bundler, this might be useful to you:
37
+ # if [[ -s Gemfile ]] && {
38
+ # ! builtin command -v bundle >/dev/null ||
39
+ # builtin command -v bundle | grep $rvm_path/bin/bundle >/dev/null
40
+ # }
41
+ # then
42
+ # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
43
+ # gem install bundler
44
+ # fi
45
+ # if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
46
+ # then
47
+ # bundle install | grep -vE '^Using|Your bundle is complete'
48
+ # fi
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - jruby-18mode # JRuby in 1.8 mode
7
+ - jruby-19mode # JRuby in 1.9 mode
8
+ - rbx-18mode
9
+ - rbx-19mode
10
+ # uncomment this line if your project needs to run something other than `rake`:
11
+ # script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pushmeup.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Nicos Karalis
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.
@@ -0,0 +1,19 @@
1
+ # Periodic Table
2
+
3
+ ## Installation
4
+
5
+ gem install pushmeup
6
+
7
+ ## Usage
8
+
9
+ require 'pushmeup'
10
+
11
+ # TODO: Make usage cases
12
+
13
+ ## Contributing
14
+
15
+ 1. Fork it
16
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
17
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
18
+ 4. Push to the branch (`git push origin my-new-feature`)
19
+ 5. Create new Pull Request
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new
6
+
7
+ task :default => :spec
8
+ task :test => :spec
@@ -0,0 +1,3 @@
1
+ require "pushmeup/version"
2
+ require "pushmeup/apple"
3
+ require "pushmeup/android"
@@ -0,0 +1 @@
1
+ require "pushmeup/gcm/core"
@@ -0,0 +1,95 @@
1
+ require 'socket'
2
+ require 'openssl'
3
+ require 'json'
4
+
5
+ module APNS
6
+
7
+ @host = 'gateway.sandbox.push.apple.com'
8
+ @port = 2195
9
+ # openssl pkcs12 -in mycert.p12 -out client-cert.pem -nodes -clcerts
10
+ @pem = nil # this should be the path of the pem file not the contentes
11
+ @pass = nil
12
+
13
+ class << self
14
+ attr_accessor :host, :pem, :port, :pass
15
+ end
16
+
17
+ def self.send_notification(device_token, message)
18
+ n = APNS::Notification.new(device_token, message)
19
+ self.send_notifications([n])
20
+ end
21
+
22
+ def self.send_notifications(notifications)
23
+ sock, ssl = self.open_connection
24
+
25
+ notifications.each do |n|
26
+ ssl.write(n.packaged_notification)
27
+ end
28
+
29
+ ssl.close
30
+ sock.close
31
+ end
32
+
33
+ def self.feedback
34
+ raise "Not implemented yet"
35
+ # sock, ssl = self.feedback_connection
36
+ #
37
+ # apns_feedback = []
38
+ # while line = ssl.read(38) # Read lines from the socket
39
+ # line.strip!
40
+ # f = line.unpack('N1n1H140')
41
+ # apns_feedback << [Time.at(f[0]), f[1], f[2]]
42
+ # end
43
+ #
44
+ # ssl.close
45
+ # sock.close
46
+ #
47
+ # return apns_feedback
48
+ end
49
+
50
+ protected
51
+
52
+ def self.open_connection
53
+ raise "The path to your pem file is not set. (APNS.pem = /path/to/cert.pem)" unless self.pem
54
+ raise "The path to your pem file does not exist!" unless File.exist?(self.pem)
55
+
56
+ context = OpenSSL::SSL::SSLContext.new
57
+ context.cert = OpenSSL::X509::Certificate.new(File.read(self.pem))
58
+ context.key = OpenSSL::PKey::RSA.new(File.read(self.pem), self.pass)
59
+
60
+ sock = TCPSocket.new(self.host, self.port)
61
+ ssl = OpenSSL::SSL::SSLSocket.new(sock,context)
62
+ ssl.connect
63
+
64
+ return sock, ssl
65
+ end
66
+
67
+ # def self.feedback_connection
68
+ # raise "The path to your pem file is not set. (APNS.pem = /path/to/cert.pem)" unless self.pem
69
+ # raise "The path to your pem file does not exist!" unless File.exist?(self.pem)
70
+ #
71
+ # context = OpenSSL::SSL::SSLContext.new
72
+ # context.cert = OpenSSL::X509::Certificate.new(File.read(self.pem))
73
+ # context.key = OpenSSL::PKey::RSA.new(File.read(self.pem), self.pass)
74
+ # context.verify_mode = OpenSSL::SSL::VERIFY_NONE
75
+ #
76
+ # fhost = self.host.gsub('gateway','feedback')
77
+ # puts fhost
78
+ #
79
+ # sock = TCPSocket.new(fhost, 2196)
80
+ # ssl = OpenSSL::SSL::SSLSocket.new(sock,context)
81
+ # # ssl.connect
82
+ #
83
+ # ssl.sync_close = true
84
+ # ssl.connect
85
+ #
86
+ # ssl.puts("GET / HTTP/1.0")
87
+ # ssl.puts("")
88
+ # while line = ssl.gets
89
+ # puts line
90
+ # end
91
+ #
92
+ # return sock, ssl
93
+ # end
94
+
95
+ end
@@ -0,0 +1,39 @@
1
+ module APNS
2
+ class Notification
3
+ attr_accessor :device_token, :alert, :badge, :sound, :other
4
+
5
+ def initialize(device_token, message)
6
+ self.device_token = device_token
7
+ if message.is_a?(Hash)
8
+ self.alert = message[:alert]
9
+ self.badge = message[:badge]
10
+ self.sound = message[:sound]
11
+ self.other = message[:other]
12
+ elsif message.is_a?(String)
13
+ self.alert = message
14
+ else
15
+ raise "Notification needs to have either a hash or string"
16
+ end
17
+ end
18
+
19
+ def packaged_notification
20
+ pt = self.packaged_token
21
+ pm = self.packaged_message
22
+ [0, 0, 32, pt, 0, pm.bytesize, pm].pack("ccca*cca*")
23
+ end
24
+
25
+ def packaged_token
26
+ [device_token.gsub(/[\s|<|>]/,'')].pack('H*')
27
+ end
28
+
29
+ def packaged_message
30
+ aps = {'aps'=> {} }
31
+ aps['aps']['alert'] = self.alert if self.alert
32
+ aps['aps']['badge'] = self.badge if self.badge
33
+ aps['aps']['sound'] = self.sound if self.sound
34
+ aps.merge!(self.other) if self.other
35
+ aps.to_json
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,2 @@
1
+ require "pushmeup/apns/core"
2
+ require "pushmeup/apns/notification"
@@ -0,0 +1,69 @@
1
+ require 'httparty'
2
+ require 'cgi'
3
+ require 'json'
4
+
5
+ class GCM
6
+ include HTTParty
7
+
8
+ @host = 'https://android.googleapis.com/gcm/send'
9
+
10
+ @base_uri = 'https://android.googleapis.com/gcm/send'
11
+ @timeout = 30
12
+ @format = :json
13
+ @key = nil
14
+
15
+ class << self
16
+ attr_accessor :base_uri, :timeout, :format, :key
17
+ end
18
+
19
+ # {
20
+ # "collapse_key": "score_update",
21
+ # "time_to_live": 108,
22
+ # "delay_while_idle": true,
23
+ # "registration_ids": ["4", "8", "15", "16", "23", "42"],
24
+ # "data" : {
25
+ # "score": "5x1",
26
+ # "time": "15:10"
27
+ # }
28
+ # }
29
+ # gcm = GCM.new(api_key)
30
+ # gcm.send_notification({registration_ids: ["4sdsx", "8sdsd"], data: {score: "5x1"}})
31
+ def self.send_notification(registration_ids, options = {})
32
+ post_body = build_post_body(registration_ids, options)
33
+
34
+ params = {
35
+ :body => post_body.to_json,
36
+ :headers => {
37
+ 'Authorization' => "key=#{@key}",
38
+ 'Content-Type' => 'application/json',
39
+ }
40
+ }
41
+
42
+ response = self.post(@base_uri, params)
43
+ build_response(response)
44
+ # {body: response.body, headers: response.headers, status: response.code}
45
+ end
46
+
47
+ private
48
+
49
+ def self.build_post_body(registration_ids, options={})
50
+ body = {registration_ids: registration_ids}.merge(options)
51
+ #p body
52
+ #raise exception if options[:time_to_live] && !options[:collapse_key]
53
+ end
54
+
55
+ def self.build_response(response)
56
+ case response.code
57
+ when 200
58
+ {response: 'success', body: response.body, headers: response.headers, status_code: response.code}
59
+ when 400
60
+ {response: 'Only applies for JSON requests. Indicates that the request could not be parsed as JSON, or it contained invalid fields.', status_code: response.code}
61
+ when 401
62
+ {response: 'There was an error authenticating the sender account.', status_code: response.code}
63
+ when 500
64
+ {response: 'There was an internal error in the GCM server while trying to process the request.', status_code: response.code}
65
+ when 503
66
+ {response: 'Server is temporarily unavailable.', status_code: response.code}
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,3 @@
1
+ module Pushmeup
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,34 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "pushmeup/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'pushmeup'
7
+ s.version = Pushmeup::VERSION
8
+ s.authors = ["Nicos Karalis"]
9
+ s.email = ["nicoskaralis@me.com"]
10
+
11
+ s.homepage = "https://github.com/NicosKaralis/pushmeup"
12
+ s.summary = %q{Send push notifications to Apple devices through ANPS and Android devices through GCM}
13
+ s.description = <<-DESC
14
+ This gem is a wrapper to send push notifications to devices.
15
+ Currently it only sends to Android or iOS devices, but more platforms will be added soon.
16
+
17
+ With APNS (Apple Push Notifications Service) you can send push notifications to Apple devices.
18
+ With GCM (Google Cloud Messaging) you can send push notifications to Android devices.
19
+ DESC
20
+
21
+ s.rubyforge_project = "pushmeup"
22
+
23
+ s.files = `git ls-files`.split("\n")
24
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
25
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
26
+
27
+ s.require_paths = ["lib"]
28
+
29
+ s.add_dependency('httparty')
30
+ s.add_dependency('json')
31
+
32
+ s.add_development_dependency 'rake'
33
+ s.add_development_dependency 'rspec'
34
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe Pushmeup do
4
+ describe "APNS" do
5
+ it "should have a APNS object" do
6
+ defined?(APNS).should_not be_false
7
+ end
8
+
9
+ it "should not forget the APNS default parameters" do
10
+ APNS.host.should == "gateway.sandbox.push.apple.com"
11
+ APNS.port.should be_equal(2195)
12
+ APNS.pem.should be_equal(nil)
13
+ APNS.pass.should be_equal(nil)
14
+ end
15
+
16
+ end
17
+
18
+ describe "GCM" do
19
+ it "should have a APNS object" do
20
+ defined?(GCM).should_not be_false
21
+ end
22
+ end
23
+ end
@@ -0,0 +1 @@
1
+ require 'pushmeup'
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pushmeup
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nicos Karalis
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httparty
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: json
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
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: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
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: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '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: '0'
78
+ description: ! " This gem is a wrapper to send push notifications
79
+ to devices.\n Currently it only sends to Android or iOS devices,
80
+ but more platforms will be added soon.\n\n With APNS (Apple
81
+ Push Notifications Service) you can send push notifications to Apple devices.\n
82
+ \ With GCM (Google Cloud Messaging) you can send push notifications
83
+ to Android devices.\n"
84
+ email:
85
+ - nicoskaralis@me.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .rvmrc
92
+ - .travis.yml
93
+ - Gemfile
94
+ - LICENSE
95
+ - README.md
96
+ - Rakefile
97
+ - lib/pushmeup.rb
98
+ - lib/pushmeup/android.rb
99
+ - lib/pushmeup/apns/core.rb
100
+ - lib/pushmeup/apns/notification.rb
101
+ - lib/pushmeup/apple.rb
102
+ - lib/pushmeup/gcm/core.rb
103
+ - lib/pushmeup/version.rb
104
+ - pushmeup.gemspec
105
+ - spec/lib/pushmeup_spec.rb
106
+ - spec/spec_helper.rb
107
+ homepage: https://github.com/NicosKaralis/pushmeup
108
+ licenses: []
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ segments:
120
+ - 0
121
+ hash: -3561276715261249912
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ! '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ segments:
129
+ - 0
130
+ hash: -3561276715261249912
131
+ requirements: []
132
+ rubyforge_project: pushmeup
133
+ rubygems_version: 1.8.24
134
+ signing_key:
135
+ specification_version: 3
136
+ summary: Send push notifications to Apple devices through ANPS and Android devices
137
+ through GCM
138
+ test_files:
139
+ - spec/lib/pushmeup_spec.rb
140
+ - spec/spec_helper.rb