one_signal 1.0.0 → 1.2.2

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
- # OneSignal Ruby bindings [![Build Status](https://travis-ci.org/tbalthazar/onesignal-ruby.svg?branch=master)](https://travis-ci.org/tbalthazar/onesignal-ruby)
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
 
@@ -10,23 +12,26 @@ gem install one_signal
10
12
 
11
13
  ## Development
12
14
 
15
+ Run `bin/setup` to install dependencies and set your API keys.
16
+
13
17
  Run the tests
14
18
 
15
19
  ```
16
20
  bundle exec rake
17
21
  ```
18
22
 
23
+ You can also run `bin/console` for an interactive prompt that will allow you to experiment with authenticated library methods.
24
+
19
25
  ## Basic usage
20
26
 
21
- See basic examples in [example.rb](/example.rb).
27
+ See some basic examples in [example.rb](/example.rb).
22
28
  To run it:
23
- - rename [.env.example](/.env.example) into `.env`
24
- - set your `USER_AUTH_KEY` and `API_KEY` in the `.env` file
25
- - run `ruby example.rb`
29
+ - run `bin/setup` to set your API keys
30
+ - then run `ruby example.rb`
26
31
 
27
32
  ## Documentation
28
33
 
29
- Specify your User Auth key to deal with Apps :
34
+ Specify your User Auth key to deal with Apps:
30
35
 
31
36
  ```ruby
32
37
  OneSignal::OneSignal.user_auth_key = YOUR_USER_AUTH_KEY
@@ -39,7 +44,23 @@ OneSignal::OneSignal.api_key = YOUR_API_KEY
39
44
  ```
40
45
 
41
46
  Then call the following methods on the `App`, `Player` and `Notification` classes.
42
- 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)
47
+ 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)
48
+
49
+ 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:
50
+
51
+ ```ruby
52
+ app_1_api_key = "fake api key 1"
53
+ app_2_api_key = "fake api key 2"
54
+
55
+ # by default, method calls will use app_1_api_key
56
+ OneSignal::OneSignal.api_key = app_1_api_key
57
+
58
+ # get player with id "123" in app_1
59
+ OneSignal::Player.get(id: "123")
60
+
61
+ # get player with id "456" in app_2
62
+ OneSignal::Player.get(id: "123", opts: {auth_key: app_2_api_key})
63
+ ```
43
64
 
44
65
  The return value of each method is a `Net::HTTPResponse`.
45
66
 
@@ -56,13 +77,14 @@ The return value of each method is a `Net::HTTPResponse`.
56
77
 
57
78
  ```ruby
58
79
  - OneSignal::Player.all(params:)
59
- - OneSignal::Player.csv_export(app_id:)
80
+ - OneSignal::Player.csv_export(params:)
60
81
  - OneSignal::Player.get(id:)
61
82
  - OneSignal::Player.create(params:)
62
83
  - OneSignal::Player.create_session(id:, params:)
63
84
  - OneSignal::Player.create_purchase(id:, params:)
64
85
  - OneSignal::Player.create_focus(id:, params:)
65
86
  - OneSignal::Player.update(id:, params:)
87
+ - OneSignal::Player.delete(id:, params:)
66
88
  ```
67
89
 
68
90
  ### Notifications
@@ -85,4 +107,4 @@ They are [listed here](https://github.com/tbalthazar/onesignal-ruby/graphs/contr
85
107
 
86
108
  ## License
87
109
 
88
- Please see [LICENSE](/LICENSE) for licensing details.
110
+ Please see [LICENSE](/LICENSE) for licensing details.
data/bin/console ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "dotenv/load"
5
+ require "one_signal"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ OneSignal::OneSignal.user_auth_key = ENV['USER_AUTH_KEY']
11
+ OneSignal::OneSignal.api_key = ENV['API_KEY']
12
+
13
+ # (If you use this, don't forget to add pry to your Gemfile!)
14
+ # require "pry"
15
+ # Pry.start
16
+
17
+ require "irb"
18
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ echo "Enter your OneSignal credentials."
8
+ echo "For dealing with Apps, you'll need a user auth key."
9
+ read -p 'User Auth Key: ' user_auth_key
10
+ echo "For dealing with Players and Notifications, you'll need an API key."
11
+ read -p 'API Key: ' api_key
12
+
13
+ cp .env.example .env
14
+
15
+ sed -i '' -e "s/YOUR_USER_AUTH_KEY_HERE/$user_auth_key/g" .env
16
+ sed -i '' -e "s/YOUR_API_KEY_HERE/$api_key/g" .env
17
+
18
+ echo "Done."
@@ -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
@@ -2,12 +2,14 @@ module OneSignal
2
2
 
3
3
  class Notification < OneSignal
4
4
 
5
- def self.all(params: {})
5
+ def self.all(params: {}, opts: {})
6
+ opts[:auth_key] ||= @@api_key
7
+
6
8
  uri_string = @@base_uri
7
9
  uri_string += "/notifications"
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:, params:)
23
+ def self.get(id: "", params: {}, opts: {})
24
+ opts[:auth_key] ||= @@api_key
25
+
22
26
  uri_string = @@base_uri
