one_signal 1.1.0 → 1.2.0

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: 4d0c9f33f989591f7d6ab3631d728a25468bd25e
4
- data.tar.gz: eb95288dede0228e6ed8a625bc4a6a9048e2e221
3
+ metadata.gz: 39e3129d534bb2ff23d4ea7f30883ba5542b5d74
4
+ data.tar.gz: 325c87cd5d9326f3148901b549e80608a603ac8b
5
5
  SHA512:
6
- metadata.gz: 8d3e910a05cb29cf3df627caa26ca3cd4262bb86241669105a3dcd4e1004b5d28f4a0834a18a5d4d4ea4e6797c0a745604204f8cb509973226d3f33be5174b61
7
- data.tar.gz: eb4182003d2cd13fa8f25dbe3102d9279476fb9b2848c74a67d3d88b18f22b34538f19496ea48f2e97f1dd365de00194234f53c5c3ed23c47bcf35db10bc958f
6
+ metadata.gz: 8e27d6a7c6e3b5568b6fb62a9caa11c17e73fcddae107d3388d822a9d9485cb08b5b93f3dad323c574c2944f4cd2a7ba8802a7139d767e4f8634c5663f38c76d
7
+ data.tar.gz: 462d24d61957420d0cee81fba85537ba6256c13c944e163b3a46b0a4a804ac6337cbae187943de1c8b69a57213cd3ed249dddd759f9e6af5f251c0015402bb6e
@@ -1,3 +1,7 @@
1
+ === 1.2.0 2016-04-26
2
+
3
+ * Add an optional `opts` hash to each method. It allows to specify the auth_key on a per method basis.
4
+
1
5
  === 1.1.0 2016-04-08
2
6
 
3
7
  * Add compatibility with Ruby 2.0
data/README.md CHANGED
@@ -26,7 +26,7 @@ To run it:
26
26
 
27
27
  ## Documentation
28
28
 
29
- Specify your User Auth key to deal with Apps :
29
+ Specify your User Auth key to deal with Apps:
30
30
 
