push_to_devices 0.0.9 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -1
- data/lib/push_to_devices.rb +147 -147
- data/push_to_devices.gemspec +1 -4
- data/spec/spec_helper.rb +1 -1
- metadata +1 -33
data/README.md
CHANGED
@@ -42,7 +42,7 @@ The example below assumes a typical Rails-like setup (with a YAML file to hold y
|
|
42
42
|
push_server_config = YAML.load_file(Rails.root + 'config/push_server.yml')[rails_env].symbolize_keys
|
43
43
|
|
44
44
|
PushToDevices::API.configure do |config|
|
45
|
-
config.
|
45
|
+
config.host = push_server_config[:host]
|
46
46
|
config.client_id = push_server_config[:client_id]
|
47
47
|
config.client_secret = push_server_config[:client_secret]
|
48
48
|
config.use_ssl = push_server_config[:use_ssl]
|
data/lib/push_to_devices.rb
CHANGED
@@ -1,12 +1,11 @@
|
|
1
1
|
require "net/http"
|
2
2
|
require "net/https"
|
3
|
-
require "active_support/core_ext/module/attribute_accessors"
|
4
3
|
require "cgi"
|
5
4
|
|
6
5
|
module PushToDevices
|
7
6
|
|
8
7
|
module Config
|
9
|
-
VERSION = '0.
|
8
|
+
VERSION = '0.1.0'
|
10
9
|
end
|
11
10
|
|
12
11
|
class Exception < ::StandardError
|
@@ -22,172 +21,173 @@ module PushToDevices
|
|
22
21
|
|
23
22
|
module API
|
24
23
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
24
|
+
class << self
|
25
|
+
attr_accessor :client_id, :client_secret, :user_agent, :use_ssl, :debug, :host, :port, :client_info
|
26
|
+
|
27
|
+
def configure
|
28
|
+
# defaults
|
29
|
+
client_id = ""
|
30
|
+
client_secret = ""
|
31
|
+
user_agent = "PushToDevices RB #{PushToDevices::Config::VERSION}"
|
32
|
+
use_ssl = true
|
33
|
+
debug = true
|
34
|
+
host = ""
|
35
|
+
port = 80
|
36
|
+
client_info = {version: PushToDevices::Config::VERSION}
|
37
|
+
|
38
|
+
yield self
|
39
|
+
end
|
30
40
|
|
31
|
-
|
32
|
-
@@user_agent = "PushToDevices RB #{PushToDevices::Config::VERSION}"
|
41
|
+
def get(endpoint, params={})
|
33
42
|
|
34
|
-
|
35
|
-
|
43
|
+
# Set up the HTTP connection
|
44
|
+
http = Net::HTTP.new(
|
45
|
+
api_host,
|
46
|
+
api_port
|
47
|
+
)
|
48
|
+
http.use_ssl = (use_ssl == true)
|
49
|
+
if debug == true
|
50
|
+
http.set_debug_output($stdout)
|
51
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
52
|
+
end
|
36
53
|
|
37
|
-
|
38
|
-
@@debug = true
|
54
|
+
uri = self.generate_uri_from_params(endpoint, params)
|
39
55
|
|
40
|
-
|
41
|
-
|
56
|
+
# Set up the request
|
57
|
+
request = Net::HTTP::Get.new(uri)
|
42
58
|
|
43
|
-
|
44
|
-
|
59
|
+
# Set credentials
|
60
|
+
client_credentials = generate_client_credentials
|
61
|
+
request["server-client-id"] = client_credentials[:client_id]
|
62
|
+
request["client-sig"] = client_credentials[:client_sig]
|
63
|
+
request["timestamp"] = client_credentials[:timestamp]
|
45
64
|
|
46
|
-
|
47
|
-
|
48
|
-
|
65
|
+
# Fire the package !
|
66
|
+
response = http.start {|http|
|
67
|
+
http.request request
|
68
|
+
}
|
49
69
|
|
50
|
-
|
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
|
70
|
+
self.handle_response(response)
|
61
71
|
end
|
62
72
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
http.set_debug_output($stdout)
|
92
|
-
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
73
|
+
def post(endpoint, params = {})
|
74
|
+
|
75
|
+
# Set up the HTTP connection
|
76
|
+
http = Net::HTTP.new(
|
77
|
+
api_host,
|
78
|
+
api_port
|
79
|
+
)
|
80
|
+
http.use_ssl = (use_ssl == true)
|
81
|
+
if debug == true
|
82
|
+
http.set_debug_output($stdout)
|
83
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
84
|
+
end
|
85
|
+
|
86
|
+
request = Net::HTTP::Post.new("/"+endpoint, initheader = {'Content-Type' =>'application/json'})
|
87
|
+
request.body = params.to_json
|
88
|
+
|
89
|
+
# Set credentials
|
90
|
+
client_credentials = generate_client_credentials
|
91
|
+
request["server-client-id"] = client_credentials[:client_id]
|
92
|
+
request["client-sig"] = client_credentials[:client_sig]
|
93
|
+
request["timestamp"] = client_credentials[:timestamp]
|
94
|
+
|
95
|
+
# Fire the package !
|
96
|
+
response = http.start {|http|
|
97
|
+
http.request request
|
98
|
+
}
|
99
|
+
|
100
|
+
self.handle_response(response)
|
93
101
|
end
|
94
102
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
request["timestamp"] = client_credentials[:timestamp]
|
103
|
-
|
104
|
-
# Fire the package !
|
105
|
-
response = http.start {|http|
|
106
|
-
http.request request
|
107
|
-
}
|
108
|
-
|
109
|
-
self.handle_response(response)
|
110
|
-
end
|
103
|
+
def api_port
|
104
|
+
if port
|
105
|
+
port
|
106
|
+
else
|
107
|
+
use_ssl ? 443 : 80
|
108
|
+
end
|
109
|
+
end
|
111
110
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
111
|
+
def generate_uri_from_params(endpoint, params)
|
112
|
+
if params.empty?
|
113
|
+
"/#{endpoint}"
|
114
|
+
else
|
115
|
+
query_string = params.map {|k, v|
|
116
|
+
"#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"
|
117
|
+
}.join("&")
|
118
|
+
"/#{endpoint}?#{query_string}"
|
119
|
+
end
|
120
120
|
end
|
121
|
-
end
|
122
121
|
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
122
|
+
def generate_client_credentials
|
123
|
+
timestamp_s = Time.now.to_i.to_s
|
124
|
+
{
|
125
|
+
client_id: client_id,
|
126
|
+
client_sig: self.generate_client_sig(timestamp_s),
|
127
|
+
timestamp: timestamp_s
|
128
|
+
}
|
129
|
+
end
|
131
130
|
|
132
|
-
|
133
|
-
|
134
|
-
|
131
|
+
def generate_client_sig(timestamp_s)
|
132
|
+
OpenSSL::HMAC.hexdigest 'sha1', client_secret, timestamp_s
|
133
|
+
end
|
135
134
|
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
135
|
+
def handle_response(response)
|
136
|
+
if response.code.to_i != 200
|
137
|
+
raise PushToDevices::Exception.new(response.code, response.body)
|
138
|
+
else
|
139
|
+
response.body
|
140
|
+
end
|
141
141
|
end
|
142
|
-
end
|
143
142
|
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
143
|
+
# GETS to service/me
|
144
|
+
# Returns the body
|
145
|
+
def get_service_info(params = {})
|
146
|
+
self.get('services/me')
|
147
|
+
end
|
149
148
|
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
149
|
+
# POSTS to users/:unique_hash/notifications
|
150
|
+
# to create a notification for a user
|
151
|
+
# Expects the following
|
152
|
+
# {
|
153
|
+
# unique_hash: a unique hash of the user in your service,
|
154
|
+
# notification_data: a hash with the following
|
155
|
+
# {
|
156
|
+
# ios_specific_fields: a hash of what you want to send to your ios users,
|
157
|
+
# android_specific_fields: a hash of whaty ou want to send to your android users
|
158
|
+
# separated into {data: {}, options: {}}
|
159
|
+
# }
|
160
|
+
# }
|
161
|
+
def post_notification_to_user(params = {})
|
162
|
+
self.post("users/#{params.delete(:unique_hash)}/notifications", params.delete(:notification_data))
|
163
|
+
end
|
165
164
|
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
165
|
+
# POSTS to users/notifications
|
166
|
+
# to create a notification for a group of users
|
167
|
+
# Expects the following
|
168
|
+
# {
|
169
|
+
# unique_hashes: an array of unique hashes
|
170
|
+
# notification_data: a hash with the following
|
171
|
+
# {
|
172
|
+
# ios_specific_fields: a hash of what you want to send to your ios users,
|
173
|
+
# android_specific_fields: a hash of whaty ou want to send to your android users
|
174
|
+
# separated into {data: {}, options: {}}
|
175
|
+
# }
|
176
|
+
# }
|
177
|
+
def post_notification_to_users(params = {})
|
178
|
+
self.post("users/notifications", params)
|
179
|
+
end
|
181
180
|
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
181
|
+
# POSTS to users/ to register a user for push notifications
|
182
|
+
# Expects the following
|
183
|
+
# {
|
184
|
+
# unique_hash: a unique hash of the user in your service,
|
185
|
+
# apn_device_token: an apple ios device token,
|
186
|
+
# gcm_registration_id: gcm_registration_id
|
187
|
+
# }
|
188
|
+
def register_user_for_push(params = {})
|
189
|
+
self.post("users/", params)
|
190
|
+
end
|
191
191
|
end
|
192
192
|
|
193
193
|
end
|
data/push_to_devices.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |gem|
|
2
2
|
gem.name = %q{push_to_devices}
|
3
|
-
gem.version = "0.0
|
3
|
+
gem.version = "0.1.0"
|
4
4
|
gem.date = %q{2013-02-15}
|
5
5
|
gem.authors = ["Lloyd Meta"]
|
6
6
|
gem.email = ["lloydmeta@gmail.com"]
|
@@ -18,7 +18,4 @@ Gem::Specification.new do |gem|
|
|
18
18
|
gem.add_development_dependency 'rake'
|
19
19
|
gem.add_development_dependency 'rspec'
|
20
20
|
gem.add_development_dependency 'webmock'
|
21
|
-
gem.add_development_dependency 'active_support'
|
22
|
-
gem.add_development_dependency 'i18n'
|
23
|
-
|
24
21
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: push_to_devices
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -75,38 +75,6 @@ dependencies:
|
|
75
75
|
- - ! '>='
|
76
76
|
- !ruby/object:Gem::Version
|
77
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
78
|
description: A Ruby library for interfacing with push_to_devices
|
111
79
|
email:
|
112
80
|
- lloydmeta@gmail.com
|