Skip to content

Commit 36b16cb

Browse files
author
coding-chimp
committed
Adds notification resources
1 parent 0cba377 commit 36b16cb

15 files changed

Lines changed: 290 additions & 1 deletion

lib/onesignal.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
require 'onesignal/models/base_model'
77
require 'onesignal/models/app'
88
require 'onesignal/models/player'
9+
require 'onesignal/models/notification'
910
require 'onesignal/resources/base_resource'
1011
require 'onesignal/resources/app_resource'
1112
require 'onesignal/resources/player_resource'
13+
require 'onesignal/resources/notification_resource'
1214

1315
module OneSignal
1416
DEVICE_TYPES = %w(

lib/onesignal/client.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ def players
1919
@players ||= PlayerResource.new(self)
2020
end
2121

22+
def notifications
23+
ensure_app_id_presence
24+
25+
@notifications ||= NotificationResource.new(self)
26+
end
27+
2228
private
2329

2430
def ensure_app_id_presence
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module OneSignal
2+
class Notification < BaseModel
3+
attr_accessor :canceled, :contents, :converted, :data, :failed, :headings,
4+
:id, :queued_at, :remaining, :send_after, :successful, :url
5+
6+
def queued_at=(time)
7+
@queued_at = Time.at(time)
8+
end
9+
10+
def send_after=(time)
11+
@send_after = Time.at(time)
12+
end
13+
end
14+
end

lib/onesignal/request.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ def put(path, params)
2828
request(uri, Net::HTTP::Put, params)
2929
end
3030

31+
def delete(path, params = {})
32+
uri = uri(path)
33+
34+
if @client.app_id
35+
params[:app_id] = @client.app_id
36+
end
37+
38+
uri.query = URI.encode_www_form(params)
39+
40+
request(uri, Net::HTTP::Delete, params)
41+
end
42+
3143
private
3244

3345
def uri(path)

lib/onesignal/resources/base_resource.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ def post(path, params)
2020
@response = request.post(path, params)
2121
end
2222

23+
def delete(path)
24+
@response = request.delete(path)
25+
end
26+
2327
def request
2428
@request ||= OneSignal::Request.new(@client)
2529
end
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module OneSignal
2+
class NotificationResource < BaseResource
3+
def all(params = {})
4+
get('/notifications', params)
5+
6+
response_body['notifications'].map(&OneSignal::Notification)
7+
end
8+
9+
def find(id)
10+
get("/notifications/#{id}")
11+
12+
OneSignal::Notification.new(response_body)
13+
end
14+
15+
def track_open(id, params)
16+
put("/notifications/#{id}", params)
17+
18+
true
19+
end
20+
21+
def create(params)
22+
post('/notifications', params)
23+
24+
response_body
25+
end
26+
27+
def cancel(id)
28+
delete("/notifications/#{id}")
29+
30+
true
31+
end
32+
end
33+
end

test/client_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,16 @@ def test_players_throws_error_when_app_id_missing
1818

1919
assert_raises(OneSignal::AppIdMissingError) { client.players }
2020
end
21+
22+
def test_notifications_returns_a_notification_resource
23+
client = OneSignal::Client.new(auth_token: 'test', app_id: '1')
24+
25+
assert_instance_of OneSignal::NotificationResource, client.notifications
26+
end
27+
28+
def test_notifications_throws_error_when_app_id_missing
29+
client = OneSignal::Client.new(auth_token: 'test')
30+
31+
assert_raises(OneSignal::AppIdMissingError) { client.notifications }
32+
end
2133
end
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"total_count":3,
3+
"offset":2,
4+
"limit":2,
5+
"notifications":
6+
[
7+
{
8+
"id":"481a2734-6b7d-11e4-a6ea-4b53294fa671",
9+
"successful":15,
10+
"failed":1,
11+
"converted":3,
12+
"remaining":0,
13+
"queued_at":1415914655,
14+
"send_after":1415914655,
15+
"canceled": false,
16+
"url": "https://yourWebsiteToOpen.com",
17+
"data": {
18+
"abc": "123",
19+
"foo": "bar"
20+
},
21+
"headings":{
22+
"en":"English and default langauge heading",
23+
"es":"Spanish language heading"
24+
},
25+
"contents":{
26+
"en":"English and default content",
27+
"es":"Hola"
28+
}
29+
},
30+
{
31+
"id":"b6b326a8-40aa-13e5-b91b-bf8bc3fa26f7",
32+
"successful":5,
33+
"failed":2,
34+
"converted":0,
35+
"remaining":0,
36+
"queued_at":1415915123,
37+
"send_after":1415915123,
38+
"canceled": false,
39+
"url": "http://foo.com",
40+
"data":{
41+
"foo":"bar",
42+
"your":"custom metadata"
43+
},
44+
"headings":{
45+
"en":"English and default langauge heading",
46+
"es":"Spanish language heading"
47+
},
48+
"contents":{
49+
"en":"English and default content",
50+
"es":"Hola"
51+
}
52+
}
53+
]
54+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{'success': "true"}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"id": "458dcec4-cf53-11e3-add2-000c2940e62c",
3+
"recipients": 5
4+
}

0 commit comments

Comments
 (0)