23
27
  uri_string += "/notifications"
24
28
  uri_string += "/#{id}"
25
29
  uri = URI.parse(uri_string)
26
30
 
27
- response = send_get_request(uri: uri, params: params)
31
+ response = send_get_request(uri: uri, params: params, 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] ||= @@api_key
44
+
39
45
  uri_string = @@base_uri
40
46
  uri_string += "/notifications"
41
47
  uri = URI.parse(uri_string)
42
-
43
- response = send_post_request(uri: uri, body: params)
48
+
49
+ response = send_post_request(uri: uri, body: params, opts: opts)
44
50
 
45
51
  if response.code != '200'
46
52
  handle_error(uri: uri, params: params, response: response)
@@ -49,13 +55,15 @@ module OneSignal
49
55
  return response
50
56
  end
51
57
 
52
- def self.update(id:, params: {})
58
+ def self.update(id: "", params: {}, opts: {})
59
+ opts[:auth_key] ||= @@api_key
60
+
53
61
  uri_string = @@base_uri
54
62
  uri_string += "/notifications"
55
63
  uri_string += "/#{id}"
56
64
  uri = URI.parse(uri_string)
57
65
 
58
- response = send_put_request(uri: uri, body: params)
66
+ response = send_put_request(uri: uri, body: params, opts: opts)
59
67
 
60
68
  ensure_http_status(response: response,
61
69
  status: '200',
@@ -66,13 +74,15 @@ module OneSignal
66
74
  return response
67
75
  end
68
76
 
69
- def self.delete(id:, params:)
77
+ def self.delete(id: "", params: {}, opts: {})
78
+ opts[:auth_key] ||= @@api_key
79
+
70
80
  uri_string = @@base_uri
71
81
  uri_string += "/notifications"
72
82
  uri_string += "/#{id}"
73
83
  uri = URI.parse(uri_string)
74
84
 
75
- response = send_delete_request(uri: uri, params: params)
85
+ response = send_delete_request(uri: uri, params: params, opts: opts)
76
86
 
77
87
  ensure_http_status(response: response,
78
88
  status: '200',
@@ -85,21 +95,26 @@ module OneSignal
85
95
 
86
96
  private
87
97
 
88
- def self.handle_error(uri:, params:, response:)
98
+ def self.handle_error(uri: nil, params: {}, response: nil)
89
99
  msg = "Create Notification error - uri: #{uri} params: #{params}"
100
+ unless response.nil?
101
+ code = response.code
102
+ body = response.body
103
+ end
90
104
  if is_no_recipients_error(response: response)
91
105
  raise NoRecipientsError.new(message: msg,
92
- http_status: response.code,
93
- http_body: response.body)
106
+ http_status: code,
107
+ http_body: body)
94
108
  else
95
109
  raise OneSignalError.new(message: msg,
96
- http_status: response.code,
97
- http_body: response.body)
110
+ http_status: code,
111
+ http_body: body)
98
112
  end
99
113
  end
100
114
 
101
- def self.is_no_recipients_error(response:)
102
- return false if response.code != '400' ||
115
+ def self.is_no_recipients_error(response: nil)
116
+ return false if response.nil? ||
117
+ response.code != '400' ||
103
118
  response.body.nil?
104
119
 
105
120
  body = JSON.parse(response.body)
@@ -43,7 +43,9 @@ module OneSignal
43
43
  @@read_timeout
44
44
  end
45
45
 
46
- def self.http_object(uri:)
46
+ def self.http_object(uri: nil)
47
+ return nil if uri.nil?
48
+
47
49
  http = Net::HTTP.new(uri.host, uri.port)
48
50
  http.use_ssl = uri.scheme == 'https'
49
51
  http.open_timeout = @@open_timeout
@@ -51,49 +53,61 @@ module OneSignal
51
53
  return http
52
54
  end
53
55
 
54
- def self.send_post_request(uri:, body:)
56
+ def self.send_post_request(uri: nil, body: nil, opts: {})
57
+ return nil if uri.nil?
58
+
55
59
  request = Net::HTTP::Post.new(uri.request_uri)
56
60
  request.body = body.to_json
57
- request = request_with_headers(request: request)
61
+ request = request_with_headers(request: request, opts: opts)
58
62
 
