onesignal-ruby-rails6 0.3.0.1

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.
Files changed (50) hide show
  1. checksums.yaml +7 -0
  2. data/.editorconfig +14 -0
  3. data/.github/workflows/gempush.yml +41 -0
  4. data/.github/workflows/ruby.yml +20 -0
  5. data/.gitignore +17 -0
  6. data/.rspec +3 -0
  7. data/.rubocop.yml +182 -0
  8. data/CODE_OF_CONDUCT.md +74 -0
  9. data/Gemfile +12 -0
  10. data/Gemfile.lock +85 -0
  11. data/LICENSE.txt +21 -0
  12. data/README.md +211 -0
  13. data/Rakefile +8 -0
  14. data/bin/console +15 -0
  15. data/bin/setup +8 -0
  16. data/fixtures/vcr_cassettes/os-csv-export.yml +135 -0
  17. data/fixtures/vcr_cassettes/os-fetch-noti.yml +76 -0
  18. data/fixtures/vcr_cassettes/os-fetch-player.yml +72 -0
  19. data/fixtures/vcr_cassettes/os-fetch-players.yml +72 -0
  20. data/fixtures/vcr_cassettes/os-send-noti.yml +145 -0
  21. data/lib/onesignal.rb +60 -0
  22. data/lib/onesignal/attachments.rb +27 -0
  23. data/lib/onesignal/auto_map.rb +13 -0
  24. data/lib/onesignal/autoloader.rb +7 -0
  25. data/lib/onesignal/client.rb +81 -0
  26. data/lib/onesignal/commands.rb +7 -0
  27. data/lib/onesignal/commands/autoloader.rb +7 -0
  28. data/lib/onesignal/commands/base_command.rb +23 -0
  29. data/lib/onesignal/commands/create_notification.rb +15 -0
  30. data/lib/onesignal/commands/csv_export.rb +15 -0
  31. data/lib/onesignal/commands/fetch_notification.rb +15 -0
  32. data/lib/onesignal/commands/fetch_player.rb +15 -0
  33. data/lib/onesignal/commands/fetch_players.rb +11 -0
  34. data/lib/onesignal/configuration.rb +19 -0
  35. data/lib/onesignal/filter.rb +140 -0
  36. data/lib/onesignal/included_targets.rb +47 -0
  37. data/lib/onesignal/notification.rb +37 -0
  38. data/lib/onesignal/notification/contents.rb +17 -0
  39. data/lib/onesignal/notification/headings.rb +17 -0
  40. data/lib/onesignal/responses.rb +7 -0
  41. data/lib/onesignal/responses/autoloader.rb +7 -0
  42. data/lib/onesignal/responses/base_response.rb +18 -0
  43. data/lib/onesignal/responses/csv_export.rb +18 -0
  44. data/lib/onesignal/responses/notification.rb +45 -0
  45. data/lib/onesignal/responses/player.rb +21 -0
  46. data/lib/onesignal/segment.rb +22 -0
  47. data/lib/onesignal/sounds.rb +39 -0
  48. data/lib/onesignal/version.rb +6 -0
  49. data/onesignal-ruby.gemspec +48 -0
  50. metadata +270 -0
