one_signal 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ t.pattern = "test/**/*_test.rb"
6
+ end
7
+
8
+ desc "Run tests"
9
+ task :default => :test
data/example.rb ADDED
@@ -0,0 +1,103 @@
1
+ require 'one_signal'
2
+ require 'dotenv'
3
+
4
+ Dotenv.load
5
+ api_key = ENV['ONESIGNAL_API_KEY']
6
+ @app_id = ENV['ONESIGNAL_APP_ID']
7
+ @device_token = "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabc3"
8
+
9
+ OneSignal::OneSignal.api_key = api_key
10
+
11
+ def create_player
12
+ params = {
13
+ app_id: @app_id,
14
+ device_type: 0,
15
+ identifier: @device_token,
16
+ tags: {
17
+ user_id: 'test2',
18
+ device_type: 'chrome'
19
+ }
20
+ }
21
+
22
+ begin
23
+ response = OneSignal::Player.create(params: params)
24
+ puts "code : #{response.code}"
25
+ puts "message : #{response.message}"
26
+ puts "body : #{response.body}"
27
+ puts "id : #{JSON.parse(response.body).class}"
28
+ return JSON.parse(response.body)["id"]
29
+ rescue OneSignal::OneSignalError => e
30
+ puts "-- message : #{e.message}"
31
+ puts "-- status : #{e.http_status}"
32
+ puts "-- body : #{e.http_body}"
33
+ end
34
+ end
35
+
36
+ def update_player(id:)
37
+ params = {
38
+ app_id: @app_id,
39
+ device_type: 0,
40
+ identifier: @device_token,
41
+ tags: {
42
+ user_id: 'test2updated',
43
+ device_type: 'chrome'
44
+ }
45
+ }
46
+
47
+ begin
48
+ response = OneSignal::Player.update(id: id, params: params)
49
+ puts "code : #{response.code}"
50
+ puts "message : #{response.message}"
51
+ puts "body : #{response.body}"
52
+ puts "id : #{JSON.parse(response.body).class}"
53
+ rescue OneSignal::OneSignalError => e
54
+ puts "-- message : #{e.message}"
55
+ puts "-- status : #{e.http_status}"
56
+ puts "-- body : #{e.http_body}"
57
+ end
58
+ end
59
+
60
+ def notify
61
+ params = {
62
+ app_id: @app_id,
63
+ contents: {
64
+ en: 'hey buddy'
65
+ },
66
+ ios_badgeType: 'None',
67
+ ios_badgeCount: 1,
68
+ tags: [
69
+ {
70
+ key: 'user_id',
71
+ relation: '=',
72
+ value: "unknown"
73
+ },
74
+ { operator: 'AND' },
75
+ {
76
+ key: 'device_type',
77
+ relation: '=',
78
+ value: 'ios'
79
+ }
80
+ ]
81
+ }
82
+
83
+ begin
84
+ response = OneSignal::Notification.create(params: params)
85
+ puts "code : #{response.code}"
86
+ puts "message : #{response.message}"
87
+ puts "body : #{response.body}"
88
+ rescue OneSignal::NoRecipientsError => e
89
+ puts "--- NoRecipientsError: #{e.inspect}"
90
+ puts "-- message : #{e.message}"
91
+ puts "-- status : #{e.http_status}"
92
+ puts "-- body : #{e.http_body}"
93
+ rescue OneSignal::OneSignalError => e
94
+ puts "--- OneSignalError : #{e.inspect}"
95
+ puts "-- message : #{e.message}"
96
+ puts "-- status : #{e.http_status}"
97
+ puts "-- body : #{e.http_body}"
98
+ end
99
+ end
100
+
101
+ # player_id = create_player
102
+ # update_player(id: player_id)
103
+ notify
@@ -0,0 +1,4 @@
1
+ module OneSignal
2
+ class NoRecipientsError < OneSignalError
3
+ end
4
+ end
@@ -0,0 +1,17 @@
1
+ module OneSignal
2
+ class OneSignalError < StandardError
3
+ attr_reader :message
4
+ attr_reader :http_status
5
+ attr_reader :http_body
6
+
7
+ def initialize(message: nil, http_status: nil, http_body: nil)
8
+ @message = message
9
+ @http_status = http_status
10
+ @http_body = http_body
11
+ @message += " - http status : #{@http_status}" unless @http_status.nil?
12
+ @message += " - http body : #{@http_body}" unless @http_body.nil?
13
+ super(@message)
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,51 @@
1
+ module OneSignal
2
+
3
+ class Notification < OneSignal
4
+
5
+ def self.create(params: {})
6
+ uri_string = @@base_uri
7
+ uri_string += "/notifications"
8
+ uri = URI.parse(uri_string)
9
+
10
+ response = send_post_request(uri: uri, body: params)
11
+
12
+ if response.code != '200'
13
+ handle_error(uri: uri, params: params, response: response)
14
+ end
15
+
16
+ return response
17
+ end
18
+
19
+ private
20
+
21
+ def self.handle_error(uri:, params:, response:)
22
+ msg = "Create Notification error - uri: #{uri} params: #{params}"
23
+ if is_no_recipients_error(response: response)
24
+ raise NoRecipientsError.new(message: msg,
25
+ http_status: response.code,
26
+ http_body: response.body)
27
+ else
28
+ raise OneSignalError.new(message: msg,
29
+ http_status: response.code,
30
+ http_body: response.body)
31
+ end
32
+ end
33
+
34
+ def self.is_no_recipients_error(response:)
35
+ return false if response.code != '400' ||
36
+ response.body.nil?
37
+
38
+ body = JSON.parse(response.body)
39
+ return false unless body['errors'] &&
40
+ body['errors'].is_a?(Array) &&
41
+ !body['errors'].empty?
42
+
43
+ expected_error_msg = "error: 0 recipients."
44
+ return false unless body['errors'][0].strip.downcase == expected_error_msg
45
+
46
+ return true
47
+ end
48
+
49
+ end
50
+
51
+ end
@@ -0,0 +1,86 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'json'
4
+
5
+ module OneSignal
6
+
7
+ class OneSignal
8
+ @@base_uri = "https://onesignal.com/api/v1"
9
+ @@api_key = nil
10
+ @@open_timeout = 30
11
+ @@read_timeout = 30
12
+
13
+ def self.api_key=(new_api_key)
14
+ @@api_key = new_api_key
15
+ end
16
+
17
+ def self.api_key
18
+ @@api_key
19
+ end
20
+
21
+ def self.open_timeout=(new_timeout)
22
+ @@open_timeout = new_timeout
23
+ end
24
+
25
+ def self.open_timeout
26
+ @@open_timeout
27
+ end
28
+
29
+ def self.read_timeout=(new_timeout)
30
+ @@read_timeout = new_timeout
31
+ end
32
+
33
+ def self.read_timeout
34
+ @@read_timeout
35
+ end
36
+
37
+ def self.http_object(uri:)
38
+ http = Net::HTTP.new(uri.host, uri.port)
39
+ http.use_ssl = uri.scheme == 'https'
40
+ http.open_timeout = @@open_timeout
41
+ http.read_timeout = @@read_timeout
42
+ return http
43
+ end
44
+
45
+ def self.send_post_request(uri:, body:)
46
+ ensure_api_key
47
+
48
+ request = Net::HTTP::Post.new(uri.request_uri)
49
+ request.body = body.to_json
50
+ request = request_with_headers(request: request)
51
+
52
+ http = http_object(uri: uri)
53
+
54
+ response = http.request(request)
55
+ end
56
+
57
+ def self.send_put_request(uri:, body:)
58
+ ensure_api_key
59
+
60
+ request = Net::HTTP::Put.new(uri.request_uri)
61
+ request.body = body.to_json
62
+ request = request_with_headers(request: request)
63
+
64
+ http = http_object(uri: uri)
65
+
66
+ response = http.request(request)
67
+ end
68
+
69
+ private
70
+
71
+ def self.ensure_api_key
72
+ unless @@api_key && !@@api_key.strip.empty?
73
+ msg = "No API key provided"
74
+ raise OneSignalError.new(message: msg)
75
+ end
76
+ end
77
+
78
+ def self.request_with_headers(request:)
79
+ request.add_field("Content-Type", "application/json")
80
+ request.add_field("Authorization", "Basic #{@@api_key}")
81
+ return request
82
+ end
83
+
84
+ end
85
+
86
+ end
@@ -0,0 +1,51 @@
1
+ module OneSignal
2
+
3
+ class Player < OneSignal
4
+
5
+ def self.create(params: {})
6
+ uri_string = @@base_uri
7
+ uri_string += "/players"
8
+ uri = URI.parse(uri_string)
9
+
10
+ response = send_post_request(uri: uri, body: params)
11
+
12
+ ensure_http_status(response: response,
13
+ status: '200',
14
+ method_name: 'Create',
15
+ uri: uri,
16
+ params: params)
17
+
18
+ return response
19
+ end
20
+
21
+ def self.update(id:, params: {})
22
+ uri_string = @@base_uri
23
+ uri_string += "/players"
24
+ uri_string += "/#{id}"
25
+ uri = URI.parse(uri_string)
26
+
27
+ response = send_put_request(uri: uri, body: params)
28
+
29
+ ensure_http_status(response: response,
30
+ status: '200',
31
+ method_name: 'Update',
32
+ uri: uri,
33
+ params: params)
34
+
35
+ return response
36
+ end
37
+
38
+ private
39
+
40
+ def self.ensure_http_status(response:, status:, method_name:, uri:, params:)
41
+ if response.code != status.to_s
42
+ msg = "#{method_name} Player error - uri: #{uri} params: #{params}"
43
+ raise OneSignalError.new(message: msg,
44
+ http_status: response.code,
45
+ http_body: response.body)
46
+ end
47
+ end
48
+
49
+ end
50
+
51
+ end
@@ -0,0 +1,3 @@
1
+ module OneSignal
2
+ VERSION = "0.0.6"
3
+ end
data/lib/one_signal.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'one_signal/version'
2
+ require 'one_signal/one_signal'
3
+ require 'one_signal/player'
4
+ require 'one_signal/notification'
5
+
6
+ # Errors
7
+ require 'one_signal/errors/one_signal_error'
8
+ require 'one_signal/errors/no_recipients_error'
@@ -0,0 +1,18 @@
1
+ require File.expand_path('lib/one_signal/version')
2
+
3
+ Gem::Specification.new do |s|
4
+ s.add_runtime_dependency 'json', '~> 1.8'
5
+ s.add_development_dependency 'rake', '~> 10.4'
6
+ s.add_development_dependency 'mocha', '~> 1.1'
7
+ s.add_development_dependency 'dotenv', '~> 2.0'
8
+ s.name = 'one_signal'
9
+ s.version = OneSignal::VERSION
10
+ s.summary = "A library which implements the OneSignal API"
11
+ s.description = "A library which implements the OneSignal API."
12
+ s.authors = ["Thomas Balthazar"]
13
+ s.email = 'thomas@balthazar.info'
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.homepage = 'https://github.com/tbalthazar/onesignal-ruby'
17
+ s.license = 'MIT'
18
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,24 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require 'minitest/autorun'
3
+ require 'mocha/mini_test'
4
+ require 'one_signal'
5
+
6
+ def mock_response_ok
7
+ response = mock()
8
+ response.expects(:code).returns("200")
9
+ return response
10
+ end
11
+
12
+ def mock_response_ko
13
+ response = mock()
14
+ response.expects(:code).returns("400").at_least_once
15
+ response.expects(:body).at_least_once
16
+ return response
17
+ end
18
+
19
+ def mock_response_no_recipients
20
+ response = mock()
21
+ response.expects(:code).returns("400").at_least_once
22
+ response.expects(:body).returns('{"errors":[" Error: 0 Recipients."]}').at_least_once
23
+ return response
24
+ end
@@ -0,0 +1,43 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class NotificationTest < MiniTest::Test
4
+
5
+ def setup
6
+ @uri = URI.parse("https://onesignal.com/api/v1/notifications")
7
+ @create_params = {
8
+ foo: "bar",
9
+ widget: "acme"
10
+ }
11
+ @api_key = "fake api key"
12
+ OneSignal::OneSignal.api_key = @api_key
13
+ end
14
+
15
+ def test_create_raises_onesignal_error
16
+ response = mock_response_ko
17
+ OneSignal::OneSignal.expects(:send_post_request)
18
+ .with(uri: @uri, body: @create_params)
19
+ .returns(response)
20
+ assert_raises OneSignal::OneSignalError do
21
+ OneSignal::Notification.create(params: @create_params)
22
+ end
23
+ end
24
+
25
+ def test_create_raises_no_recipients_error
26
+ response = mock_response_no_recipients
27
+ OneSignal::OneSignal.expects(:send_post_request)
28
+ .with(uri: @uri, body: @create_params)
29
+ .returns(response)
30
+ assert_raises OneSignal::NoRecipientsError do
31
+ OneSignal::Notification.create(params: @create_params)
32
+ end
33
+ end
34
+
35
+ def test_create
36
+ response = mock_response_ok
37
+ OneSignal::OneSignal.expects(:send_post_request)
38
+ .with(uri: @uri, body: @create_params)
39
+ .returns(response)
40
+ assert_equal response, OneSignal::Notification.create(params: @create_params)
41
+ end
42
+
43
+ end
@@ -0,0 +1,123 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class OneSignalTest < MiniTest::Test
4
+
5
+ def setup
6
+ @api_key = "fake_api_123"
7
+ @body = "fake body"
8
+ @uri = URI.parse("https://www.example.com/foo/bar")
9
+ @default_timeout = 30
10
+ end
11
+
12
+ def teardown
13
+ OneSignal::OneSignal.api_key = nil
14
+ OneSignal::OneSignal.open_timeout = @default_timeout
15
+ OneSignal::OneSignal.read_timeout = @default_timeout
16
+ end
17
+
18
+ def build_mock_request
19
+ request = mock()
20
+ request.expects(:body=).with(@body.to_json)
21
+ request.expects(:add_field).with("Content-Type", "application/json")
22
+ request.expects(:add_field).with("Authorization", "Basic #{@api_key}")
23
+ return request
24
+ end
25
+
26
+ def build_mock_http_object
27
+ use_ssl = @uri.scheme == 'https'
28
+ http = mock()
29
+ http.expects(:use_ssl=).with(use_ssl)
30
+ http.expects(:open_timeout=)
31
+ http.expects(:read_timeout=)
32
+ return http
33
+ end
34
+
35
+ def test_building_request_with_nil_api_key_raises_error
36
+ OneSignal::OneSignal.api_key = nil
37
+
38
+ assert_raises OneSignal::OneSignalError do
39
+ OneSignal::OneSignal.send_post_request(uri: @uri, body: @body)
40
+ end
41
+ assert_raises OneSignal::OneSignalError do
42
+ OneSignal::OneSignal.send_put_request(uri: @uri, body: @body)
43
+ end
44
+ end
45
+
46
+ def test_building_request_with_empty_api_key_raises_error
47
+ OneSignal::OneSignal.api_key = " "
48
+
49
+ assert_raises OneSignal::OneSignalError do
50
+ OneSignal::OneSignal.send_post_request(uri: @uri, body: @body)
51
+ end
52
+ assert_raises OneSignal::OneSignalError do
53
+ OneSignal::OneSignal.send_put_request(uri: @uri, body: @body)
54
+ end
55
+ end
56
+
57
+ def test_default_timeout
58
+ assert_equal @default_timeout, OneSignal::OneSignal.open_timeout
59
+ assert_equal @default_timeout, OneSignal::OneSignal.read_timeout
60
+ end
61
+
62
+ def test_setting_timeout
63
+ open_timeout = OneSignal::OneSignal.open_timeout + 1
64
+ read_timeout = OneSignal::OneSignal.read_timeout + 2
65
+ OneSignal::OneSignal.open_timeout = open_timeout
66
+ OneSignal::OneSignal.read_timeout = read_timeout
67
+
68
+ assert_equal open_timeout, OneSignal::OneSignal.open_timeout
69
+ assert_equal read_timeout, OneSignal::OneSignal.read_timeout
70
+ end
71
+
72
+ def test_http_object
73
+ # test default timeout values
74
+ http_object = OneSignal::OneSignal.http_object(uri: @uri)
75
+ assert_equal @default_timeout, http_object.open_timeout
76
+ assert_equal @default_timeout, http_object.read_timeout
77
+
78
+ # change timeout values
79
+ open_timeout = OneSignal::OneSignal.open_timeout + 1
80
+ read_timeout = OneSignal::OneSignal.read_timeout + 2
81
+ OneSignal::OneSignal.open_timeout = open_timeout
82
+ OneSignal::OneSignal.read_timeout = read_timeout
83
+ http_object = OneSignal::OneSignal.http_object(uri: @uri)
84
+
85
+ assert_equal open_timeout, http_object.open_timeout
86
+ assert_equal read_timeout, http_object.read_timeout
87
+ end
88
+
89
+ def test_send_post_request
90
+ # test request creation
91
+ request = build_mock_request
92
+ Net::HTTP::Post.expects(:new).with(@uri.request_uri).returns(request)
93
+
94
+ # test http object creation
95
+ http = build_mock_http_object
96
+ Net::HTTP.expects(:new).with(@uri.host, @uri.port).returns(http)
97
+
98
+ # test send request
99
+ response = mock()
100
+ http.expects(:request).with(request).returns(response)
101
+
102
+ OneSignal::OneSignal.api_key = @api_key
103
+ assert_equal response, OneSignal::OneSignal.send_post_request(uri: @uri, body: @body)
104
+ end
105
+
106
+ def test_send_put_request
107
+ # test request creation
108
+ request = build_mock_request
109
+ Net::HTTP::Put.expects(:new).with(@uri.request_uri).returns(request)
110
+
111
+ # test http object creation
112
+ http = build_mock_http_object
113
+ Net::HTTP.expects(:new).with(@uri.host, @uri.port).returns(http)
114
+
115
+ # test send request
116
+ response = mock()
117
+ http.expects(:request).with(request).returns(response)
118
+
119
+ OneSignal::OneSignal.api_key = @api_key
120
+ assert_equal response, OneSignal::OneSignal.send_put_request(uri: @uri, body: @body)
121
+ end
122
+
123
+ end
@@ -0,0 +1,56 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class PlayerTest < MiniTest::Test
4
+
5
+ def setup
6
+ base_url = "https://onesignal.com/api/v1/players"
7
+ @create_uri = URI.parse(base_url)
8
+
9
+ @player_id = "fake-id-123"
10
+ @update_uri = URI.parse(base_url + "/#{@player_id}")
11
+
12
+ @params = {
13
+ foo: "bar",
14
+ widget: "acme"
15
+ }
16
+ @api_key = "fake api key"
17
+ OneSignal::OneSignal.api_key = @api_key
18
+ end
19
+
20
+ def test_create_raises_error
21
+ response = mock_response_ko
22
+ OneSignal::OneSignal.expects(:send_post_request)
23
+ .with(uri: @create_uri, body: @params)
24
+ .returns(response)
25
+ assert_raises OneSignal::OneSignalError do
26
+ OneSignal::Player.create(params: @params)
27
+ end
28
+ end
29
+
30
+ def test_create
31
+ response = mock_response_ok
32
+ OneSignal::OneSignal.expects(:send_post_request)
33
+ .with(uri: @create_uri, body: @params)
34
+ .returns(response)
35
+ assert_equal response, OneSignal::Player.create(params: @params)
36
+ end
37
+
38
+ def test_update_raises_error
39
+ response = mock_response_ko
40
+ OneSignal::OneSignal.expects(:send_put_request)
41
+ .with(uri: @update_uri, body: @params)
42
+ .returns(response)
43
+ assert_raises OneSignal::OneSignalError do
44
+ OneSignal::Player.update(id: @player_id, params: @params)
45
+ end
46
+ end
47
+
48
+ def test_update
49
+ response = mock_response_ok
50
+ OneSignal::OneSignal.expects(:send_put_request)
51
+ .with(uri: @update_uri, body: @params)
52
+ .returns(response)
53
+ assert_equal response, OneSignal::Player.update(id: @player_id, params: @params)
54
+ end
55
+
56
+ end