one_signal 0.0.12 → 0.0.13

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 36e9569d2a051f3e9fc4e2c70439c18322c89c21
4
- data.tar.gz: a74e6c5eeb18553a235e03132b5003f03d8da20d
3
+ metadata.gz: bab389232fff84659f6f0343f8f880729fdb51d9
4
+ data.tar.gz: bad83e1ae89015acdc7309a3534deb5120ca8cbd
5
5
  SHA512:
6
- metadata.gz: 2f6e387073ffe3c503f5e8d1fbc520e9f06a875bef9fe1ccf9ba22be608da56af328fa1852eb09f2c67efebeaed72fa3184df1c389d0d3c3f86c1b158a759a07
7
- data.tar.gz: 23cc0acb614002fbee84cf298bd46a9e29e115c4b2cc9817ce9cfad2a73e24398dbfbe8d5bad5fef2c363bc400ae293c9c3121cefe056787f7a9c3ca2031c607
6
+ metadata.gz: 4b7f50a6a54b29f24dd05ba67daa16b5363cbb0c983f9e1155ede871f28a1f6b6ddd0722145585d2c06704c8f9bd9bd1d5120537e87ab470860f324fab1f9a09
7
+ data.tar.gz: 700aabec5ffe831e523389da25b4b6d93f292ed44e7eb4ae4c71b4e4edfe64863c9ea6f78218b850a4507f9db9f6392d50f57e527354e0f75f8a0216b18bea6b
data/CHANGELOG.txt CHANGED
@@ -1,3 +1,10 @@
1
+ === 0.0.13 2016-02-18
2
+
3
+ * Add App#all method
4
+ * Add App#get method
5
+ * Add App#create method
6
+ * Add App#update method
7
+
1
8
  === 0.0.12 2016-02-16
2
9
 
