one_signal 0.0.14 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # OneSignal Ruby bindings
2
2
 
3
- This gem provides a simple SDK to access the [OneSignal API](https://documentation.onesignal.com/docs/server-api-overview).
3
+ This gem provides a simple SDK to access the [OneSignal API](https://documentation.onesignal.com/reference).
4
+
5
+ Also check my [onesignal-go](https://github.com/tbalthazar/onesignal-go) library.
4
6
 
5
7
  ## Installation
6
8
 
@@ -18,11 +20,15 @@ bundle exec rake
18
20
 
19
21
  ## Basic usage
20
22
 
21
- See basic examples in [example.rb](/example.rb)
23
+ See basic examples in [example.rb](/example.rb).
24
+ To run it:
25
+ - rename [.env.example](/.env.example) into `.env`
26
+ - set your `USER_AUTH_KEY` and `API_KEY` in the `.env` file
27
+ - run `ruby example.rb`
22
28
 
23
29
  ## Documentation
24
30
 
25
- Specify your User Auth key to deal with Apps :
31
+ Specify your User Auth key to deal with Apps:
26
32
 
27
33
  ```ruby
28
34
  OneSignal::OneSignal.user_auth_key = YOUR_USER_AUTH_KEY
@@ -35,7 +41,23 @@ OneSignal::OneSignal.api_key = YOUR_API_KEY
35
41
  ```
36
42
 
37
43
  Then call the following methods on the `App`, `Player` and `Notification` classes.
38
- The `params` argument in those methods is a ruby hash and the accepted/required keys for this hash are documented in the [OneSignal API documentation](https://documentation.onesignal.com/docs/server-api-overview)
44
+ The `params` argument in those methods is a ruby hash and the accepted/required keys for this hash are documented in the [OneSignal API documentation](https://documentation.onesignal.com/reference)
45
+
46
+ Each method also accepts an optional `opts` hash that allows you to specify the `user_auth_key`/`api_key` on a per method basis. It allows a ruby app to talk to several different OneSignal apps:
47
+
48
+ ```ruby
49
+ app_1_api_key = "fake api key 1"
50
+ app_2_api_key = "fake api key 2"
51
+
52
+ # by default, method calls will use app_1_api_key
53
+ OneSignal::OneSignal.api_key = app_1_api_key
54
+
55
+ # get player with id "123" in app_1
56
+ OneSignal::Player.get(id: "123")
57
+
58
+ # get player with id "456" in app_2
59
+ OneSignal::Player.get(id: "123", opts: {auth_key: app_2_api_key})
60
+ ```
39
61
 
40
62
  The return value of each method is a `Net::HTTPResponse`.
41
63
 
@@ -52,7 +74,7 @@ The return value of each method is a `Net::HTTPResponse`.
52
74
 
53
75
  ```ruby
54
76
  - OneSignal::Player.all(params:)
55
- - OneSignal::Player.csv_export(app_id:)
77
+ - OneSignal::Player.csv_export(params:)
56
78
  - OneSignal::Player.get(id:)
57
79
  - OneSignal::Player.create(params:)
58
80
  - OneSignal::Player.create_session(id:, params:)
data/example.rb CHANGED
@@ -1,309 +1,53 @@
1
1
  require 'one_signal'
2
2
  require 'dotenv'
3
3
 
4
+ # load your keys with https://github.com/bkeepers/dotenv
4
5
  Dotenv.load
5
6
  api_key = ENV['ONESIGNAL_API_KEY']
6
7
  user_auth_key = ENV['ONESIGNAL_USER_AUTH_KEY']
7
- @app_id = ENV['ONESIGNAL_APP_ID']
8
- @device_token = "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabc3"
9
- @example_player_id = ENV['EXAMPLE_PLAYER_ID']
10
8
 
9
+ # configure OneSignal
11
10
  OneSignal::OneSignal.api_key = api_key
12
11
  OneSignal::OneSignal.user_auth_key = user_auth_key
13
12
 
14
- def create_player
15
- params = {
16
- app_id: @app_id,
17
- device_type: 0,
18
- identifier: @device_token,
19
- tags: {
20
- user_id: 'test2',
21
- device_type: 'chrome'
22
- }
23
- }
24
-
25
- begin
26
- response = OneSignal::Player.create(params: params)
27
- puts "code : #{response.code}"
28
- puts "message : #{response.message}"
29
- puts "body : #{response.body}"
30
- puts "id : #{JSON.parse(response.body).class}"
31
- return JSON.parse(response.body)["id"]
32
- rescue OneSignal::OneSignalError => e
33
- puts "-- message : #{e.message}"
34
- puts "-- status : #{e.http_status}"
35
- puts "-- body : #{e.http_body}"
36
- end
37
- end
38
-
39
- def create_player_session(id:)
40
- params = {
41
- # app_id: @app_id,
42
- # device_type: 0,
43
- # identifier: @device_token,
44
- # tags: {
45
- # user_id: 'test2',
46
- # device_type: 'chrome'
47
- # }
48
- device_os: 'iPhone5,1',
49
- game_version: '1.0.6',
50
- timezone: -28800
51
- }
52
-
53
- begin
54
- response = OneSignal::Player.create_session(id: id, params: params)
55
- puts "code : #{response.code}"
56
- puts "message : #{response.message}"
57
- puts "body : #{response.body}"
58
- rescue OneSignal::OneSignalError => e
59
- puts "-- message : #{e.message}"
60
- puts "-- status : #{e.http_status}"
61
- puts "-- body : #{e.http_body}"
62
- end
63
- end
64
-
65
- def create_player_purchase(id:)
66
- params = {
67
- purchases: [
68
- {
69
- sku: 'SKU123',
70
- iso: 'USD',
71
- amount: '0.99'
72
- }
73
- ]
74
- }
75
-
76
- begin
77
- response = OneSignal::Player.create_purchase(id: id, params: params)
78
- puts "code : #{response.code}"
79
- puts "message : #{response.message}"
80
- puts "body : #{response.body}"
81
- rescue OneSignal::OneSignalError => e
82
- puts "-- message : #{e.message}"
83
- puts "-- status : #{e.http_status}"
84
- puts "-- body : #{e.http_body}"
85
- end
86
- end
87
-
88
- def create_player_focus(id:)
89
- params = {
90
- state: 'ping',
91
- active_time: 60
92
- }
93
-
94
- begin
95
- response = OneSignal::Player.create_focus(id: id, params: params)
96
- puts "code : #{response.code}"
97
- puts "message : #{response.message}"
98
- puts "body : #{response.body}"
99
- rescue OneSignal::OneSignalError => e
100
- puts "-- message : #{e.message}"
101
- puts "-- status : #{e.http_status}"
102
- puts "-- body : #{e.http_body}"
103
- end
104
- end
105
-
106
- def update_player(id:)
107
- params = {
108
- app_id: @app_id,
109
- device_type: 0,
110
- identifier: @device_token,
111
- tags: {
112
- user_id: 'test2updated',
113
- device_type: 'chrome'
114
- }
115
- }
116
-
117
- begin
118
- response = OneSignal::Player.update(id: id, params: params)
119
- puts "code : #{response.code}"
120
- puts "message : #{response.message}"
121
- puts "body : #{response.body}"
122
- puts "id : #{JSON.parse(response.body).class}"
123
- rescue OneSignal::OneSignalError => e
124
- puts "-- message : #{e.message}"
125
- puts "-- status : #{e.http_status}"
126
- puts "-- body : #{e.http_body}"
127
- end
128
- end
129
-
130
- def all_notifs
131
- params = {
132
- app_id: @app_id,
133
- limit: 5,
134
- offset: 0
135
- }
136
-
137
- begin
138
- response = OneSignal::Notification.all(params: params)
139
- puts "code : #{response.code}"
140
- puts "message : #{response.message}"
141
- puts "body : #{response.body}"
142
- puts "id : #{JSON.parse(response.body).class}"
143
- rescue OneSignal::OneSignalError => e
144
- puts "-- message : #{e.message}"
145
- puts "-- status : #{e.http_status}"
146
- puts "-- body : #{e.http_body}"
147
- end
148
- end
149
-
150
- def get_notif(id:)
151
- params = {
152
- app_id: @app_id
153
- }
154
-
155
- begin
156
- response = OneSignal::Notification.get(id: id, params: params)
157
- puts "code : #{response.code}"
158
- puts "message : #{response.message}"
159
- puts "body : #{response.body}"
160
- puts "id : #{JSON.parse(response.body).class}"
161
- rescue OneSignal::OneSignalError => e
162
- puts "-- message : #{e.message}"
163
- puts "-- status : #{e.http_status}"
164
- puts "-- body : #{e.http_body}"
165
- end
166
- end
167
-
168
- def notify
169
- params = {
170
- app_id: @app_id,
171
- contents: {
172
- en: 'hey buddy'
173
- },
174
- ios_badgeType: 'None',
175
- ios_badgeCount: 1,
176
- tags: [
177
- {
178
- key: 'user_id',
179
- relation: '=',
180
- value: "2"
181
- },
182
- { operator: 'AND' },
183
- {
184
- key: 'device_type',
185
- relation: '=',
186
- value: 'ios'
187
- }
188
- ]
189
- }
190
-
191
- begin
192
- response = OneSignal::Notification.create(params: params)
193
- puts "code : #{response.code}"
194
- puts "message : #{response.message}"
195
- puts "body : #{response.body}"
196
- body = JSON.parse(response.body)
197
- return body["id"]
198
- rescue OneSignal::NoRecipientsError => e
199
- puts "--- NoRecipientsError: #{e.inspect}"
200
- puts "-- message : #{e.message}"
201
- puts "-- status : #{e.http_status}"
202
- puts "-- body : #{e.http_body}"
203
- rescue OneSignal::OneSignalError => e
204
- puts "--- OneSignalError : #{e.inspect}"
205
- puts "-- message : #{e.message}"
206
- puts "-- status : #{e.http_status}"
207
- puts "-- body : #{e.http_body}"
208
- end
209
- end
210
-
211
- def csv_export
212
- response = OneSignal::Player.csv_export(app_id: @app_id)
213
- puts response.body
214
- end
215
-
216
- def all_players
217
- response = OneSignal::Player.all(params: {app_id: @app_id, limit: 3})
218
- puts response.body
13
+ # create app
14
+ params = {
15
+ name: 'test-app'
16
+ }
17
+ response = OneSignal::App.create(params: params)
18
+ app_id = JSON.parse(response.body)["id"]
19
+ puts "--- created app id: #{app_id}"
20
+
21
+ # add a player
22
+ device_token = "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabc1"
23
+ params = {
24
+ app_id: app_id,
25
+ device_type: 0,
26
+ identifier: device_token,
27
+ tags: {
28
+ user_id: '123'
29
+ }
30
+ }
31
+ response = OneSignal::Player.create(params: params)
32
+ player_id = JSON.parse(response.body)["id"]
33
+ puts "--- created player id: #{player_id}"
34
+
35
+ # notify the player (this will fail because we haven't configured the app yet)
36
+ params = {
37
+ app_id: app_id,
38
+ contents: {
39
+ en: 'hello player'
40
+ },
41
+ ios_badgeType: 'None',
42
+ ios_badgeCount: 1,
43
+ include_player_ids: [player_id]
44
+ }
45
+ begin
46
+ response = OneSignal::Notification.create(params: params)
47
+ notification_id = JSON.parse(response.body)["id"]
48
+ rescue OneSignal::OneSignalError => e
49
+ puts "--- OneSignalError :"
50
+ puts "-- message : #{e.message}"
51
+ puts "-- status : #{e.http_status}"
52
+ puts "-- body : #{e.http_body}"
219
53
  end
220
-
221
- def get_player(id:)
222
- response = OneSignal::Player.get(id: id)
223
- puts response.body
224
- end
225
-
226
- def update_notif(id:)
227
- params = {
228
- app_id: @app_id,
229
- opened: true
230
- }
231
- response = OneSignal::Notification.update(id: id, params: params)
232
- puts response.body
233
- end
234
-
235
- def delete_notif(id:)
236
- params = {
237
- app_id: @app_id
238
- }
239
-
240
- begin
241
- response = OneSignal::Notification.delete(id: id, params: params)
242
- puts "code : #{response.code}"
243
- puts "message : #{response.message}"
244
- puts "body : #{response.body}"
245
- puts "id : #{JSON.parse(response.body).class}"
246
- rescue OneSignal::OneSignalError => e
247
- puts "-- message : #{e.message}"
248
- puts "-- status : #{e.http_status}"
249
- puts "-- body : #{e.http_body}"
250
- end
251
- end
252
-
253
- def all_apps
254
- begin
255
- response = OneSignal::App.all(params: nil)
256
- puts "code : #{response.code}"
257
- puts "message : #{response.message}"
258
- puts "body : #{response.body}"
259
- puts "id : #{JSON.parse(response.body).class}"
260
- rescue OneSignal::OneSignalError => e
261
- puts "-- message : #{e.message}"
262
- puts "-- status : #{e.http_status}"
263
- puts "-- body : #{e.http_body}"
264
- end
265
- end
266
-
267
- def get_app(id:)
268
- response = OneSignal::App.get(id: id)
269
- puts response.body
270
- end
271
-
272
- def create_app
273
- params = {
274
- name: 'footest'
275
- }
276
- response = OneSignal::App.create(params: params)
277
- puts response.body
278
- end
279
-
280
- def update_app
281
- params = {
282
- name: 'bartest'
283
- }
284
- response = OneSignal::App.update(id: "29203a07-8f1f-438a-9932-9549c6b28c14",
285
- params: params)
286
- puts response.body
287
- end
288
-
289
- # player_id = create_player
290
- # update_player(id: player_id)
291
- player_id = '9c9a3fb4-62e0-455d-865b-670ccea594a1'
292
- notification_id = 'c6e10bac-af7d-4879-a5e9-439de53e3cff'
293
- # create_player_session(id: player_id)
294
- # create_player_purchase(id: player_id)
295
- # create_player_focus(id: player_id)
296
- # notification_id = notify
297
- # puts "--- notif id: #{notification_id}"
298
- # csv_export
299
- # all_players
300
- # get_player(id: player_id)
301
- # all_notifs
302
- # get_notif(id: notification_id)
303
- # delete_notif(id: notification_id)
304
- # update_notif(id: notification_id)
305
- # get_notif(id: notification_id)
306
- # all_apps
307
- # get_app(id: @app_id)
308
- # create_app
309
- update_app
@@ -2,12 +2,14 @@ module OneSignal
2
2
 
3
3
  class App < OneSignal
4
4
 
5
- def self.all(params: {})
5
+ def self.all(params: {}, opts: {})
6
+ opts[:auth_key] ||= @@user_auth_key
7
+
6
8
  uri_string = @@base_uri
7
9
  uri_string += "/apps"
8
10
  uri = URI.parse(uri_string)
9
11
 
10
- response = send_get_request(uri: uri, params: params)
12
+ response = send_get_request(uri: uri, params: params, opts: opts)
11
13
 
12
14
  ensure_http_status(response: response,
13
15
  status: '200',
@@ -18,13 +20,15 @@ module OneSignal
18
20
  return response
19
21
  end
20
22
 
21
- def self.get(id:)
23
+ def self.get(id: "", opts: {})
24
+ opts[:auth_key] ||= @@user_auth_key
25
+
22
26
  uri_string = @@base_uri
23
27
  uri_string += "/apps"
24
28
  uri_string += "/#{id}"
25
29
  uri = URI.parse(uri_string)
26
30
 
27
- response = send_get_request(uri: uri, params: nil)
31
+ response = send_get_request(uri: uri, params: nil, opts: opts)
28
32
 
29
33
  ensure_http_status(response: response,
30
34
  status: '200',
@@ -35,12 +39,14 @@ module OneSignal
35
39
  return response
36
40
  end
37
41
 
38
- def self.create(params: {})
42
+ def self.create(params: {}, opts: {})
43
+ opts[:auth_key] ||= @@user_auth_key
44
+
39
45
  uri_string = @@base_uri
40
46
  uri_string += "/apps"
41
47
  uri = URI.parse(uri_string)
42
48
 
43
- response = send_post_request(uri: uri, body: params)
49
+ response = send_post_request(uri: uri, body: params, opts: opts)
44
50
 
45
51
  ensure_http_status(response: response,
46
52
  status: '200',
@@ -51,13 +57,15 @@ module OneSignal
51
57
  return response
52
58
  end
53
59
 
54
- def self.update(id:, params: {})
60
+ def self.update(id: "", params: {}, opts: {})
61
+ opts[:auth_key] ||= @@user_auth_key
62
+
55
63
  uri_string = @@base_uri
56
64
  uri_string += "/apps"
57
65
  uri_string += "/#{id}"
58
66
  uri = URI.parse(uri_string)
59
67
 
60
- response = send_put_request(uri: uri, body: params)
68
+ response = send_put_request(uri: uri, body: params, opts: opts)
61
69
 
62
70
  ensure_http_status(response: response,
63
71
  status: '200',
@@ -68,12 +76,6 @@ module OneSignal
68
76
  return response
69
77
  end
70
78
 
71
- private
72
-
73
- def self.auth_key
74
- return @@user_auth_key
75
- end
76
-
77
79
  end
78
80
 
79
81
  end