59
63
  http = http_object(uri: uri)
60
64
 
61
- response = http.request(request)
65
+ return http.request(request)
62
66
  end
63
67
 
64
- def self.send_delete_request(uri:, params: {})
68
+ def self.send_delete_request(uri: nil, params: {}, opts: {})
69
+ return nil if uri.nil?
70
+
65
71
  uri.query = URI.encode_www_form(params) unless params.nil?
66
72
  request = Net::HTTP::Delete.new(uri.request_uri)
67
- request = request_with_headers(request: request)
73
+ request = request_with_headers(request: request, opts: opts)
68
74
 
69
75
  http = http_object(uri: uri)
70
76
 
71
- response = http.request(request)
77
+ return http.request(request)
72
78
  end
73
79
 
74
- def self.send_put_request(uri:, body:)
80
+ def self.send_put_request(uri: nil, body: nil, opts: {})
81
+ return nil if uri.nil?
82
+
75
83
  request = Net::HTTP::Put.new(uri.request_uri)
76
84
  request.body = body.to_json
77
- request = request_with_headers(request: request)
85
+ request = request_with_headers(request: request, opts: opts)
78
86
 
79
87
  http = http_object(uri: uri)
80
88
 
81
- response = http.request(request)
89
+ return http.request(request)
82
90
  end
83
91
 
84
- def self.send_get_request(uri:, params: {})
92
+ def self.send_get_request(uri: nil, params: {}, opts: {})
93
+ return nil if uri.nil?
94
+
85
95
  uri.query = URI.encode_www_form(params) unless params.nil?
86
96
  request = Net::HTTP::Get.new(uri.request_uri)
87
- request = request_with_headers(request: request)
97
+ request = request_with_headers(request: request, opts: opts)
88
98
 
89
99
  http = http_object(uri: uri)
90
100
 
91
- response = http.request(request)
101
+ return http.request(request)
92
102
  end
93
103
 
94
104
  private
95
105
 
96
- def self.ensure_http_status(response:, status:, method_name:, uri:, params:)
106
+ def self.ensure_http_status(response: nil, status: nil, method_name: "", uri: "", params: {})
107
+ if response.nil? || status.nil?
108
+ raise OneSignalError.new(message: "Please provide a response (#{response}) and a status (#{status})")
109
+ end
110
+
97
111
  if response.code != status.to_s
98
112
  msg = "#{self.name}##{method_name} error - uri: #{uri} params: #{params}"