@@ -0,0 +1,72 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://onesignal.com/api/v1/players/?app_id
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ User-Agent:
11
+ - Faraday v0.15.2
12
+ Content-Type:
13
+ - application/json
14
+ Authorization:
15
+ - Basic test
16
+ Accept-Encoding:
17
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
18
+ Accept:
19
+ - "*/*"
20
+ response:
21
+ status:
22
+ code: 200
23
+ message: OK
24
+ headers:
25
+ Date:
26
+ - Thu, 28 Jun 2018 15:30:58 GMT
27
+ Content-Type:
28
+ - application/json; charset=utf-8
29
+ Transfer-Encoding:
30
+ - chunked
31
+ Connection:
32
+ - keep-alive
33
+ Set-Cookie:
34
+ - __cfduid=d162ae07cbc70fafa075f6d10a6694cc61530199858; expires=Fri, 28-Jun-19
35
+ 15:30:58 GMT; path=/; domain=.onesignal.com; HttpOnly
36
+ Status:
37
+ - 200 OK
38
+ Cache-Control:
39
+ - max-age=0, private, must-revalidate
40
+ Access-Control-Allow-Origin:
41
+ - "*"
42
+ X-Xss-Protection:
43
+ - 1; mode=block
44
+ X-Request-Id:
45
+ - 768dd4da-9f24-4a93-8a1b-3efbc3bf22b2
46
+ Access-Control-Allow-Headers:
47
+ - SDK-Version
48
+ Etag:
49
+ - W/"cee038b261c95e403233f1ada96658e3"
50
+ X-Frame-Options:
51
+ - SAMEORIGIN
52
+ X-Runtime:
53
+ - '0.029141'
54
+ X-Content-Type-Options:
55
+ - nosniff
56
+ X-Powered-By:
57
+ - Phusion Passenger 5.3.2
58
+ Expect-Ct:
59
+ - max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
60
+ Server:
61
+ - cloudflare
62
+ Cf-Ray:
63
+ - 4321329ccab1430a-MXP
64
+ body:
65
+ encoding: ASCII-8BIT
66
+ string: '{"total_count":8,"offset":0,"limit":300,"players":[{"id":"14c8141f-d57d-4652-8911-960a00ff14fa","identifier":"52812f0ca9b3cc1c1f6a13fd1467c3a2a9f7ef3db5ee56cb951b00fb6bb78665","session_count":10,"language":"it","timezone":7200,"game_version":"1","device_os":"11.4","device_type":0,"device_model":"iPhone10,5","ad_id":"7A30EDB9-A4EA-404E-BA5E-2AEA86C74AA7","tags":{},"last_active":1530184068,"playtime":1219,"amount_spent":0.0,"created_at":1530177751,"invalid_identifier":false,"badge_count":0,"sdk":"020805","test_type":1,"ip":null},{"id":"16c0eee0-edc0-4630-8792-1c432a2e7da1","identifier":null,"session_count":2,"language":"en","timezone":7200,"game_version":"1","device_os":"7.1.2","device_type":1,"device_model":"MotoG3","ad_id":null,"tags":{},"last_active":1530192672,"playtime":0,"amount_spent":0.0,"created_at":1530192553,"invalid_identifier":true,"badge_count":0,"sdk":"030901","test_type":null,"ip":null},{"id":"066e3db6-1ed1-49f0-9340-624327e3faf8","identifier":null,"session_count":1,"language":"en","timezone":7200,"game_version":"1","device_os":"7.1.2","device_type":1,"device_model":"MotoG3","ad_id":null,"tags":{},"last_active":1530193737,"playtime":0,"amount_spent":0.0,"created_at":1530193737,"invalid_identifier":true,"badge_count":0,"sdk":"030901","test_type":null,"ip":null},{"id":"cf435718-f137-4fd8-ba43-b9ada7e88846","identifier":null,"session_count":1,"language":"en","timezone":7200,"game_version":"1","device_os":"7.1.2","device_type":1,"device_model":"MotoG3","ad_id":null,"tags":{},"last_active":1530194294,"playtime":0,"amount_spent":0.0,"created_at":1530194294,"invalid_identifier":true,"badge_count":0,"sdk":"030901","test_type":null,"ip":null},{"id":"ba4b2ab3-cc89-4bd6-8c1a-9f69e7a2aa55","identifier":null,"session_count":1,"language":"en","timezone":7200,"game_version":"1","device_os":"7.1.2","device_type":1,"device_model":"MotoG3","ad_id":null,"tags":{},"last_active":1530197017,"playtime":0,"amount_spent":0.0,"created_at":1530197017,"invalid_identifier":true,"badge_count":0,"sdk":"030901","test_type":null,"ip":null},{"id":"152c5092-beb7-4b54-a0c0-168460ff41c0","identifier":null,"session_count":1,"language":"en","timezone":0,"game_version":"1","device_os":"8.1.0","device_type":1,"device_model":"Android
67
+ SDK built for x86","ad_id":null,"tags":{},"last_active":1530198026,"playtime":0,"amount_spent":0.0,"created_at":1530198026,"invalid_identifier":true,"badge_count":0,"sdk":"030901","test_type":null,"ip":null},{"id":"16421e68-fa8f-4072-82db-2d2d1765c317","identifier":null,"session_count":1,"language":"en","timezone":0,"game_version":"1","device_os":"8.1.0","device_type":1,"device_model":"Android
68
+ SDK built for x86","ad_id":null,"tags":{},"last_active":1530198244,"playtime":0,"amount_spent":0.0,"created_at":1530198244,"invalid_identifier":true,"badge_count":0,"sdk":"030901","test_type":null,"ip":null},{"id":"e818b09c-3eca-41be-b0ee-8379cab19d93","identifier":null,"session_count":1,"language":"en","timezone":0,"game_version":"1","device_os":"8.1.0","device_type":1,"device_model":"Android
69
+ SDK built for x86","ad_id":null,"tags":{},"last_active":1530199447,"playtime":0,"amount_spent":0.0,"created_at":1530199447,"invalid_identifier":true,"badge_count":0,"sdk":"030901","test_type":null,"ip":null}]}'
70
+ http_version:
71
+ recorded_at: Thu, 28 Jun 2018 15:30:58 GMT
72
+ recorded_with: VCR 4.0.0
@@ -0,0 +1,72 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://onesignal.com/api/v1/players?app_id
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ User-Agent:
11
+ - Faraday v0.15.2
12
+ Content-Type:
13
+ - application/json
14
+ Authorization:
15
+ - Basic test
16
+ Accept-Encoding:
17
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
18
+ Accept:
19
+ - "*/*"
20
+ response:
21
+ status:
22
+ code: 200
23
+ message: OK
24
+ headers:
25
+ Date:
26
+ - Thu, 28 Jun 2018 15:30:58 GMT
27
+ Content-Type:
28
+ - application/json; charset=utf-8
29
+ Transfer-Encoding:
30
+ - chunked
31
+ Connection:
32
+ - keep-alive
33
+ Set-Cookie:
34
+ - __cfduid=db617b0b45cf8c5a16bdbc61edef5a1a81530199858; expires=Fri, 28-Jun-19
35
+ 15:30:58 GMT; path=/; domain=.onesignal.com; HttpOnly
36
+ Status:
37
+ - 200 OK
38
+ Cache-Control:
39
+ - max-age=0, private, must-revalidate
40
+ Access-Control-Allow-Origin:
41
+ - "*"
42
+ X-Xss-Protection:
43
+ - 1; mode=block
44
+ X-Request-Id:
45
+ - 1dbdac59-c104-4c1d-9e92-428badf0dacf
46
+ Access-Control-Allow-Headers:
47
+ - SDK-Version
48
+ Etag:
49
+ - W/"cee038b261c95e403233f1ada96658e3"
50
+ X-Frame-Options:
51
+ - SAMEORIGIN
52
+ X-Runtime:
53
+ - '0.051957'
54
+ X-Content-Type-Options:
55
+ - nosniff
56
+ X-Powered-By:
57
+ - Phusion Passenger 5.3.2
58
+ Expect-Ct:
59
+ - max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
60
+ Server:
61
+ - cloudflare
62
+ Cf-Ray:
63
+ - 4321329b4893434c-MXP
64
+ body:
65
+ encoding: ASCII-8BIT
66
+ string: '{"total_count":8,"offset":0,"limit":300,"players":[{"id":"14c8141f-d57d-4652-8911-960a00ff14fa","identifier":"52812f0ca9b3cc1c1f6a13fd1467c3a2a9f7ef3db5ee56cb951b00fb6bb78665","session_count":10,"language":"it","timezone":7200,"game_version":"1","device_os":"11.4","device_type":0,"device_model":"iPhone10,5","ad_id":"7A30EDB9-A4EA-404E-BA5E-2AEA86C74AA7","tags":{},"last_active":1530184068,"playtime":1219,"amount_spent":0.0,"created_at":1530177751,"invalid_identifier":false,"badge_count":0,"sdk":"020805","test_type":1,"ip":null},{"id":"16c0eee0-edc0-4630-8792-1c432a2e7da1","identifier":null,"session_count":2,"language":"en","timezone":7200,"game_version":"1","device_os":"7.1.2","device_type":1,"device_model":"MotoG3","ad_id":null,"tags":{},"last_active":1530192672,"playtime":0,"amount_spent":0.0,"created_at":1530192553,"invalid_identifier":true,"badge_count":0,"sdk":"030901","test_type":null,"ip":null},{"id":"066e3db6-1ed1-49f0-9340-624327e3faf8","identifier":null,"session_count":1,"language":"en","timezone":7200,"game_version":"1","device_os":"7.1.2","device_type":1,"device_model":"MotoG3","ad_id":null,"tags":{},"last_active":1530193737,"playtime":0,"amount_spent":0.0,"created_at":1530193737,"invalid_identifier":true,"badge_count":0,"sdk":"030901","test_type":null,"ip":null},{"id":"cf435718-f137-4fd8-ba43-b9ada7e88846","identifier":null,"session_count":1,"language":"en","timezone":7200,"game_version":"1","device_os":"7.1.2","device_type":1,"device_model":"MotoG3","ad_id":null,"tags":{},"last_active":1530194294,"playtime":0,"amount_spent":0.0,"created_at":1530194294,"invalid_identifier":true,"badge_count":0,"sdk":"030901","test_type":null,"ip":null},{"id":"ba4b2ab3-cc89-4bd6-8c1a-9f69e7a2aa55","identifier":null,"session_count":1,"language":"en","timezone":7200,"game_version":"1","device_os":"7.1.2","device_type":1,"device_model":"MotoG3","ad_id":null,"tags":{},"last_active":1530197017,"playtime":0,"amount_spent":0.0,"created_at":1530197017,"invalid_identifier":true,"badge_count":0,"sdk":"030901","test_type":null,"ip":null},{"id":"152c5092-beb7-4b54-a0c0-168460ff41c0","identifier":null,"session_count":1,"language":"en","timezone":0,"game_version":"1","device_os":"8.1.0","device_type":1,"device_model":"Android
67
+ SDK built for x86","ad_id":null,"tags":{},"last_active":1530198026,"playtime":0,"amount_spent":0.0,"created_at":1530198026,"invalid_identifier":true,"badge_count":0,"sdk":"030901","test_type":null,"ip":null},{"id":"16421e68-fa8f-4072-82db-2d2d1765c317","identifier":null,"session_count":1,"language":"en","timezone":0,"game_version":"1","device_os":"8.1.0","device_type":1,"device_model":"Android
68
+ SDK built for x86","ad_id":null,"tags":{},"last_active":1530198244,"playtime":0,"amount_spent":0.0,"created_at":1530198244,"invalid_identifier":true,"badge_count":0,"sdk":"030901","test_type":null,"ip":null},{"id":"e818b09c-3eca-41be-b0ee-8379cab19d93","identifier":null,"session_count":1,"language":"en","timezone":0,"game_version":"1","device_os":"8.1.0","device_type":1,"device_model":"Android
69
+ SDK built for x86","ad_id":null,"tags":{},"last_active":1530199447,"playtime":0,"amount_spent":0.0,"created_at":1530199447,"invalid_identifier":true,"badge_count":0,"sdk":"030901","test_type":null,"ip":null}]}'
70
+ http_version:
71
+ recorded_at: Thu, 28 Jun 2018 15:30:58 GMT
72
+ recorded_with: VCR 4.0.0
@@ -0,0 +1,145 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: https://onesignal.com/api/v1/notifications
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"contents":{"en":"Live Test"},"headings":{"en":"This is a live test
9
+ for OneSignal"},"included_segments":["Active Users"],"app_id":"22bc6dec-5150-4d6d-8628-377259d2dd14"}'
10
+ headers:
11
+ User-Agent:
12
+ - Faraday v0.15.2
13
+ Content-Type:
14
+ - application/json
15
+ Authorization:
16
+ - Basic test
17
+ Accept-Encoding:
18
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
19
+ Accept:
20
+ - "*/*"
21
+ response:
22
+ status:
23
+ code: 200
24
+ message: OK
25
+ headers:
26
+ Date:
27
+ - Thu, 28 Jun 2018 15:30:57 GMT
28
+ Content-Type:
29
+ - application/json; charset=utf-8
30
+ Transfer-Encoding:
31
+ - chunked
32
+ Connection:
33
+ - keep-alive
34
+ Set-Cookie:
35
+ - __cfduid=daaa29327ffbbadef32c14c4f68e2ddcc1530199857; expires=Fri, 28-Jun-19
36
+ 15:30:57 GMT; path=/; domain=.onesignal.com; HttpOnly
37
+ Status:
38
+ - 200 OK
39
+ Cache-Control:
40
+ - max-age=0, private, must-revalidate
41
+ Access-Control-Allow-Origin:
42
+ - "*"
43
+ X-Xss-Protection:
44
+ - 1; mode=block
45
+ X-Request-Id:
46
+ - 12e56155-1b2a-4ae2-9c43-959cf76d1219
47
+ Access-Control-Allow-Headers:
48
+ - SDK-Version
49
+ Etag:
50
+ - W/"dcc6500f0081d2c4c38f7f6490657d92"
51
+ X-Frame-Options:
52
+ - SAMEORIGIN
53
+ X-Runtime:
54
+ - '0.067198'
55
+ X-Content-Type-Options:
56
+ - nosniff
57
+ X-Powered-By:
58
+ - Phusion Passenger 5.3.2
59
+ Expect-Ct:
60
+ - max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
61
+ Server:
62
+ - cloudflare
63
+ Cf-Ray:
64
+ - 432132947a73430a-MXP
65
+ body:
66
+ encoding: ASCII-8BIT
67
+ string: '{"id":"29e9c2ad-41da-46f0-94c3-e71ee5c103dc","recipients":1}'
68
+ http_version:
69
+ recorded_at: Thu, 28 Jun 2018 15:30:57 GMT
70
+ - request:
71
+ method: get
72
+ uri: https://onesignal.com/api/v1/notifications/29e9c2ad-41da-46f0-94c3-e71ee5c103dc?app_id
73
+ body:
74
+ encoding: US-ASCII
75
+ string: ''
76
+ headers:
77
+ User-Agent:
78
+ - Faraday v0.15.2
79
+ Content-Type:
80
+ - application/json
81
+ Authorization:
82
+ - Basic NTc3Y2RhMTAtMjQzZC00NDIxLWE3MWUtNWU0OWM3YTVhZTlh
83
+ Accept-Encoding:
84
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
85
+ Accept:
86
+ - "*/*"
87
+ response:
88
+ status:
89
+ code: 200
90
+ message: OK
91
+ headers:
92
+ Date:
93
+ - Thu, 28 Jun 2018 15:30:57 GMT
94
+ Content-Type:
95
+ - application/json; charset=utf-8
96
+ Transfer-Encoding:
97
+ - chunked
98
+ Connection:
99
+ - keep-alive
100
+ Set-Cookie:
101
+ - __cfduid=daaa29327ffbbadef32c14c4f68e2ddcc1530199857; expires=Fri, 28-Jun-19
102
+ 15:30:57 GMT; path=/; domain=.onesignal.com; HttpOnly
103
+ Status:
104
+ - 200 OK
105
+ Cache-Control:
106
+ - public, max-age=7200
107
+ Access-Control-Allow-Origin:
108
+ - "*"
109
+ X-Xss-Protection:
110
+ - 1; mode=block
111
+ X-Request-Id:
112
+ - 713da936-b783-4ff4-aae6-4ec89a5e9ef7
113
+ Access-Control-Allow-Headers:
114
+ - SDK-Version
115
+ Etag:
116
+ - W/"8e772220e2df249cd3a196949cc81bfa"
117
+ X-Frame-Options:
118
+ - SAMEORIGIN
119
+ X-Runtime:
120
+ - '0.016556'
121
+ X-Content-Type-Options:
122
+ - nosniff
123
+ X-Powered-By:
124
+ - Phusion Passenger 5.3.2
125
+ Cf-Cache-Status:
126
+ - MISS
127
+ Vary:
128
+ - Accept-Encoding
129
+ Expires:
130
+ - Thu, 28 Jun 2018 17:30:57 GMT
131
+ Expect-Ct:
132
+ - max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
133
+ Server:
134
+ - cloudflare
135
+ Cf-Ray:
136
+ - 432132961c5b430a-MXP
137
+ body:
138
+ encoding: ASCII-8BIT
139
+ string: '{"adm_big_picture":null,"adm_group":null,"adm_group_message":null,"adm_large_icon":null,"adm_small_icon":null,"adm_sound":null,"spoken_text":null,"alexa_ssml":null,"alexa_display_title":null,"amazon_background_data":null,"android_accent_color":null,"android_group":null,"android_group_message":null,"android_led_color":null,"android_sound":null,"android_visibility":null,"app_id":"22bc6dec-5150-4d6d-8628-377259d2dd14","big_picture":null,"buttons":null,"canceled":false,"chrome_big_picture":null,"chrome_icon":null,"chrome_web_icon":null,"chrome_web_image":null,"chrome_web_badge":null,"content_available":null,"contents":{"en":"Live
140
+ Test"},"converted":0,"data":null,"delayed_option":null,"delivery_time_of_day":null,"errored":0,"excluded_segments":[],"failed":0,"firefox_icon":null,"headings":{"en":"This
141
+ is a live test for OneSignal"},"id":"29e9c2ad-41da-46f0-94c3-e71ee5c103dc","include_player_ids":null,"included_segments":["Active
142
+ Users"],"ios_badgeCount":null,"ios_badgeType":null,"ios_category":null,"ios_sound":null,"apns_alert":null,"isAdm":false,"isAndroid":true,"isChrome":false,"isChromeWeb":false,"isAlexa":false,"isFirefox":false,"isIos":true,"isSafari":false,"isWP":false,"isWP_WNS":false,"isEdge":false,"large_icon":null,"priority":null,"queued_at":1530199857,"remaining":1,"send_after":1530199857,"completed_at":null,"small_icon":null,"successful":0,"tags":null,"filters":null,"template_id":null,"ttl":null,"url":null,"web_buttons":null,"web_push_topic":null,"wp_sound":null,"wp_wns_sound":null}'
143
+ http_version:
144
+ recorded_at: Thu, 28 Jun 2018 15:30:57 GMT
145
+ recorded_with: VCR 4.0.0
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/string'
4
+ require 'active_support/json'
5
+ require 'onesignal/version'
6
+ require 'onesignal/commands'
7
+
8
+ ActiveSupport.escape_html_entities_in_json = false
9
+
10
+ module OneSignal
11
+ class << self
12
+ def configure
13
+ yield config
14
+ end
15
+
16
+ def send_notification notification
17
+ return unless OneSignal.config.active
18
+
19
+ created = Commands::CreateNotification.call notification
20
+ fetch_notification(JSON.parse(created.body)['id'])
21
+ end
22
+
23
+ def fetch_notification notification_id
24
+ return unless OneSignal.config.active
25
+
26
+ fetched = Commands::FetchNotification.call notification_id
27
+ Responses::Notification.from_json fetched.body
28
+ end
29
+
30
+ def fetch_player player_id
31
+ return unless OneSignal.config.active
32
+
33
+ fetched = Commands::FetchPlayer.call player_id
34
+ Responses::Player.from_json fetched.body
35
+ end
36
+
37
+ def fetch_players
38
+ return unless OneSignal.config.active
39
+
40
+ fetched = Commands::FetchPlayers.call
41
+ JSON.parse(fetched.body)['players'].map { |player| Responses::Player.from_json player }
42
+ end
43
+
44
+ def csv_export params = {}
45
+ return unless OneSignal.config.active
46
+
47
+ fetched = Commands::CsvExport.call params
48
+ Responses::CsvExport.from_json fetched.body
49
+ end
50
+
51
+ def config
52
+ @config ||= Configuration.new
53
+ end
54
+
55
+ alias define configure
56
+ end
57
+ end
58
+
59
+ require 'onesignal/autoloader'
60
+ require 'onesignal/responses'
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OneSignal
4
+ class Attachments
5
+ attr_reader :data, :url, :ios_attachments, :android_picture, :amazon_picture, :chrome_picture
6
+
7
+ def initialize data: nil, url: nil, ios_attachments: nil, android_picture: nil, amazon_picture: nil, chrome_picture: nil
8
+ @data = data
9
+ @url = url
10
+ @ios_attachments = ios_attachments
11
+ @android_picture = android_picture
12
+ @amazon_picture = amazon_picture
13
+ @chrome_picture = chrome_picture
14
+ end
15
+
16
+ def as_json options = nil
17
+ {
18
+ 'data' => @data.as_json(options),
19
+ 'url' => @url,
20
+ 'ios_attachments' => @ios_attachments.as_json(options),
21
+ 'big_picture' => @android_picture,
22
+ 'adm_big_picture' => @amazon_picture,
23
+ 'chrome_big_picture' => @chrome_picture
24
+ }
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OneSignal
4
+ module AutoMap
5
+ def create_readers **attributes
6
+ attributes.keys.each do |k|
7
+ self.class.define_method k do
8
+ attributes[k]
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ Dir["#{File.expand_path(__dir__)}/*.rb"].each do |file|
4
+ filename = File.basename file
5
+ classname = filename.split('.rb').first.camelize
6
+ OneSignal.autoload classname, File.expand_path("../#{filename}", __FILE__)
7
+ end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+
5
+ module OneSignal
6
+ class Client
7
+ class ApiError < RuntimeError; end
8
+
9
+ def initialize app_id, api_key, api_url
10
+ @app_id = app_id
11
+ @api_key = api_key
12
+ @api_url = api_url
13
+ @conn = ::Faraday.new(url: api_url) do |faraday|
14
+ # faraday.response :logger do |logger|
15
+ # logger.filter(/(api_key=)(\w+)/, '\1[REMOVED]')
16
+ # logger.filter(/(Basic )(\w+)/, '\1[REMOVED]')
17
+ # end
18
+ faraday.adapter Faraday.default_adapter
19
+ end
20
+ end
21
+
22
+ def create_notification notification
23
+ post 'notifications', notification
24
+ end
25
+
26
+ def fetch_notification notification_id
27
+ get "notifications/#{notification_id}"
28
+ end
29
+
30
+ def fetch_players
31
+ get 'players'
32
+ end
33
+
34
+ def fetch_player player_id
35
+ get "players/#{player_id}"
36
+ end
37
+
38
+ def csv_export extra_fields: nil, last_active_since: nil, segment_name: nil
39
+ post "players/csv_export?app_id=#{@app_id}",
40
+ extra_fields: extra_fields,
41
+ last_active_since: last_active_since&.to_i&.to_s,
42
+ segment_name: segment_name
43
+ end
44
+
45
+ private
46
+
47
+ def create_body payload
48
+ body = payload.as_json.delete_if { |_, v| v.nil? }
49
+ body['app_id'] = @app_id
50
+ body
51
+ end
52
+
53
+ def post url, body
54
+ res = @conn.post do |req|
55
+ req.url url
56
+ req.body = create_body(body).to_json
57
+ req.headers['Content-Type'] = 'application/json'
58
+ req.headers['Authorization'] = "Basic #{@api_key}"
59
+ end
60
+
61
+ handle_errors res
62
+ end
63
+
64
+ def get url
65
+ res = @conn.get do |req|
66
+ req.url url, app_id: @app_id
67
+ req.headers['Content-Type'] = 'application/json'
68
+ req.headers['Authorization'] = "Basic #{@api_key}"
69
+ end
70
+
71
+ handle_errors res
72
+ end
73
+
74
+ def handle_errors res
75
+ errors = JSON.parse(res.body).fetch 'errors', []
76
+ raise ApiError, (errors.first || "Error code #{res.status}") if res.status > 399 || errors.any?
77
+
78
+ res
79
+ end
80
+ end
81
+ end