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.
@@ -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(app_id:)
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: {app_id: app_id})
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: "", 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: nil, 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
@@ -1,3 +1,3 @@
1
1
  module OneSignal
2
- VERSION = "0.0.14"
2
+ VERSION = '1.2.1'
3
3
  end
data/one_signal.gemspec CHANGED
@@ -1,9 +1,8 @@
1
1
  require File.expand_path('lib/one_signal/version')
2
2
 
3
3
  Gem::Specification.new do |s|
4
- s.required_ruby_version = '>= 2.1.0'
5
- s.add_runtime_dependency 'json', '~> 1.8'
6
- s.add_development_dependency 'rake', '~> 10.4'
4
+ s.required_ruby_version = '>= 2.0.0'
5
+ s.add_development_dependency 'rake', '~> 12.0'
7
6
  s.add_development_dependency 'mocha', '~> 1.1'
8
7
  s.add_development_dependency 'dotenv', '~> 2.0'
9
8
  s.add_development_dependency 'minitest', '~> 5.8'
data/test/app_test.rb CHANGED
@@ -15,40 +15,81 @@ class AppTest < MiniTest::Test
15
15
  widget: "acme"
16
16
  }
17
17
 
18
- @user_auth_key = "fake user auth key"
19
- OneSignal::OneSignal.user_auth_key = @user_auth_key
18
+ @user_auth_key = "fake user auth key 123"
19
+ @user_auth_key_2 = "fake user auth key 456"
20
+ @opts = {
21
+ key1: "value1",
22
+ key2: "vaue2",
23
+ auth_key: @user_auth_key_2
24
+ }
25
+ @default_opts = {
26
+ auth_key: @user_auth_key
27
+ }
28
+ OneSignal::OneSignal.user_auth_key = @user_auth_key
20
29
  end
21
30
 
22
31
  def test_all
23
32
  response = mock_response_ok
24
33
  OneSignal::OneSignal.expects(:send_get_request)
25
- .with(uri: @all_uri, params: @params)
34
+ .with(uri: @all_uri, params: @params, opts: @default_opts)
26
35
  .returns(response)
27
36
  assert_equal response, OneSignal::App.all(params: @params)
28
37
  end
29
38
 
39
+ def test_all_with_auth_key
40
+ response = mock_response_ok
41
+ OneSignal::OneSignal.expects(:send_get_request)
42
+ .with(uri: @all_uri, params: @params, opts: @opts)
43
+ .returns(response)
44
+ assert_equal response, OneSignal::App.all(params: @params, opts: @opts)
45
+ end
46
+
30
47
  def test_get
31
48
  response = mock_response_ok
32
49
  OneSignal::OneSignal.expects(:send_get_request)
33
- .with(uri: @get_uri, params: nil)
50
+ .with(uri: @get_uri, params: nil, opts: @default_opts)
34
51
  .returns(response)
35
52
  assert_equal response, OneSignal::App.get(id: @app_id)
36
53
  end
37
54
 
55
+ def test_get_with_auth_key
56
+ response = mock_response_ok
57
+ OneSignal::OneSignal.expects(:send_get_request)
58
+ .with(uri: @get_uri, params: nil, opts: @opts)
59
+ .returns(response)
60
+ assert_equal response, OneSignal::App.get(id: @app_id, opts: @opts)
61
+ end
62
+
38
63
  def test_create
39
64
  response = mock_response_ok
40
65
  OneSignal::OneSignal.expects(:send_post_request)
41
- .with(uri: @create_uri, body: @params)
66
+ .with(uri: @create_uri, body: @params, opts: @default_opts)
42
67
  .returns(response)
43
68
  assert_equal response, OneSignal::App.create(params: @params)
44
69
  end
45
70
 
71
+ def test_create_with_auth_key
72
+ response = mock_response_ok
73
+ OneSignal::OneSignal.expects(:send_post_request)
74
+ .with(uri: @create_uri, body: @params, opts: @opts)
75
+ .returns(response)
76
+ assert_equal response, OneSignal::App.create(params: @params, opts: @opts)
77
+ end
78
+
46
79
  def test_update
47
80
  response = mock_response_ok
48
81
  OneSignal::OneSignal.expects(:send_put_request)
49
- .with(uri: @update_uri, body: @params)
82
+ .with(uri: @update_uri, body: @params, opts: @default_opts)
50
83
  .returns(response)
51
84
  assert_equal response, OneSignal::App.update(id: @app_id, params: @params)
52
85
  end
53
86
 
87
+ def test_update_with_auth_key
88
+ response = mock_response_ok
89
+ OneSignal::OneSignal.expects(:send_put_request)
90
+ .with(uri: @update_uri, body: @params, opts: @opts)
91
+ .returns(response)
92
+ assert_equal response, OneSignal::App.update(id: @app_id, params: @params, opts: @opts)
93
+ end
94
+
54
95
  end