31
31
  ```ruby
32
32
  OneSignal::OneSignal.user_auth_key = YOUR_USER_AUTH_KEY
@@ -41,6 +41,22 @@ OneSignal::OneSignal.api_key = YOUR_API_KEY
41
41
  Then call the following methods on the `App`, `Player` and `Notification` classes.
42
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)
43
43
 
44
+ Each method also accepts an optional `opts` hash that allows you to specify the `user_auth_key`/`api_key` on a per method basis:
45
+
46
+ ```ruby
47
+ app_1_api_key = "fake api key 1"
48
+ app_2_api_key = "fake api key 2"
49
+
50
+ # by default, method calls will use app_1_api_key
51
+ OneSignal::OneSignal.api_key = app_1_api_key
52
+
53
+ # get player with id "123" in app_1
54
+ OneSignal::Player.get(id: "123")
55
+
56
+ # get player with id "456" in app_2
57
+ OneSignal::Player.get(id: "123", opts: {auth_key: app_2_api_key})
58
+ ```
59
+
44
60
  The return value of each method is a `Net::HTTPResponse`.
45
61
 
46
62
  ### Apps
@@ -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',
@@ -53,48 +53,48 @@ module OneSignal
53
53
  return http
54
54
  end
55
55
 
56
- def self.send_post_request(uri: nil, body: nil)
56
+ def self.send_post_request(uri: nil, body: nil, opts: {})
57
57
  return nil if uri.nil?
58
58
 
59
59
  request = Net::HTTP::Post.new(uri.request_uri)
60
60
  request.body = body.to_json
61
- request = request_with_headers(request: request)
61
+ request = request_with_headers(request: request, opts: opts)
62
62
 
63
63
  http = http_object(uri: uri)
64
64
 
65
65
  return http.request(request)
66
66
  end
67
67
 
68
- def self.send_delete_request(uri: nil, params: {})
68
+ def self.send_delete_request(uri: nil, params: {}, opts: {})
69
69
  return nil if uri.nil?
70
70
 
71
71
  uri.query = URI.encode_www_form(params) unless params.nil?
72
72
  request = Net::HTTP::Delete.new(uri.request_uri)
73
- request = request_with_headers(request: request)
73
+ request = request_with_headers(request: request, opts: opts)
74
74
 
75
75
  http = http_object(uri: uri)
76
76
 
77
77
  return http.request(request)
78
78
  end
79
79
 
80
- def self.send_put_request(uri: nil, body: nil)
80
+ def self.send_put_request(uri: nil, body: nil, opts: {})
81
81
  return nil if uri.nil?
82
82
 
83
83
  request = Net::HTTP::Put.new(uri.request_uri)
84
84
  request.body = body.to_json
85
- request = request_with_headers(request: request)
85
+ request = request_with_headers(request: request, opts: opts)
86
86
 
87
87
  http = http_object(uri: uri)
88
88
 
89
89
  return http.request(request)
90
90
  end
91
91
 
92
- def self.send_get_request(uri: nil, params: {})
92
+ def self.send_get_request(uri: nil, params: {}, opts: {})
93
93
  return nil if uri.nil?
94
94
 
95
95
  uri.query = URI.encode_www_form(params) unless params.nil?
96
96
  request = Net::HTTP::Get.new(uri.request_uri)
97
- request = request_with_headers(request: request)
97
+ request = request_with_headers(request: request, opts: opts)
98
98
 
99
99
  http = http_object(uri: uri)
100
100
 
@@ -116,18 +116,14 @@ module OneSignal
116
116
  end
117
117
  end
118
118
 
119
- def self.request_with_headers(request: nil)
119
+ def self.request_with_headers(request: nil, opts: {})
120
120
  return nil if request.nil?
121
121
 
122
122
  request.add_field("Content-Type", "application/json")
123
- request.add_field("Authorization", "Basic #{self.auth_key}")
123
+ request.add_field("Authorization", "Basic #{opts[:auth_key]}")
124
124
  return request
125
125
  end
126
126
 
127
- def self.auth_key
128
- return @@api_key
129
- end
130
-
131
127
  end
132
128
 
133
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
48
 
43
- response = send_get_request(uri: uri, params: nil)
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',
@@ -1,3 +1,3 @@
1
1
  module OneSignal
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -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
@@ -10,35 +10,63 @@ class NotificationTest < MiniTest::Test
10
10
  @delete_uri = URI.parse(base_url + "/#{@notification_id}")
11
11
  @all_uri = URI.parse(base_url)
12
12
  @get_uri = URI.parse(base_url + "/#{@notification_id}")
13
+ @api_key = "fake api key 123"
14
+ @api_key_2 = "fake api key 456"
13
15
  @params = {
14
16
  foo: "bar",
15
17
  widget: "acme"
16
18
  }
17
- @api_key = "fake api key"
19
+ @opts = {
20
+ key1: "value1",
21
+ key2: "vaue2",
22
+ auth_key: @api_key_2
23
+ }
24
+ @default_opts = {
25
+ auth_key: @api_key
26
+ }
18
27
  OneSignal::OneSignal.api_key = @api_key
19
28
  end
20
29
 
21
30
  def test_all
22
31
  response = mock_response_ok
23
32
  OneSignal::OneSignal.expects(:send_get_request)
24
- .with(uri: @all_uri, params: @params)
33
+ .with(uri: @all_uri, params: @params, opts: @default_opts)
25
34
  .returns(response)
26
35
  assert_equal response, OneSignal::Notification.all(params: @params)
27
36
  end
28
37
 
38
+ def test_all_with_auth_key
39
+ response = mock_response_ok
40
+ OneSignal::OneSignal.expects(:send_get_request)
41
+ .with(uri: @all_uri, params: @params, opts: @opts)
42
+ .returns(response)
43
+ assert_equal response, OneSignal::Notification.all(params: @params,
44
+ opts: @opts)
45
+ end
46
+
29
47
  def test_get
30
48
  response = mock_response_ok
31
49
  OneSignal::OneSignal.expects(:send_get_request)
32
- .with(uri: @get_uri, params: @params)
50
+ .with(uri: @get_uri, params: @params, opts: @default_opts)
33
51
  .returns(response)
34
52
  assert_equal response, OneSignal::Notification.get(id: @notification_id,
35
53
  params: @params)
36
54
  end
37
55
 
56
+ def test_get_with_auth_key
57
+ response = mock_response_ok
58
+ OneSignal::OneSignal.expects(:send_get_request)
59
+ .with(uri: @get_uri, params: @params, opts: @opts)
60
+ .returns(response)
61
+ assert_equal response, OneSignal::Notification.get(id: @notification_id,
62
+ params: @params,
63
+ opts: @opts)
64
+ end
65
+
38
66
  def test_create_raises_onesignal_error
39
67
  response = mock_response_ko
40
68
  OneSignal::OneSignal.expects(:send_post_request)
41
- .with(uri: @create_uri, body: @params)
69
+ .with(uri: @create_uri, body: @params, opts: @default_opts)
42
70
  .returns(response)
43
71
  assert_raises OneSignal::OneSignalError do
44
72
  OneSignal::Notification.create(params: @params)
@@ -48,7 +76,7 @@ class NotificationTest < MiniTest::Test
48
76
  def test_create_raises_no_recipients_error
49
77
  response = mock_response_no_recipients
50
78
  OneSignal::OneSignal.expects(:send_post_request)
51
- .with(uri: @create_uri, body: @params)
79
+ .with(uri: @create_uri, body: @params, opts: @default_opts)
52
80
  .returns(response)
53
81
  assert_raises OneSignal::NoRecipientsError do
54
82
  OneSignal::Notification.create(params: @params)
@@ -58,27 +86,55 @@ class NotificationTest < MiniTest::Test
58
86
  def test_create
59
87
  response = mock_response_ok
60
88
  OneSignal::OneSignal.expects(:send_post_request)
61
- .with(uri: @create_uri, body: @params)
89
+ .with(uri: @create_uri, body: @params, opts: @default_opts)
62
90
  .returns(response)
63
91
  assert_equal response, OneSignal::Notification.create(params: @params)
64
92
  end
65
93
 
94
+ def test_create_with_auth_key
95
+ response = mock_response_ok
96
+ OneSignal::OneSignal.expects(:send_post_request)
97
+ .with(uri: @create_uri, body: @params, opts: @opts)
98
+ .returns(response)
99
+ assert_equal response, OneSignal::Notification.create(params: @params, opts: @opts)
100
+ end
101
+
66
102
  def test_update
67
103
  response = mock_response_ok
68
104
  OneSignal::OneSignal.expects(:send_put_request)
69
- .with(uri: @update_uri, body: @params)
105
+ .with(uri: @update_uri, body: @params, opts: @default_opts)
70
106
  .returns(response)
71
107
  assert_equal response, OneSignal::Notification.update(id: @notification_id,
72
108
  params: @params)
73
109
  end
74
110
 
111
+ def test_update_with_auth_key
112
+ response = mock_response_ok
113
+ OneSignal::OneSignal.expects(:send_put_request)
114
+ .with(uri: @update_uri, body: @params, opts: @opts)
115
+ .returns(response)
116
+ assert_equal response, OneSignal::Notification.update(id: @notification_id,
117
+ params: @params,
118
+ opts: @opts)
119
+ end
120
+
75
121
  def test_delete
76
122
  response = mock_response_ok
77
123
  OneSignal::OneSignal.expects(:send_delete_request)
78
- .with(uri: @delete_uri, params: @params)
124
+ .with(uri: @delete_uri, params: @params, opts: @default_opts)
79
125
  .returns(response)
80
126
  assert_equal response, OneSignal::Notification.delete(id: @notification_id,
81
127
  params: @params)
82
128
  end
83
129
 
130
+ def test_delete_with_auth_key
131
+ response = mock_response_ok
132
+ OneSignal::OneSignal.expects(:send_delete_request)
133
+ .with(uri: @delete_uri, params: @params, opts: @opts)
134
+ .returns(response)
135
+ assert_equal response, OneSignal::Notification.delete(id: @notification_id,
136
+ params: @params,
137
+ opts: @opts)
138
+ end
139
+
84
140
  end
@@ -6,6 +6,7 @@ class OneSignalTest < MiniTest::Test
6
6
  @api_key = "fake_api_123"
7
7
  @body = "fake body"
8
8
  @params = {foo: 'bar', widget: 'acme'}
9
+ @opts = {auth_key: @api_key}
9
10
  @uri = URI.parse("https://www.example.com/foo/bar")
10
11
  @default_timeout = 30
11
12
  end
@@ -16,11 +17,11 @@ class OneSignalTest < MiniTest::Test
16
17
  OneSignal::OneSignal.read_timeout = @default_timeout
17
18
  end
18
19
 
19
- def build_mock_request(body: nil)
20
+ def build_mock_request(body: nil, auth_key: nil)
20
21
  request = mock()
21
22
  request.expects(:body=).with(body.to_json) unless body.nil?
22
23
  request.expects(:add_field).with("Content-Type", "application/json")
23
- request.expects(:add_field).with("Authorization", "Basic #{@api_key}")
24
+ request.expects(:add_field).with("Authorization", "Basic #{auth_key}")
24
25
  return request
25
26
  end
26
27
 
@@ -67,7 +68,7 @@ class OneSignalTest < MiniTest::Test
67
68
 
68
69
  def test_send_post_request
69
70
  # test request creation
70
- request = build_mock_request(body: @body)
71
+ request = build_mock_request(body: @body, auth_key: @api_key)
71
72
  Net::HTTP::Post.expects(:new).with(@uri.request_uri).returns(request)
72
73
 
73
74
  # test http object creation
@@ -78,8 +79,9 @@ class OneSignalTest < MiniTest::Test
78
79
  response = mock()
79
80
  http.expects(:request).with(request).returns(response)
80
81
 
81
- OneSignal::OneSignal.api_key = @api_key
82
- assert_equal response, OneSignal::OneSignal.send_post_request(uri: @uri, body: @body)
82
+ assert_equal response, OneSignal::OneSignal.send_post_request(uri: @uri,
83
+ body: @body,
84
+ opts: @opts)
83
85
  end
84
86
 
85
87
  def test_send_delete_request
@@ -87,7 +89,7 @@ class OneSignalTest < MiniTest::Test
87
89
  expected_uri.query = URI.encode_www_form(@params)
88
90
 
89
91
  # test request creation
90
- request = build_mock_request
92
+ request = build_mock_request(auth_key: @api_key)
91
93
  Net::HTTP::Delete.expects(:new).with(expected_uri.request_uri).returns(request)
92
94
 
93
95
  # test http object creation
@@ -98,14 +100,14 @@ class OneSignalTest < MiniTest::Test
98
100
  response = mock()
99
101
  http.expects(:request).with(request).returns(response)
100
102
 
101
- OneSignal::OneSignal.api_key = @api_key
102
103
  assert_equal response, OneSignal::OneSignal.send_delete_request(uri: @uri,
103
- params: @params)
104
+ params: @params,
105
+ opts: @opts)
104
106
  end
105
107
 
106
108
  def test_send_put_request
107
109
  # test request creation
108
- request = build_mock_request(body: @body)
110
+ request = build_mock_request(body: @body, auth_key: @api_key)
109
111
  Net::HTTP::Put.expects(:new).with(@uri.request_uri).returns(request)
110
112
 
111
113
  # test http object creation
@@ -116,8 +118,9 @@ class OneSignalTest < MiniTest::Test
116
118
  response = mock()
117
119
  http.expects(:request).with(request).returns(response)
118
120
 
119
- OneSignal::OneSignal.api_key = @api_key
120
- assert_equal response, OneSignal::OneSignal.send_put_request(uri: @uri, body: @body)
121
+ assert_equal response, OneSignal::OneSignal.send_put_request(uri: @uri,
122
+ body: @body,
123
+ opts: @opts)
121
124
  end
122
125
 
123
126
  def test_send_get_request
@@ -125,7 +128,7 @@ class OneSignalTest < MiniTest::Test
125
128
  expected_uri.query = URI.encode_www_form(@params)
126
129
 
127
130
  # test request creation
128
- request = build_mock_request
131
+ request = build_mock_request(auth_key: @api_key)
129
132
  Net::HTTP::Get.expects(:new).with(expected_uri.request_uri).returns(request)
130
133
 
131
134
  # test http object creation
@@ -136,9 +139,9 @@ class OneSignalTest < MiniTest::Test
136
139
  response = mock()
137
140
  http.expects(:request).with(request).returns(response)
138
141
 
139
- OneSignal::OneSignal.api_key = @api_key
140
142
  assert_equal response, OneSignal::OneSignal.send_get_request(uri: @uri,
141
- params: @params)
143
+ params: @params,
144
+ opts: @opts)
142
145
  end
143
146
 
144
147
  end
@@ -18,14 +18,23 @@ class PlayerTest < MiniTest::Test
18
18
  foo: "bar",
19
19
  widget: "acme"
20
20
  }
21
- @api_key = "fake api key"
21
+ @api_key = "fake api key 123"
22
+ @api_key_2 = "fake api key 456"
23
+ @opts = {
24
+ key1: "value1",
25
+ key2: "vaue2",
26
+ auth_key: @api_key_2
27
+ }
28
+ @default_opts = {
29
+ auth_key: @api_key
30
+ }
22
31
  OneSignal::OneSignal.api_key = @api_key
23
32
  end
24
33
 
25
34
  def test_create_raises_error
26
35
  response = mock_response_ko
27
36
  OneSignal::OneSignal.expects(:send_post_request)
28
- .with(uri: @create_uri, body: @params)
37
+ .with(uri: @create_uri, body: @params, opts: @default_opts)
29
38
  .returns(response)
30
39
  assert_raises OneSignal::OneSignalError do
31
40
  OneSignal::Player.create(params: @params)
@@ -35,39 +44,71 @@ class PlayerTest < MiniTest::Test
35
44
  def test_csv_export
36
45
  response = mock_response_ok
37
46
  OneSignal::OneSignal.expects(:send_post_request)
38
- .with(uri: @csv_export_uri, body: @params)
47
+ .with(uri: @csv_export_uri, body: @params, opts: @default_opts)
39
48
  .returns(response)
40
49
  assert_equal response, OneSignal::Player.csv_export(params: @params)
41
50
  end
42
51
 
52
+ def test_csv_export_with_auth_key
53
+ response = mock_response_ok
54
+ OneSignal::OneSignal.expects(:send_post_request)
55
+ .with(uri: @csv_export_uri, body: @params, opts: @opts)
56
+ .returns(response)
57
+ assert_equal response, OneSignal::Player.csv_export(params: @params, opts: @opts)
58
+ end
59
+
43
60
  def test_all
44
61
  response = mock_response_ok
45
62
  OneSignal::OneSignal.expects(:send_get_request)
46
- .with(uri: @all_uri, params: @params)
63
+ .with(uri: @all_uri, params: @params, opts: @default_opts)
47
64
  .returns(response)
48
65
  assert_equal response, OneSignal::Player.all(params: @params)
49
66
  end
50
67
 
68
+ def test_all_with_auth_key
69
+ response = mock_response_ok
70
+ OneSignal::OneSignal.expects(:send_get_request)
71
+ .with(uri: @all_uri, params: @params, opts: @opts)
72
+ .returns(response)
73
+ assert_equal response, OneSignal::Player.all(params: @params, opts: @opts)
74
+ end
75
+
51
76
  def test_get
52
77
  response = mock_response_ok
53
78
  OneSignal::OneSignal.expects(:send_get_request)
54
- .with(uri: @get_uri, params: nil)
79
+ .with(uri: @get_uri, params: nil, opts: @default_opts)
55
80
  .returns(response)
56
81
  assert_equal response, OneSignal::Player.get(id: @player_id)
57
82
  end
58
83
 
84
+ def test_get_with_auth_key
85
+ response = mock_response_ok
86
+ OneSignal::OneSignal.expects(:send_get_request)
87
+ .with(uri: @get_uri, params: nil, opts: @opts)
88
+ .returns(response)
89
+ assert_equal response, OneSignal::Player.get(id: @player_id, opts: @opts)
90
+ end
91
+
59
92
  def test_create
60
93
  response = mock_response_ok
61
94
  OneSignal::OneSignal.expects(:send_post_request)
62
- .with(uri: @create_uri, body: @params)
95
+ .with(uri: @create_uri, body: @params, opts: @default_opts)
63
96
  .returns(response)
64
97
  assert_equal response, OneSignal::Player.create(params: @params)
65
98
  end
66
99
 
100
+ def test_create_with_auth_key
101
+ response = mock_response_ok
102
+ OneSignal::OneSignal.expects(:send_post_request)
103
+ .with(uri: @create_uri, body: @params, opts: @opts)
104
+ .returns(response)
105
+ assert_equal response, OneSignal::Player.create(params: @params, opts: @opts)
106
+ end
107
+
67
108
  def test_update_raises_error
68
109
  response = mock_response_ko
69
110
  OneSignal::OneSignal.expects(:send_put_request)
70
- .with(uri: @update_uri, body: @params)
111
+ .with(uri: @update_uri, body: @params, opts: @default_opts)
71
112
  .returns(response)
72
113
  assert_raises OneSignal::OneSignalError do
73
114
  OneSignal::Player.update(id: @player_id, params: @params)
@@ -77,36 +118,80 @@ class PlayerTest < MiniTest::Test
77
118
  def test_update
78
119
  response = mock_response_ok
79
120
  OneSignal::OneSignal.expects(:send_put_request)
80
- .with(uri: @update_uri, body: @params)
121
+ .with(uri: @update_uri, body: @params, opts: @default_opts)
81
122
  .returns(response)
82
123
  assert_equal response, OneSignal::Player.update(id: @player_id, params: @params)
83
124
  end
84
125
 
126
+ def test_update_with_auth_key
127
+ response = mock_response_ok
128
+ OneSignal::OneSignal.expects(:send_put_request)
129
+ .with(uri: @update_uri, body: @params, opts: @opts)
130
+ .returns(response)
131
+ assert_equal response, OneSignal::Player.update(id: @player_id,
132
+ params: @params,
133
+ opts: @opts)
134
+ end
135
+
85
136
  def test_create_session
86
137
  response = mock_response_ok
87
138
  OneSignal::OneSignal.expects(:send_post_request)
88
- .with(uri: @create_session_uri, body: @params)
139
+ .with(uri: @create_session_uri, body: @params, opts: @default_opts)
89
140
  .returns(response)
90
141
  assert_equal response, OneSignal::Player.create_session(id: @player_id,
91
142
  params: @params)
92
143
  end
93
144
 
145
+ def test_create_session_with_auth_key
146
+ response = mock_response_ok
147
+ OneSignal::OneSignal.expects(:send_post_request)
148
+ .with(uri: @create_session_uri, body: @params, opts: @opts)
149
+ .returns(response)
150
+ assert_equal response, OneSignal::Player.create_session(id: @player_id,
151
+ params: @params,
152
+ opts: @opts)
153
+ end
154
+
94
155
  def test_create_purchase
95
156
  response = mock_response_ok
96
157
  OneSignal::OneSignal.expects(:send_post_request)
97
- .with(uri: @create_purchase_uri, body: @params)
158
+ .with(uri: @create_purchase_uri,
159
+ body: @params,
160
+ opts: @default_opts)
98
161
  .returns(response)
99
162
  assert_equal response, OneSignal::Player.create_purchase(id: @player_id,
100
163
  params: @params)
101
164
  end
102
165
 
166
+ def test_create_purchase_with_auth_key
167
+ response = mock_response_ok
168
+ OneSignal::OneSignal.expects(:send_post_request)
169
+ .with(uri: @create_purchase_uri,
170
+ body: @params,
171
+ opts: @opts)
172
+ .returns(response)
173
+ assert_equal response, OneSignal::Player.create_purchase(id: @player_id,
174
+ params: @params,
175
+ opts: @opts)
176
+ end
177
+
103
178
  def test_create_focus
104
179
  response = mock_response_ok
105
180
  OneSignal::OneSignal.expects(:send_post_request)
106
- .with(uri: @create_focus_uri, body: @params)
181
+ .with(uri: @create_focus_uri, body: @params, opts: @default_opts)
107
182
  .returns(response)
108
183
  assert_equal response, OneSignal::Player.create_focus(id: @player_id,
109
184
  params: @params)
110
185
  end
111
186
 
187
+ def test_create_focus_with_auth_key
188
+ response = mock_response_ok
189
+ OneSignal::OneSignal.expects(:send_post_request)
190
+ .with(uri: @create_focus_uri, body: @params, opts: @opts)
191
+ .returns(response)
192
+ assert_equal response, OneSignal::Player.create_focus(id: @player_id,
193
+ params: @params,
194
+ opts: @opts)
195
+ end
196
+
112
197
  end
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: 1.1.0
4
+ version: 1.2.0
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-04-08 00:00:00.000000000 Z
11
+ date: 2016-04-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake