pushradar 2.0.2 → 3.1.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
- SHA1:
3
- metadata.gz: e658126d203b053e9374f48359144207555b6563
4
- data.tar.gz: cc90a66d8a7725c13f37141df80435e3fc7e7398
2
+ SHA256:
3
+ metadata.gz: 4cb5a3926082d52f847bbb57e800e4c964cb9ea35c005e67c3df6246ceb17a07
4
+ data.tar.gz: a942aaea6376412ca2addd8d8e760df9919bb99ffe5c4de7164c148c5b306a57
5
5
  SHA512:
6
- metadata.gz: cdedffae714725bee4d4a3ba7874f3d2975f598f9277a22842fbc1ec364dd524efdcd363291a9ecd9aa04aaa3b92b1dd3a32881c12796203dff5426075b01a4f
7
- data.tar.gz: 6035678595a7db5347326ad95161110b3f7c7abaa9b447f4ead82911a702dbbba4d169ac5ec462a6e4f4b4cd84bd706e99f5b128e4acd531b441d4d4896e7281
6
+ metadata.gz: 99a48b950b8d6d196bbb621c3fb9c7d7a3f08fd9fdc80f8e1edbe7b100358743c48eb68314ebc237e8e64bbdc239391f71992b4e251578200463072d088f06cf
7
+ data.tar.gz: b7b05961ebc5e0492b55430fc223954fa497b3186eef2a15214da55920acc9af346a654d15096ac8d190749d860860f87c93c7d5e09c8584910ef5882b292dd7
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gemtest ADDED
File without changes
data/.gitignore CHANGED
@@ -1,10 +1,35 @@
1
- /.bundle/
2
- /.yardoc
3
- /Gemfile.lock
4
- /_yardoc/
5
- /coverage/
6
- /doc/
7
- /pkg/
8
- /spec/reports/
9
- /tmp/
10
- todo
1
+ .DS_Store
2
+ *.tmproj
3
+ tmtags
4
+ *~
5
+ \#*
6
+ .\#*
7
+ *.swp
8
+ coverage
9
+ rdoc
10
+ pkg
11
+ .yardoc
12
+ Gemfile.lock
13
+ .bundle
14
+
15
+ /node_modules
16
+ /public/hot
17
+ /public/storage
18
+ /storage/*.key
19
+ /vendor
20
+ .env
21
+ .env.backup
22
+ .env.production
23
+ .phpunit.result.cache
24
+ docker-compose.override.yml
25
+ Homestead.json
26
+ Homestead.yaml
27
+ npm-debug.log
28
+ yarn-error.log
29
+ composer.lock
30
+ phpunit.xml
31
+ tests/config.php
32
+ vendor
33
+ /src/_notUsed
34
+ /_pr-test
35
+ /_notUsed
data/.idea/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ # Default ignored files
2
+ /shelf/
3
+ /workspace.xml
4
+ # Datasource local storage ignored files
5
+ /dataSources/
6
+ /dataSources.local.xml
7
+ # Editor-based HTTP Client requests
8
+ /httpRequests/
@@ -1,6 +1,6 @@
1
- <component name="InspectionProjectProfileManager">
2
- <profile version="1.0">
3
- <option name="myName" value="Project Default" />
4
- <inspection_tool class="Rubocop" enabled="false" level="WARNING" enabled_by_default="false" />
5
- </profile>
1
+ <component name="InspectionProjectProfileManager">
2
+ <profile version="1.0">
3
+ <option name="myName" value="Project Default" />
4
+ <inspection_tool class="Rubocop" enabled="false" level="WARNING" enabled_by_default="false" />
5
+ </profile>
6
6
  </component>
data/.idea/misc.xml CHANGED
@@ -1,4 +1,4 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="ProjectRootManager" version="2" project-jdk-name="ruby-2.4.1-p111" project-jdk-type="RUBY_SDK" />
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectRootManager" version="2" project-jdk-name="ruby-2.6.3-p62" project-jdk-type="RUBY_SDK" />
4
4
  </project>
data/.idea/modules.xml CHANGED
@@ -1,8 +1,8 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="ProjectModuleManager">
4
- <modules>
5
- <module fileurl="file://$PROJECT_DIR$/.idea/PushRadarRuby.iml" filepath="$PROJECT_DIR$/.idea/PushRadarRuby.iml" />
6
- </modules>
7
- </component>
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/pushradar-server-ruby.iml" filepath="$PROJECT_DIR$/.idea/pushradar-server-ruby.iml" />
6
+ </modules>
7
+ </component>
8
8
  </project>
@@ -1,13 +1,16 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <module type="RUBY_MODULE" version="4">
3
- <component name="ModuleRunConfigurationManager">
4
- <shared />
5
- </component>
6
- <component name="NewModuleRootManager">
7
- <content url="file://$MODULE_DIR$" />
8
- <orderEntry type="inheritedJdk" />
9
- <orderEntry type="sourceFolder" forTests="false" />
10
- <orderEntry type="library" scope="PROVIDED" name="bundler (v1.15.3, ruby-2.4.1-p111) [gem]" level="application" />
11
- <orderEntry type="library" scope="PROVIDED" name="rake (v10.5.0, ruby-2.4.1-p111) [gem]" level="application" />
12
- </component>
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="RUBY_MODULE" version="4">
3
+ <component name="ModuleRunConfigurationManager">
4
+ <shared />
5
+ </component>
6
+ <component name="NewModuleRootManager">
7
+ <content url="file://$MODULE_DIR$">
8
+ <sourceFolder url="file://$MODULE_DIR$/features" isTestSource="true" />
9
+ <sourceFolder url="file://$MODULE_DIR$/spec" isTestSource="true" />
10
+ <sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
11
+ </content>
12
+ <orderEntry type="inheritedJdk" />
13
+ <orderEntry type="sourceFolder" forTests="false" />
14
+ <orderEntry type="library" scope="PROVIDED" name="bundler (v1.17.2, ruby-2.6.3-p62) [gem]" level="application" />
15
+ </component>
13
16
  </module>
data/.idea/vcs.xml CHANGED
@@ -1,6 +1,6 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="VcsDirectoryMappings">
4
- <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
- </component>
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
+ </component>
6
6
  </project>
data/CHANGELOG.md CHANGED
@@ -1,36 +1,11 @@
1
- ### 2.0.2 (2019-07-04)
1
+ ## 3.1.0 (2021-04-05)
2
2
 
3
- * Stable release for next version of PushRadar (PushRadar 2)
3
+ [NEW] Added support for client data registration
4
4
 
5
- ### 2.0.0 (2019-07-04)
5
+ ## 3.0.0 (2021-02-18)
6
6
 
7
- * Version 2 of the platform
7
+ [NEW] Updated for release of PushRadar v3
8
8
 
9
- ### 1.8.2 (2019-07-04)
9
+ ## 3.0.0-alpha.1 (2021-02-08)
10
10
 
11
- * Broadcasting bug fixes
12
-
13
- ### 1.8.1 (2019-07-04)
14
-
15
- * Changed channel auth token generation
16
-
17
- ### 1.8.0 (2019-07-04)
18
-
19
- * New PushRadar API
20
- * Added channel authentication
21
-
22
- ### 1.0.2 (2017-08-21)
23
-
24
- * Launch of PushRadar
25
-
26
- ### 1.0.1 (2017-08-16)
27
-
28
- * Modifications to continent targeting
29
-
30
- ### 1.0.0 (2017-08-16)
31
-
32
- * Closed beta release
33
-
34
- ### 0.9.1 (2017-08-13)
35
-
36
- * Pre-release of PushRadar's Ruby server library
11
+ [NEW] Work on base code of package, compatible with the next release of PushRadar
data/Gemfile CHANGED
@@ -1,5 +1,2 @@
1
- source 'https://rubygems.org'
2
-
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
-
1
+ source "https://rubygems.org"
5
2
  gemspec
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
- The MIT License (MIT)
1
+ The MIT License
2
2
 
3
- Copyright (c) 2019. PushRadar
3
+ Copyright (c) 2021. PushRadar
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,78 +1,114 @@
1
- ## Introduction
2
-
3
- [PushRadar](https://www.pushradar.com) is a realtime notifications API service for the web. The service uses a simple publish-subscribe model, allowing you to broadcast "notifications" on "channels" that are subscribed to by one or more clients. Notifications are pushed in realtime to those clients.
4
-
5
- PushRadar features advanced targeting options, including the ability to target clients by actions they have taken on your website or web app, geographical location (countries & continents), IP address, web browser and user ID.
6
-
7
- This is PushRadar's official Ruby library.
8
-
9
- ## Prerequisites
10
-
11
- In order to use this library, please ensure that you have the following:
12
-
13
- - A PushRadar account - you can sign up at [www.pushradar.com](https://www.pushradar.com).
14
-
15
- ## Installation
16
-
17
- To install PushRadar's Ruby gem, add this line to your application's Gemfile:
18
-
19
- ```ruby
20
- gem 'pushradar'
21
- ```
22
-
23
- And then execute:
24
-
25
- ```
26
- $ bundle
27
- ```
28
-
29
- Alternatively, you can install it yourself by running the command:
30
-
31
- ```
32
- $ gem install pushradar
33
- ```
34
-
35
- ## Getting Started
36
-
37
- "Hello World!" example:
38
-
39
- ```ruby
40
- require 'PushRadar'
41
- include PushRadar
42
-
43
- radar = Radar.new('your-secret-key')
44
- radar.broadcast('test-channel', {message: 'Hello World!'})
45
- ```
46
-
47
- ## Receiving Notifications
48
-
49
- To subscribe to channels and receive notifications broadcast on them, check out the documentation for PushRadar's [JavaScript client library](https://www.pushradar.com/documentation/latest/javascript).
50
-
51
- ## Fluent Syntax
52
-
53
- The library supports fluent method chaining to structure broadcasts. For example, to target a notification to website visitors in the US who have not used live chat before:
54
-
55
- ```ruby
56
- radar.target_country('US').target_not_action('live-chat').broadcast('test-channel',
57
- {message: 'Would you like to talk to one of our customer support team members on live chat?'})
58
- ```
59
-
60
- Please note that targeting options reset after each call to the `broadcast` method.
61
-
62
- ## Private and Presence Channels
63
-
64
- Private and presence channels require authentication before subscribers can receive messages on them.
65
-
66
- To generate a channel authentication token, PushRadar provides a convenient `channel_auth` method that you can call from your authentication endpoint:
67
-
68
- ```ruby
69
- {authToken: radar.channel_auth("channel-name")}
70
- ```
71
-
72
- Please note that private channels must start with the prefix 'private-' and presence channels must start with the prefix 'presence-'.
73
-
74
- Read the documentation on [private](https://www.pushradar.com/documentation/latest/private-channels) and [presence](https://www.pushradar.com/documentation/latest/presence-channels) channels to learn more.
75
-
76
- ## Documentation
77
-
78
- Full documentation for PushRadar's Ruby library can be found at: [www.pushradar.com/documentation/latest/ruby](https://www.pushradar.com/documentation/latest/ruby).
1
+ <p align="center"><a href="https://pushradar.com" target="_blank"><img src="https://pushradar.com/images/logo/pushradar-logo-dark.svg" width="300"></a></p>
2
+
3
+ <p align="center">
4
+ <a href="https://rubygems.org/gems/pushradar"><img src="https://img.shields.io/gem/v/pushradar?cacheSeconds=60&color=5b86e5"></a>
5
+ <a href="https://rubygems.org/gems/pushradar"><img src="https://img.shields.io/gem/dt/pushradar?cacheSeconds=60&color=5b86e5"></a>
6
+ <a href="https://rubygems.org/gems/pushradar"><img src="https://img.shields.io/packagist/l/pushradar/pushradar-server-php?cacheSeconds=60&color=5b86e5"></a>
7
+ </p>
8
+ <br />
9
+
10
+ ## PushRadar Ruby Server Library
11
+
12
+ [PushRadar](https://pushradar.com) is a realtime API service for the web. The service uses a simple publish-subscribe model, allowing you to broadcast "messages" on "channels" that are subscribed to by one or more clients. Messages are pushed in realtime to those clients.
13
+
14
+ This is PushRadar's official Ruby server library.
15
+
16
+ ## Prerequisites
17
+
18
+ In order to use this library, please ensure that you have the following:
19
+
20
+ - Ruby 2.4+
21
+ - A PushRadar account - you can sign up at [pushradar.com](https://pushradar.com)
22
+
23
+ ## Installation
24
+
25
+ The easiest way to get up and running is to install the library using bundler. Add the following to your Gemfile:
26
+
27
+ ```ruby
28
+ gem 'pushradar'
29
+ ```
30
+
31
+ And then run bundle install:
32
+
33
+ ```bash
34
+ $ bundle install
35
+ ```
36
+
37
+ Alterntively, install manually by running `$ gem install pushradar`
38
+
39
+ ## Broadcasting Messages
40
+
41
+ ```ruby
42
+ require 'pushradar'
43
+
44
+ radar = PushRadar::Client.new('your-secret-key')
45
+ radar.broadcast('channel-1', { message: 'Hello world!' })
46
+ ```
47
+
48
+ ## Receiving Messages
49
+
50
+ ```html
51
+ <script src="https://pushradar.com/js/v3/pushradar.min.js"></script>
52
+ <script>
53
+ var radar = new PushRadar('your-public-key');
54
+ radar.subscribe.to('channel-1', function (data) {
55
+ console.log(data.message);
56
+ });
57
+ </script>
58
+ ```
59
+
60
+ ## Private Channels
61
+
62
+ Private channels require authentication and start with the prefix **private-**. We recommend that you use private channels by default to prevent unauthorised access to channels.
63
+
64
+ You will need to set up an authentication endpoint that returns a token using the `auth(...)` method if the user is allowed to subscribe to the channel. For example:
65
+
66
+ ```ruby
67
+ radar = PushRadar::Client.new('your-secret-key')
68
+ channel_name = params[:channelName]
69
+ socket_id = params[:socketID]
70
+ # is user allowed to access channel?
71
+ if true
72
+ return { 'token': radar.auth(channel_name, socket_id) }.to_json
73
+ end
74
+ ```
75
+
76
+ Then register your authentication endpoint by calling the `auth(...)` method client-side:
77
+
78
+ ```javascript
79
+ radar.auth('/auth');
80
+ ```
81
+
82
+ ## Presence Channels
83
+
84
+ Presence channels require authentication and start with the prefix **presence-**. Presence channels are eligible for 'presence messages' containing information about channel subscribers.
85
+
86
+ You will need to set up an authentication endpoint as with private channels (see above). You should then register a `onPresence(...)` callback which will be called periodically. Your callback should accept two parameters: subscriber count and subscriber data. For example:
87
+
88
+ ```javascript
89
+ radar.auth('/auth');
90
+ radar.call.on.connection('/connected');
91
+
92
+ radar.subscribe.to('presence-channel-1', function (data) {
93
+ console.log(data.message);
94
+ }).onPresence(function (count, clientData) {
95
+ console.log(count);
96
+ });
97
+ ```
98
+
99
+ If you wish to pass through subscriber (client) data, you can set up an endpoint and pass its URL to the `call.on.connection(...)` method. Your endpoint will be called when a user first connects to the service. From your endpoint you can register client data as follows:
100
+
101
+ ```ruby
102
+ radar = PushRadar::Client.new('your-secret-key')
103
+ socket_id = params[:socketID]
104
+ radar.register_client_data(socket_id, { '##uniqueID': 1, 'name': 'James Smith' })
105
+ ```
106
+
107
+ ## Complete Documentation
108
+
109
+ Complete documentation for PushRadar's Ruby server library can be found at: <https://pushradar.com/docs/3.x/ruby>
110
+
111
+ ## License
112
+
113
+ Copyright © 2021, PushRadar. PushRadar's Ruby server library is licensed under the MIT license:
114
+ <https://opensource.org/licenses/mit-license.php>
data/Rakefile CHANGED
@@ -1 +1,2 @@
1
- require 'bundler/gem_tasks'
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.0.2
1
+ 3.1.0
data/lib/pushradar.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'uri'
2
+ require 'forwardable'
3
+
4
+ require_relative 'pushradar/client'
5
+
6
+ module PushRadar
7
+ class Error < RuntimeError; end
8
+ end
9
+
10
+ require_relative 'pushradar/version'
@@ -0,0 +1,119 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'cgi'
4
+
5
+ module PushRadar
6
+ class Client
7
+ def initialize(secret_key)
8
+ unless secret_key.is_a?(String)
9
+ raise PushRadar::Error, 'Secret key must be a string.'
10
+ end
11
+
12
+ unless secret_key.start_with?('sk_')
13
+ raise PushRadar::Error, 'Please provide your PushRadar secret key. You can find it on the API page of your dashboard.'
14
+ end
15
+
16
+ @secret_key = secret_key
17
+ @api_endpoint = 'https://api.pushradar.com/v3'
18
+ end
19
+
20
+ def validate_channel_name(channel_name)
21
+ if /[^A-Za-z0-9_\-=@,.;]/.match(channel_name)
22
+ raise PushRadar::Error, "Invalid channel name: #{channel_name}. Channel names cannot contain spaces, and must consist of only " +
23
+ "upper and lowercase letters, numbers, underscores, equals characters, @ characters, commas, periods, semicolons, " +
24
+ "and hyphens (A-Za-z0-9_=@,.;-)."
25
+ end
26
+ end
27
+
28
+ def broadcast(channel_name, data)
29
+ unless channel_name.is_a?(String)
30
+ raise PushRadar::Error, 'Channel name must be a string.'
31
+ end
32
+
33
+ if channel_name.nil? || channel_name.strip.empty?
34
+ raise PushRadar::Error, 'Channel name empty. Please provide a channel name.'
35
+ end
36
+
37
+ validate_channel_name(channel_name)
38
+ response = do_http_request('POST', @api_endpoint + "/broadcasts", { channel: channel_name, data: data.to_json })
39
+
40
+ if response[:status] === 200
41
+ true
42
+ else
43
+ raise PushRadar::Error, 'An error occurred while calling the API. Server returned: ' + response[:body]
44
+ end
45
+ end
46
+
47
+ def auth(channel_name, socket_id)
48
+ unless channel_name.is_a?(String)
49
+ raise PushRadar::Error, 'Channel name must be a string.'
50
+ end
51
+
52
+ if channel_name.nil? || channel_name.strip.empty?
53
+ raise PushRadar::Error, 'Channel name empty. Please provide a channel name.'
54
+ end
55
+
56
+ unless channel_name.start_with?('private-') || channel_name.start_with?('presence-')
57
+ raise PushRadar::Error, 'Channel authentication can only be used with private and presence channels.'
58
+ end
59
+
60
+ unless socket_id.is_a?(String)
61
+ raise PushRadar::Error, 'Socket ID must be a string.'
62
+ end
63
+
64
+ if socket_id.nil? || socket_id.strip.empty?
65
+ raise PushRadar::Error, 'Socket ID empty. Please pass through a socket ID.'
66
+ end
67
+
68
+ response = do_http_request('GET', @api_endpoint + "/channels/auth?channel=" + CGI.escape(channel_name) + "&socketID=" + CGI.escape(socket_id), {})
69
+ if response[:status] === 200
70
+ JSON(response[:body])['token']
71
+ else
72
+ raise PushRadar::Error, 'There was a problem receiving a channel authentication token. Server returned: ' + response[:body]
73
+ end
74
+ end
75
+
76
+ def register_client_data(socket_id, client_data)
77
+ unless socket_id.is_a?(String)
78
+ raise PushRadar::Error, 'Socket ID must be a string.'
79
+ end
80
+
81
+ if socket_id.nil? || socket_id.strip.empty?
82
+ raise PushRadar::Error, 'Socket ID empty. Please pass through a socket ID.'
83
+ end
84
+
85
+ response = do_http_request('POST', @api_endpoint + "/client-data", { socketID: socket_id, clientData: client_data.to_json })
86
+
87
+ if response[:status] === 200
88
+ true
89
+ else
90
+ raise PushRadar::Error, 'An error occurred while calling the API. Server returned: ' + response[:body]
91
+ end
92
+ end
93
+
94
+ def do_http_request(method, url, data)
95
+ uri = URI.parse(url)
96
+ http = Net::HTTP.new(uri.host, uri.port)
97
+ http.use_ssl = true
98
+
99
+ if method.downcase === 'post'
100
+ req = Net::HTTP::Post.new(url)
101
+ req['X-PushRadar-Library'] = 'pushradar-server-ruby ' + VERSION
102
+ req['Authorization'] = "Bearer " + @secret_key
103
+ req.content_type = 'application/json'
104
+ data = data.to_json
105
+ req.body = data
106
+ else
107
+ req = Net::HTTP::Get.new(url)
108
+ req['X-PushRadar-Library'] = 'pushradar-server-ruby ' + VERSION
109
+ req['Authorization'] = "Bearer " + @secret_key
110
+ req.content_type = 'application/json'
111
+ end
112
+
113
+ response = http.request(req)
114
+ { body: response.body, status: response.code.to_i }
115
+ end
116
+
117
+ private :validate_channel_name, :do_http_request
118
+ end
119
+ end