99
113
  raise OneSignalError.new(message: msg,
@@ -102,16 +116,14 @@ module OneSignal
102
116
  end
103
117
  end
104
118
 
105
- def self.request_with_headers(request:)
119
+ def self.request_with_headers(request: nil, opts: {})
120
+ return nil if request.nil?
121
+
106
122
  request.add_field("Content-Type", "application/json")
107
- request.add_field("Authorization", "Basic #{self.auth_key}")
123
+ request.add_field("Authorization", "Basic #{opts[:auth_key]}")
108
124
  return request
109
125
  end
110
126
 
111
- def self.auth_key
112
- return @@api_key
113
- end
114
-
115
127
  end
116
128
 
117
129
  end
@@ -2,12 +2,14 @@ module OneSignal
2
2
 
3
3
  class Player < OneSignal
4
4
 
5
- def self.csv_export(params:)
5
+ def self.csv_export(params: {}, opts: {})
6
+ opts[:auth_key] ||= @@api_key
7
+
6
8
  uri_string = @@base_uri
7
9
  uri_string += "/players/csv_export"
8
10
  uri = URI.parse(uri_string)
9
11
 
10
- response = send_post_request(uri: uri, body: params)
12
+ response = send_post_request(uri: uri, body: params, opts: opts)
11
13
 
12
14
  ensure_http_status(response: response,
13
15
  status: '200',
@@ -18,12 +20,14 @@ module OneSignal
18
20
  return response
19
21
  end
20
22
 
21
- def self.all(params: {})
23
+ def self.all(params: {}, opts: {})
24
+ opts[:auth_key] ||= @@api_key
25
+
22
26
  uri_string = @@base_uri
23
27
  uri_string += "/players"
24
28
  uri = URI.parse(uri_string)
25
29
 
26
- response = send_get_request(uri: uri, params: params)
30
+ response = send_get_request(uri: uri, params: params, opts: opts)
27
31
 
28
32
  ensure_http_status(response: response,
29
33
  status: '200',
@@ -34,13 +38,15 @@ module OneSignal
34
38
  return response
35
39
  end
36
40
 
37
- def self.get(id:)
41
+ def self.get(id: "", opts: {})
42
+ opts[:auth_key] ||= @@api_key
43
+
38
44
  uri_string = @@base_uri
39
45
  uri_string += "/players"
40
46
  uri_string += "/#{id}"
41
47
  uri = URI.parse(uri_string)
42
-
43
- response = send_get_request(uri: uri, params: nil)
48
+
49
+ response = send_get_request(uri: uri, params: nil, opts: opts)
44
50
 
45
51
  ensure_http_status(response: response,
46
52
  status: '200',
@@ -51,12 +57,14 @@ module OneSignal
51
57
  return response
52
58
  end
53
59
 
54
- def self.create(params: {})
60
+ def self.create(params: {}, opts: {})
61
+ opts[:auth_key] ||= @@api_key
62
+
55
63
  uri_string = @@base_uri
56
64
  uri_string += "/players"
57
65
  uri = URI.parse(uri_string)
58
66
 
59
- response = send_post_request(uri: uri, body: params)
67
+ response = send_post_request(uri: uri, body: params, opts: opts)
60
68
 
61
69
  ensure_http_status(response: response,
62
70
  status: '200',
@@ -67,14 +75,16 @@ module OneSignal
67
75
  return response
68
76
  end
69
77
 
70
- def self.create_session(id:, params: {})
78
+ def self.create_session(id: "", params: {}, opts: {})
79
+ opts[:auth_key] ||= @@api_key
80
+
71
81
  uri_string = @@base_uri
72
82
  uri_string += "/players"
73
83
  uri_string += "/#{id}"
74
84
  uri_string += "/on_session"
75
85
  uri = URI.parse(uri_string)
76
86
 
77
- response = send_post_request(uri: uri, body: params)
87
+ response = send_post_request(uri: uri, body: params, opts: opts)
78
88
 
79
89
  ensure_http_status(response: response,
80
90
  status: '200',
@@ -85,14 +95,16 @@ module OneSignal
85
95
  return response
86
96
  end
87
97
 
88
- def self.create_purchase(id:, params: {})
98
+ def self.create_purchase(id: "", params: {}, opts: {})
99
+ opts[:auth_key] ||= @@api_key
100
+
89
101
  uri_string = @@base_uri
90
102
  uri_string += "/players"
91
103
  uri_string += "/#{id}"
92
104
  uri_string += "/on_purchase"
93
105
  uri = URI.parse(uri_string)
94
106
 
95
- response = send_post_request(uri: uri, body: params)
107
+ response = send_post_request(uri: uri, body: params, opts: opts)
96
108
 
97
109
  ensure_http_status(response: response,
98
110
  status: '200',
@@ -103,14 +115,16 @@ module OneSignal
103
115
  return response
104
116
  end
105
117
 
106
- def self.create_focus(id:, params: {})
118
+ def self.create_focus(id: "", params: {}, opts: {})
119
+ opts[:auth_key] ||= @@api_key
120
+
107
121
  uri_string = @@base_uri
108
122
  uri_string += "/players"
109
123
  uri_string += "/#{id}"
110
124
  uri_string += "/on_focus"
111
125
  uri = URI.parse(uri_string)
112
126
 
113
- response = send_post_request(uri: uri, body: params)
127
+ response = send_post_request(uri: uri, body: params, opts: opts)
114
128
 
115
129
  ensure_http_status(response: response,
116
130
  status: '200',
@@ -121,13 +135,15 @@ module OneSignal
121
135
  return response
122
136
  end
123
137
 
124
- def self.update(id:, params: {})
138
+ def self.update(id: "", params: {}, opts: {})
139
+ opts[:auth_key] ||= @@api_key
140
+
125
141
  uri_string = @@base_uri
126
142
  uri_string += "/players"
127
143
  uri_string += "/#{id}"
128
144
  uri = URI.parse(uri_string)
129
145
 
130
- response = send_put_request(uri: uri, body: params)
146
+ response = send_put_request(uri: uri, body: params, opts: opts)
131
147
 
132
148
  ensure_http_status(response: response,
133
149
  status: '200',
@@ -138,6 +154,25 @@ module OneSignal
138
154
  return response
139
155
  end
140
156
 
157
+ def self.delete(id: "", params: {}, opts: {})
158
+ opts[:auth_key] ||= @@api_key
159
+
160
+ uri_string = @@base_uri
161
+ uri_string += "/players"
162
+ uri_string += "/#{id}"
163
+ uri = URI.parse(uri_string)
164
+
165
+ response = send_delete_request(uri: uri, params: params, opts: opts)
166
+
167
+ ensure_http_status(response: response,
168
+ status: '200',
169
+ method_name: 'Delete',
170
+ uri: uri,
171
+ params: nil)
172
+
173
+ return response
174
+ end
175
+
141
176
  end
142
177
 
143
178
  end