3
10
  * Add Notification#update method
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
- # onesignal-ruby
2
- OneSignal Ruby bindings
1
+ # OneSignal Ruby bindings
2
+
3
+ This gem provides a simple SDK to access the [OneSignal API](https://documentation.onesignal.com/docs/server-api-overview).
3
4
 
4
5
  ## Installation
5
6
 
@@ -7,10 +8,63 @@ OneSignal Ruby bindings
7
8
  gem install one_signal
8
9
  ```
9
10
 
11
+ ## Development
12
+
13
+ Run the tests
14
+
15
+ ```
16
+ bundle exec rake
17
+ ```
18
+
10
19
  ## Basic usage
11
20
 
12
21
  See basic examples in [example.rb](/example.rb)
13
22
 
23
+ ## Documentation
24
+
25
+ Specify your API key:
26
+
27
+ ```ruby
28
+ OneSignal::OneSignal.api_key = YOUR_API_KEY
29
+ ```
30
+
31
+ Then call the following methods on the `App`, `Player` and `Notification` classes.
32
+ 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)
33
+
34
+ The return value of each method is a `Net::HTTPResponse`.
35
+
36
+ ### Apps
37
+
38
+ ```ruby
39
+ - OneSignal::App.all(params:)
40
+ - OneSignal::App.get(id:)
41
+ - OneSignal::App.create(params:)
42
+ - OneSignal::App.update(id:, params:)
43
+ ```
44
+
45
+ ### Players
46
+
47
+ ```ruby
48
+ - OneSignal::Player.all(params:)
49
+ - OneSignal::Player.csv_export(app_id:)
50
+ - OneSignal::Player.get(id:)
51
+ - OneSignal::Player.create(params:)
52
+ - OneSignal::Player.create_session(id:, params:)
53
+ - OneSignal::Player.create_purchase(id:, params:)
54
+ - OneSignal::Player.create_focus(id:, params:)
55
+ - OneSignal::Player.update(id:, params:)
56
+ ```
57
+
58
+ ### Notifications
59
+
60
+ ```ruby
61
+ - OneSignal::Notification.all(params:)
62
+ - OneSignal::Notification.get(id:, params:)
63
+ - OneSignal::Notification.create(params:)
64
+ - OneSignal::Notification.update(id:, params:)
65
+ - OneSignal::Notification.delete(id:, params:)
66
+ ```
67
+
14
68
  ## Changes
15
69
 
16
70
  See [CHANGELOG.txt](CHANGELOG.txt)
data/example.rb CHANGED
@@ -3,11 +3,13 @@ require 'dotenv'
3
3
 
4
4
  Dotenv.load
5
5
  api_key = ENV['ONESIGNAL_API_KEY']
6
+ user_auth_key = ENV['ONESIGNAL_USER_AUTH_KEY']
6
7
  @app_id = ENV['ONESIGNAL_APP_ID']
7
8
  @device_token = "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabc3"
8
9
  @example_player_id = ENV['EXAMPLE_PLAYER_ID']
9
10
 
10
11
  OneSignal::OneSignal.api_key = api_key
12
+ OneSignal::OneSignal.user_auth_key = user_auth_key
11
13
 
12
14
  def create_player
13
15
  params = {
@@ -175,7 +177,7 @@ def notify
175
177
  {
176
178
  key: 'user_id',
177
179
  relation: '=',
178
- value: "unknown"
180
+ value: "2"
179
181
  },
180
182
  { operator: 'AND' },
181
183
  {
@@ -191,6 +193,8 @@ def notify
191
193
  puts "code : #{response.code}"
192
194
  puts "message : #{response.message}"
193
195
  puts "body : #{response.body}"
196
+ body = JSON.parse(response.body)
197
+ return body["id"]
194
198
  rescue OneSignal::NoRecipientsError => e
195
199
  puts "--- NoRecipientsError: #{e.inspect}"
196
200
  puts "-- message : #{e.message}"
@@ -228,6 +232,60 @@ def update_notif(id:)
228
232
  puts response.body
229
233
  end
230
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
+
231
289
  # player_id = create_player
232
290
  # update_player(id: player_id)
233
291
  player_id = '9c9a3fb4-62e0-455d-865b-670ccea594a1'
@@ -235,11 +293,17 @@ notification_id = 'c6e10bac-af7d-4879-a5e9-439de53e3cff'
235
293
  # create_player_session(id: player_id)
236
294
  # create_player_purchase(id: player_id)
237
295
  # create_player_focus(id: player_id)
238
- # notify
296
+ # notification_id = notify
297
+ # puts "--- notif id: #{notification_id}"
239
298
  # csv_export
240
299
  # all_players
241
300
  # get_player(id: player_id)
242
301
  # all_notifs
243
- get_notif(id: notification_id)
244
- update_notif(id: notification_id)
245
- get_notif(id: notification_id)
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
@@ -0,0 +1,79 @@
1
+ module OneSignal
2
+
3
+ class App < OneSignal
4
+
5
+ def self.all(params: {})
6
+ uri_string = @@base_uri
7
+ uri_string += "/apps"
8
+ uri = URI.parse(uri_string)
9
+
10
+ response = send_get_request(uri: uri, params: params)
11
+
12
+ ensure_http_status(response: response,
13
+ status: '200',
14
+ method_name: 'All',
15
+ uri: uri,
16
+ params: params)
17
+
18
+ return response
19
+ end
20
+
21
+ def self.get(id:)
22
+ uri_string = @@base_uri
23
+ uri_string += "/apps"
24
+ uri_string += "/#{id}"
25
+ uri = URI.parse(uri_string)
26
+
27
+ response = send_get_request(uri: uri, params: nil)
28
+
29
+ ensure_http_status(response: response,
30
+ status: '200',
31
+ method_name: 'Get',
32
+ uri: uri,
33
+ params: nil)
34
+
35
+ return response
36
+ end
37
+
38
+ def self.create(params: {})
39
+ uri_string = @@base_uri
40
+ uri_string += "/apps"
41
+ uri = URI.parse(uri_string)
42
+
43
+ response = send_post_request(uri: uri, body: params)
44
+
45
+ ensure_http_status(response: response,
46
+ status: '200',
47
+ method_name: 'Create',
48
+ uri: uri,
49
+ params: params)
50
+
51
+ return response
52
+ end
53
+
54
+ def self.update(id:, params: {})
55
+ uri_string = @@base_uri
56
+ uri_string += "/apps"
57
+ uri_string += "/#{id}"
58
+ uri = URI.parse(uri_string)
59
+
60
+ response = send_put_request(uri: uri, body: params)
61
+
62
+ ensure_http_status(response: response,
63
+ status: '200',
64
+ method_name: 'Update',
65
+ uri: uri,
66
+ params: params)
67
+
68
+ return response
69
+ end
70
+
71
+ private
72
+
73
+ def self.auth_key
74
+ return @@user_auth_key
75
+ end
76
+
77
+ end
78
+
79
+ end
@@ -66,6 +66,23 @@ module OneSignal
66
66
  return response
67
67
  end
68
68
 
69
+ def self.delete(id:, params:)
70
+ uri_string = @@base_uri
71
+ uri_string += "/notifications"
72
+ uri_string += "/#{id}"
73
+ uri = URI.parse(uri_string)
74
+
75
+ response = send_delete_request(uri: uri, params: params)
76
+
77
+ ensure_http_status(response: response,
78
+ status: '200',
79
+ method_name: 'Delete',
80
+ uri: uri,
81
+ params: nil)
82
+
83
+ return response
84
+ end
85
+
69
86
  private
70
87
 
71
88
  def self.handle_error(uri:, params:, response:)
@@ -7,6 +7,7 @@ module OneSignal
7
7
  class OneSignal
8
8
  @@base_uri = "https://onesignal.com/api/v1"
9
9
  @@api_key = nil
10
+ @@user_auth_key = nil
10
11
  @@open_timeout = 30
11
12
  @@read_timeout = 30
12
13
 
@@ -18,6 +19,14 @@ module OneSignal
18
19
  @@api_key
19
20
  end
20
21
 
22
+ def self.user_auth_key=(new_user_auth_key)
23
+ @@user_auth_key = new_user_auth_key
24
+ end
25
+
26
+ def self.user_auth_key
27
+ @@user_auth_key
28
+ end
29
+
21
30
  def self.open_timeout=(new_timeout)
22
31
  @@open_timeout = new_timeout
23
32
  end
@@ -43,8 +52,6 @@ module OneSignal
43
52
  end
44
53
 
45
54
  def self.send_post_request(uri:, body:)
46
- ensure_api_key
47
-
48
55
  request = Net::HTTP::Post.new(uri.request_uri)
49
56
  request.body = body.to_json
50
57
  request = request_with_headers(request: request)
@@ -54,11 +61,9 @@ module OneSignal
54
61
  response = http.request(request)
55
62
  end
56
63
 
57
- def self.send_delete_request(uri:, body:)
58
- ensure_api_key
59
-
64
+ def self.send_delete_request(uri:, params: {})
65
+ uri.query = URI.encode_www_form(params) unless params.nil?
60
66
  request = Net::HTTP::Delete.new(uri.request_uri)
61
- request.body = body.to_json
62
67
  request = request_with_headers(request: request)
63
68
 
64
69
  http = http_object(uri: uri)
@@ -67,8 +72,6 @@ module OneSignal
67
72
  end
68
73
 
69
74
  def self.send_put_request(uri:, body:)
70
- ensure_api_key
71
-
72
75
  request = Net::HTTP::Put.new(uri.request_uri)
73
76
  request.body = body.to_json
74
77
  request = request_with_headers(request: request)
@@ -79,8 +82,6 @@ module OneSignal
79
82
  end
80
83
 
81
84
  def self.send_get_request(uri:, params: {})
82
- ensure_api_key
83
-
84
85
  uri.query = URI.encode_www_form(params) unless params.nil?
85
86
  request = Net::HTTP::Get.new(uri.request_uri)
86
87
  request = request_with_headers(request: request)
@@ -94,26 +95,23 @@ module OneSignal
94
95
 
95
96
  def self.ensure_http_status(response:, status:, method_name:, uri:, params:)
96
97
  if response.code != status.to_s
97
- msg = "#{method_name} error - uri: #{uri} params: #{params}"
98
+ msg = "#{self.name}##{method_name} error - uri: #{uri} params: #{params}"
98
99
  raise OneSignalError.new(message: msg,
99
100
  http_status: response.code,
100
101
  http_body: response.body)
101
102
  end
102
103
  end
103
104
 
104
- def self.ensure_api_key
105
- unless @@api_key && !@@api_key.strip.empty?
106
- msg = "No API key provided"
107
- raise OneSignalError.new(message: msg)
108
- end
109
- end
110
-
111
105
  def self.request_with_headers(request:)
112
106
  request.add_field("Content-Type", "application/json")
113
- request.add_field("Authorization", "Basic #{@@api_key}")
107
+ request.add_field("Authorization", "Basic #{self.auth_key}")
114
108
  return request
115
109
  end
116
110
 
111
+ def self.auth_key
112
+ return @@api_key
113
+ end
114
+
117
115
  end
118
116
 
119
117
  end
@@ -1,3 +1,3 @@
1
1
  module OneSignal
2
- VERSION = "0.0.12"
2
+ VERSION = "0.0.13"
3
3
  end
data/lib/one_signal.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'one_signal/version'
2
2
  require 'one_signal/one_signal'
3
+ require 'one_signal/app'
3
4
  require 'one_signal/player'
4
5
  require 'one_signal/notification'
5
6
 
data/test/app_test.rb ADDED
@@ -0,0 +1,54 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class AppTest < MiniTest::Test
4
+
5
+ def setup
6
+ base_url = "https://onesignal.com/api/v1/apps"
7
+ @app_id = "fake-id-123"
8
+ @all_uri = URI.parse(base_url)
9
+ @get_uri = URI.parse(base_url + "/#{@app_id}")
10
+ @create_uri = URI.parse(base_url)
11
+ @update_uri = URI.parse(base_url + "/#{@app_id}")
12
+
13
+ @params = {
14
+ foo: "bar",
15
+ widget: "acme"
16
+ }
17
+
18
+ @user_auth_key = "fake user auth key"
19
+ OneSignal::OneSignal.user_auth_key = @user_auth_key
20
+ end
21
+
22
+ def test_all
23
+ response = mock_response_ok
24
+ OneSignal::OneSignal.expects(:send_get_request)
25
+ .with(uri: @all_uri, params: @params)
26
+ .returns(response)
27
+ assert_equal response, OneSignal::App.all(params: @params)
28
+ end
29
+
30
+ def test_get
31
+ response = mock_response_ok
32
+ OneSignal::OneSignal.expects(:send_get_request)
33
+ .with(uri: @get_uri, params: nil)
34
+ .returns(response)
35
+ assert_equal response, OneSignal::App.get(id: @app_id)
36
+ end
37
+
38
+ def test_create
39
+ response = mock_response_ok
40
+ OneSignal::OneSignal.expects(:send_post_request)
41
+ .with(uri: @create_uri, body: @params)
42
+ .returns(response)
43
+ assert_equal response, OneSignal::App.create(params: @params)
44
+ end
45
+
46
+ def test_update
47
+ response = mock_response_ok
48
+ OneSignal::OneSignal.expects(:send_put_request)
49
+ .with(uri: @update_uri, body: @params)
50
+ .returns(response)
51
+ assert_equal response, OneSignal::App.update(id: @app_id, params: @params)
52
+ end
53
+
54
+ end
@@ -7,6 +7,7 @@ class NotificationTest < MiniTest::Test
7
7
  base_url = "https://onesignal.com/api/v1/notifications"
8
8
  @create_uri = URI.parse(base_url)
9
9
  @update_uri = URI.parse(base_url + "/#{@notification_id}")
10
+ @delete_uri = URI.parse(base_url + "/#{@notification_id}")
10
11
  @all_uri = URI.parse(base_url)
11
12
  @get_uri = URI.parse(base_url + "/#{@notification_id}")
12
13
  @params = {
@@ -71,4 +72,13 @@ class NotificationTest < MiniTest::Test
71
72
  params: @params)
72
73
  end
73
74
 
75
+ def test_delete
76
+ response = mock_response_ok
77
+ OneSignal::OneSignal.expects(:send_delete_request)
78
+ .with(uri: @delete_uri, params: @params)
79
+ .returns(response)
80
+ assert_equal response, OneSignal::Notification.delete(id: @notification_id,
81
+ params: @params)
82
+ end
83
+
74
84
  end
@@ -33,28 +33,6 @@ class OneSignalTest < MiniTest::Test
33
33
  return http
34
34
  end
35
35
 
36
- def test_building_request_with_nil_api_key_raises_error
37
- OneSignal::OneSignal.api_key = nil
38
-
39
- assert_raises OneSignal::OneSignalError do
40
- OneSignal::OneSignal.send_post_request(uri: @uri, body: @body)
41
- end
42
- assert_raises OneSignal::OneSignalError do
43
- OneSignal::OneSignal.send_put_request(uri: @uri, body: @body)
44
- end
45
- end
46
-
47
- def test_building_request_with_empty_api_key_raises_error
48
- OneSignal::OneSignal.api_key = " "
49
-
50
- assert_raises OneSignal::OneSignalError do
51
- OneSignal::OneSignal.send_post_request(uri: @uri, body: @body)
52
- end
53
- assert_raises OneSignal::OneSignalError do
54
- OneSignal::OneSignal.send_put_request(uri: @uri, body: @body)
55
- end
56
- end
57
-
58
36
  def test_default_timeout
59
37
  assert_equal @default_timeout, OneSignal::OneSignal.open_timeout
60
38
  assert_equal @default_timeout, OneSignal::OneSignal.read_timeout
@@ -105,20 +83,24 @@ class OneSignalTest < MiniTest::Test
105
83
  end
106
84
 
107
85
  def test_send_delete_request
86
+ expected_uri = @uri.clone
87
+ expected_uri.query = URI.encode_www_form(@params)
88
+
108
89
  # test request creation
109
- request = build_mock_request(body: @body)
110
- Net::HTTP::Delete.expects(:new).with(@uri.request_uri).returns(request)
90
+ request = build_mock_request
91
+ Net::HTTP::Delete.expects(:new).with(expected_uri.request_uri).returns(request)
111
92
 
112
93
  # test http object creation
113
94
  http = build_mock_http_object
114
- Net::HTTP.expects(:new).with(@uri.host, @uri.port).returns(http)
95
+ Net::HTTP.expects(:new).with(expected_uri.host, expected_uri.port).returns(http)
115
96
 
116
97
  # test send request
117
98
  response = mock()
118
99
  http.expects(:request).with(request).returns(response)
119
100
 
120
101
  OneSignal::OneSignal.api_key = @api_key
121
- assert_equal response, OneSignal::OneSignal.send_delete_request(uri: @uri, body: @body)
102
+ assert_equal response, OneSignal::OneSignal.send_delete_request(uri: @uri,
103
+ params: @params)
122
104
  end
123
105
 
124
106
  def test_send_put_request
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: one_signal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: 0.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Balthazar
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-16 00:00:00.000000000 Z
11
+ date: 2016-02-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -94,6 +94,7 @@ files:
94
94
  - Rakefile
95
95
  - example.rb
96
96
  - lib/one_signal.rb
97
+ - lib/one_signal/app.rb
97
98
  - lib/one_signal/errors/no_recipients_error.rb
98
99
  - lib/one_signal/errors/one_signal_error.rb
99
100
  - lib/one_signal/notification.rb
@@ -101,6 +102,7 @@ files:
101
102
  - lib/one_signal/player.rb
102
103
  - lib/one_signal/version.rb
103
104
  - one_signal.gemspec
105
+ - test/app_test.rb
104
106
  - test/helper.rb
105
107
  - test/notification_test.rb
106
108
  - test/one_signal_test.rb