push_to_devices 0.0.5

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.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ .DS_Store
2
+ *.gem
3
+ .bundle
4
+ Gemfile.lock
5
+ pkg/*
6
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ group :development, :test do
6
+ gem 'rake'
7
+ gem 'rspec'
8
+ gem 'webmock'
9
+ end
data/Rakefile ADDED
@@ -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 do |t|
6
+ end
7
+
8
+ task :default => :spec
@@ -0,0 +1,190 @@
1
+ require "net/http"
2
+ require "net/https"
3
+ require "active_support/core_ext/module/attribute_accessors"
4
+ require "cgi"
5
+
6
+ module PushToDevice
7
+
8
+ module Config
9
+ VERSION = '0.01'
10
+ end
11
+
12
+ class Exception < ::StandardError
13
+ attr_accessor :response_code, :response_body
14
+
15
+ # Pretty self explanatory stuff here...
16
+ def initialize(response_code, response_body)
17
+ @response_code = response_code
18
+ @response_body = response_body
19
+ super "Response was #{response_code}, #{response_body}"
20
+ end
21
+ end
22
+
23
+ module API
24
+
25
+ mattr_accessor :client_id
26
+ @@client_id = ""
27
+
28
+ mattr_accessor :client_secret
29
+ @@client_secret = ""
30
+
31
+ mattr_accessor :user_agent
32
+ @@user_agent = "PushToDevice RB #{PushToDevice::Config::VERSION}"
33
+
34
+ mattr_accessor :use_ssl
35
+ @@use_ssl = true
36
+
37
+ mattr_accessor :debug
38
+ @@debug = true
39
+
40
+ mattr_accessor :api_host
41
+ @@api_host = ""
42
+
43
+ mattr_accessor :client_info
44
+ @@client_info = {version: PushToDevice::Config::VERSION}
45
+
46
+ def self.configure
47
+ yield self if block_given?
48
+ end
49
+
50
+ def self.get(endpoint, params={})
51
+
52
+ # Set up the HTTP connection
53
+ http = Net::HTTP.new(
54
+ @@api_host,
55
+ @@use_ssl == true ? 443 : 80
56
+ )
57
+ http.use_ssl = (@@use_ssl == true)
58
+ if @@debug == true
59
+ http.set_debug_output($stdout)
60
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
61
+ end
62
+
63
+ if params.empty?
64
+ uri="/"+endpoint
65
+ else
66
+ query_string = params.map {|k, v|
67
+ "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"
68
+ }.join("&")
69
+ uri = "/"+endpoint+"?"+query_string
70
+ end
71
+
72
+ # Set up the request
73
+ request = Net::HTTP::Get.new(uri)
74
+
75
+ # Set credentials
76
+ client_credentials = generate_client_credentials
77
+ request["server-client-id"] = client_credentials[:client_id]
78
+ request["client-sig"] = client_credentials[:client_sig]
79
+ request["timestamp"] = client_credentials[:timestamp]
80
+
81
+ # Fire the package !
82
+ response = http.start {|http|
83
+ http.request request
84
+ }
85
+
86
+ self.handle_response(response)
87
+ end
88
+
89
+ def self.post(endpoint, params = {})
90
+
91
+ # Set up the HTTP connection
92
+ http = Net::HTTP.new(
93
+ @@api_host,
94
+ @@use_ssl == true ? 443 : 80
95
+ )
96
+ http.use_ssl = (@@use_ssl == true)
97
+ if @@debug == true
98
+ http.set_debug_output($stdout)
99
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
100
+ end
101
+
102
+ request = Net::HTTP::Post.new("/"+endpoint, initheader = {'Content-Type' =>'application/json'})
103
+ request.body = params.to_json
104
+
105
+ # Set credentials
106
+ client_credentials = generate_client_credentials
107
+ request["server-client-id"] = client_credentials[:client_id]
108
+ request["client-sig"] = client_credentials[:client_sig]
109
+ request["timestamp"] = client_credentials[:timestamp]
110
+
111
+ # Fire the package !
112
+ response = http.start {|http|
113
+ http.request request
114
+ }
115
+
116
+ self.handle_response(response)
117
+ end
118
+
119
+ def self.generate_client_credentials
120
+ timestamp_s = Time.now.to_i.to_s
121
+ {
122
+ client_id: @@client_id,
123
+ client_sig: self.generate_client_sig(timestamp_s),
124
+ timestamp: timestamp_s
125
+ }
126
+ end
127
+
128
+ def self.generate_client_sig(timestamp_s)
129
+ OpenSSL::HMAC.hexdigest 'sha1', @@client_secret, timestamp_s
130
+ end
131
+
132
+ def self.handle_response(response)
133
+ if response.code.to_i != 200
134
+ raise PushToDevice::Exception.new(response.code, response.body)
135
+ else
136
+ response.body
137
+ end
138
+ end
139
+
140
+ # GETS to service/me
141
+ # Returns the body
142
+ def self.get_service_info(params = {})
143
+ self.get('services/me')
144
+ end
145
+
146
+ # POSTS to users/:unique_hash/notifications
147
+ # to create a notification for a user
148
+ # Expects the following
149
+ # {
150
+ # unique_hash: a unique hash of the user in your service,
151
+ # notification_data: a hash with the following
152
+ # {
153
+ # ios_specific_fields: a hash of what you want to send to your ios users,
154
+ # android_specific_fields: a hash of whaty ou want to send to your android users
155
+ # separated into {data: {}, options: {}}
156
+ # }
157
+ # }
158
+ def self.post_notification_to_user(params = {})
159
+ self.post("users/#{params.delete(:unique_hash)}/notifications", params.delete(:notification_data))
160
+ end
161
+
162
+ # POSTS to users/notifications
163
+ # to create a notification for a group of users
164
+ # Expects the following
165
+ # {
166
+ # unique_hashes: an array of unique hashes
167
+ # notification_data: a hash with the following
168
+ # {
169
+ # ios_specific_fields: a hash of what you want to send to your ios users,
170
+ # android_specific_fields: a hash of whaty ou want to send to your android users
171
+ # separated into {data: {}, options: {}}
172
+ # }
173
+ # }
174
+ def self.post_notification_to_users(params = {})
175
+ self.post("users/notifications", params)
176
+ end
177
+
178
+ # POSTS to users/ to register a user for push notifications
179
+ # Expects the following
180
+ # {
181
+ # unique_hash: a unique hash of the user in your service,
182
+ # apn_device_token: an apple ios device token,
183
+ # gcm_registration_id: gcm_registration_id
184
+ # }
185
+ def self.register_user_for_push(params = {})
186
+ self.post("users/", params)
187
+ end
188
+
189
+ end
190
+ end
@@ -0,0 +1,24 @@
1
+ Gem::Specification.new do |gem|
2
+ gem.name = %q{push_to_devices}
3
+ gem.version = "0.0.5"
4
+ gem.date = %q{2013-02-15}
5
+ gem.authors = ["Lloyd Meta"]
6
+ gem.email = ["lloydmeta@gmail.com"]
7
+ gem.homepage = "http://github.com/lloydmeta/push_to_devices_rb"
8
+ gem.description = %q{A Ruby library for interfacing with push_to_devices}
9
+ gem.summary = gem.description
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.require_paths = ["lib"]
15
+
16
+ gem.add_dependency 'json'
17
+
18
+ gem.add_development_dependency 'rake'
19
+ gem.add_development_dependency 'rspec'
20
+ gem.add_development_dependency 'webmock'
21
+ gem.add_development_dependency 'active_support'
22
+ gem.add_development_dependency 'i18n'
23
+
24
+ end
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+
3
+ describe PushToDevice do
4
+
5
+ before(:all) do
6
+ PushToDevice::API.configure do |config|
7
+ config.api_host = "nowhere.com"
8
+ config.client_id = "fakeclientid"
9
+ config.client_secret = "fakeclientsecret"
10
+ config.use_ssl = false
11
+ end
12
+ end
13
+
14
+ describe ".get_service_info" do
15
+
16
+ it "should make a request to the proper endpoint" do
17
+ PushToDevice::API.get_service_info
18
+ a_request(:get, "http://nowhere.com/services/me").should have_been_made
19
+ end
20
+
21
+ end
22
+
23
+ describe ".post_notification_to_user" do
24
+
25
+ it "should make a request to the proper endpoint" do
26
+ unique_hash = "1234123412"
27
+ notification_data = {
28
+ ios_specific_fields: {text: "ios"},
29
+ android_specific_fields: {text: "android"}
30
+ }
31
+ PushToDevice::API.post_notification_to_user(unique_hash: unique_hash, notification_data: notification_data)
32
+ a_request(:post, "http://nowhere.com/users/#{unique_hash}/notifications").with(body: notification_data.to_json).should have_been_made
33
+ end
34
+ end
35
+
36
+ describe ".post_notification_to_users" do
37
+
38
+ it "should make a request to the proper endpoint" do
39
+ unique_hashes = ["1234123412", "asdfasfdfa"]
40
+ notification_data = {
41
+ ios_specific_fields: {text: "ios"},
42
+ android_specific_fields: {text: "android"}
43
+ }
44
+ PushToDevice::API.post_notification_to_users(unique_hashes: unique_hashes, notification_data: notification_data)
45
+ a_request(:post, "http://nowhere.com/users/notifications").with(body: {unique_hashes: unique_hashes, notification_data: notification_data}.to_json).should have_been_made
46
+ end
47
+ end
48
+
49
+ describe ".register_user_for_push" do
50
+
51
+ it "should make a request to the proper endpoint with the proper body" do
52
+ data = {
53
+ unique_hash: "a unique hash of the user in your service",
54
+ apn_device_token: "an apple ios device token",
55
+ gcm_registration_id: "gcm_registration_id"
56
+ }
57
+ PushToDevice::API.register_user_for_push(data)
58
+ a_request(:post, "http://nowhere.com/users/").with(body: data.to_json).should have_been_made
59
+ end
60
+ end
61
+
62
+ end
@@ -0,0 +1,17 @@
1
+ require 'bundler/setup'
2
+ require 'rspec'
3
+ require 'push_to_device'
4
+ require 'webmock/rspec'
5
+ require 'active_support/core_ext'
6
+
7
+ Dir[File.expand_path('../support/**/*', __FILE__)].each { |f| require f }
8
+
9
+ RSpec.configure do |config|
10
+
11
+ config.before(:each) do
12
+ WebMock.reset!
13
+ WebMock.disable_net_connect!
14
+ stub_request(:any, /.*/).to_return(:body => {status: "ok"}.to_json)
15
+ end
16
+
17
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: push_to_devices
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Lloyd Meta
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: json
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: rake
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: rspec
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: webmock
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
+ - !ruby/object:Gem::Dependency
79
+ name: active_support
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
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: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: i18n
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
+ description: A Ruby library for interfacing with push_to_devices
111
+ email:
112
+ - lloydmeta@gmail.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - .gitignore
118
+ - .rspec
119
+ - Gemfile
120
+ - Rakefile
121
+ - lib/push_to_device.rb
122
+ - push_to_devices.gemspec
123
+ - spec/push_to_device/push_to_device_spec.rb
124
+ - spec/spec_helper.rb
125
+ homepage: http://github.com/lloydmeta/push_to_devices_rb
126
+ licenses: []
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ none: false
133
+ requirements:
134
+ - - ! '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ! '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ requirements: []
144
+ rubyforge_project:
145
+ rubygems_version: 1.8.24
146
+ signing_key:
147
+ specification_version: 3
148
+ summary: A Ruby library for interfacing with push_to_devices
149
+ test_files:
150
+ - spec/push_to_device/push_to_device_spec.rb
151
+ - spec/spec_helper.rb