pushradar 2.0.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: d5ab52adafa179f70ef8372afdef5ff22e87aa00
4
- data.tar.gz: 4ecc373a0598bf42d7f6625ad909e37f1483be90
2
+ SHA256:
3
+ metadata.gz: 9edffa9f99b71bf60f915bfc6a4d91be7481491a29e84214fa36aa68e2e669cc
4
+ data.tar.gz: a3e47791e64cd2796ce5cb0820c0af3d5dc2c4b0282f693f256264e5a22015e9
5
5
  SHA512:
6
- metadata.gz: 1b6794052f56995da6c246671cc93c68ace686603df65faa88566cff639c7a2ce284e6426e76e5edec9f785511433c5eb0cf2f81a3a59b8cbdf566cb4ac67169
7
- data.tar.gz: 34c53f2221f27a148ecbdbccf26e5893463a53ebd307a1c1a867823acde34e94e900456b4d6f7899478b1725cbc50f26d0150c47a0bb78241feb1a714118c0af
6
+ metadata.gz: e8b05a7441edc5070490c0a909b0800105fe361a99a7f34965d94d4e0253be4bab21c2b18c8128e3a8d24a35468555c691d0a2f0c1eebbe9df1a988c3f7eec4d
7
+ data.tar.gz: e6c32fc1588940ff1ea0f8cd991dcbc4256b47befa4c92cacadd316eda1df46b00efd36284d842ef5e09c238893bd73525405100d80cc1502711335df463c1fb
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,32 +1,7 @@
1
- ### 2.0.0 (2019-07-04)
1
+ ## 3.0.0 (2021-02-18)
2
2
 
3
- * Version 2 of the platform
3
+ [NEW] Updated for release of PushRadar v3
4
4
 
5
- ### 1.8.2 (2019-07-04)
5
+ ## 3.0.0-alpha.1 (2021-02-08)
6
6
 
7
- * Broadcasting bug fixes
8
-
9
- ### 1.8.1 (2019-07-04)
10
-
11
- * Changed channel auth token generation
12
-
13
- ### 1.8.0 (2019-07-04)
14
-
15
- * New PushRadar API
16
- * Added channel authentication
17
-
18
- ### 1.0.2 (2017-08-21)
19
-
20
- * Launch of PushRadar
21
-
22
- ### 1.0.1 (2017-08-16)
23
-
24
- * Modifications to continent targeting
25
-
26
- ### 1.0.0 (2017-08-16)
27
-
28
- * Closed beta release
29
-
30
- ### 0.9.1 (2017-08-13)
31
-
32
- * Pre-release of PushRadar's Ruby server library
7
+ [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,80 +1,89 @@
1
- <p align="center"><img src="https://www.pushradar.com/img/logo/pushradar_github.png"></p>
2
-
3
- ## Introduction
4
-
5
- [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.
6
-
7
- 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.
8
-
9
- This is PushRadar's official Ruby library.
10
-
11
- ## Prerequisites
12
-
13
- In order to use this library, please ensure that you have the following:
14
-
15
- - A PushRadar account - you can sign up at [www.pushradar.com](https://www.pushradar.com).
16
-
17
- ## Installation
18
-
19
- To install PushRadar's Ruby gem, add this line to your application's Gemfile:
20
-
21
- ```ruby
22
- gem 'pushradar'
23
- ```
24
-
25
- And then execute:
26
-
27
- ```
28
- $ bundle
29
- ```
30
-
31
- Alternatively, you can install it yourself by running the command:
32
-
33
- ```
34
- $ gem install pushradar
35
- ```
36
-
37
- ## Getting Started
38
-
39
- "Hello World!" example:
40
-
41
- ```ruby
42
- require 'PushRadar'
43
- include PushRadar
44
-
45
- radar = Radar.new('your-secret-key')
46
- radar.broadcast('test-channel', {message: 'Hello World!'})
47
- ```
48
-
49
- ## Receiving Notifications
50
-
51
- To subscribe to channels and receive notifications broadcast on them, check out the documentation for PushRadar's [JavaScript client library](https://www.pushradar.com/docs/latest/javascript).
52
-
53
- ## Fluent Syntax
54
-
55
- 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:
56
-
57
- ```ruby
58
- radar.target_country('US').target_not_action('live-chat').broadcast('test-channel',
59
- {message: 'Would you like to talk to one of our customer support team members on live chat?'})
60
- ```
61
-
62
- Please note that targeting options reset after each call to the `broadcast` method.
63
-
64
- ## Private and Presence Channels
65
-
66
- Private and presence channels require authentication before subscribers can receive messages on them.
67
-
68
- To generate a channel authentication token, PushRadar provides a convenient method that you can call from your authentication endpoint.
69
-
70
- ```ruby
71
- {authToken: radar.channel_auth("channel-name")}
72
- ```
73
-
74
- Please note that private channels must start with the prefix 'private-' and presence channels must start with the prefix 'presence-'.
75
-
76
- 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.
77
-
78
- ## Documentation
79
-
80
- Full documentation for PushRadar's Ruby library can be found at: [www.pushradar.com/docs/ruby](https://www.pushradar.com/docs/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
+ ## Complete Documentation
83
+
84
+ Complete documentation for PushRadar's Ruby server library can be found at: <https://pushradar.com/docs/3.x?lang=ruby>
85
+
86
+ ## License
87
+
88
+ Copyright © 2021, PushRadar. PushRadar's Ruby server library is licensed under the MIT license:
89
+ <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.0
1
+ 3.0.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,101 @@
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-')
57
+ raise PushRadar::Error, 'Channel authentication can only be used with private 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 do_http_request(method, url, data)
77
+ uri = URI.parse(url)
78
+ http = Net::HTTP.new(uri.host, uri.port)
79
+ http.use_ssl = true
80
+
81
+ if method.downcase === 'post'
82
+ req = Net::HTTP::Post.new(url)
83
+ req['X-PushRadar-Library'] = 'pushradar-server-ruby ' + VERSION
84
+ req['Authorization'] = "Bearer " + @secret_key
85
+ req.content_type = 'application/json'
86
+ data = data.to_json
87
+ req.body = data
88
+ else
89
+ req = Net::HTTP::Get.new(url)
90
+ req['X-PushRadar-Library'] = 'pushradar-server-ruby ' + VERSION
91
+ req['Authorization'] = "Bearer " + @secret_key
92
+ req.content_type = 'application/json'
93
+ end
94
+
95
+ response = http.request(req)
96
+ { body: response.body, status: response.code.to_i }
97
+ end
98
+
99
+ private :validate_channel_name, :do_http_request
100
+ end
101
+ end