huawei-push-kit 1.0.3

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 185268eddeb26c9646757b6d12cd9b2bc2de5a976e59390340bd3be5432ed5cb
4
+ data.tar.gz: dac8f55a3d7358c866891a1dda6da67e404a55bb74f0a10b451929443d507b53
5
+ SHA512:
6
+ metadata.gz: 7d27bc596705f0b4d09fc8d7669be3bb4d531ef37801b6dfd89b9abb4c5cded09db3f9e9799b97f6de1af1a0a064674466e62c5833278f2cd32b65d0bef052b4
7
+ data.tar.gz: 9c9d46ff974bc6dced5d9efc53f0a128814a6b881df7f9a83c664a7cd04a924a0a2b995eb390d0c285021c24fe3a097e2b1d2060241ed2f01d6c737d0ac7b7b6
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) [2024] [Karim Marabet]
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # 📬 Huawei Push Kit Ruby Client
2
+
3
+ Welcome to the unofficial Ruby client for Huawei Push Kit, designed to seamlessly integrate Huawei Push Kit capabilities into your Ruby applications.
4
+
5
+ ## 🌟 Features
6
+
7
+ - Send notifications to devices using a simple Ruby interface.
8
+ - Support for major features provided by the Huawei Push Kit API.
9
+
10
+ ## 🛠 Installation
11
+
12
+ To start using this gem in your Ruby application, add this line to your Gemfile:
13
+
14
+ ```ruby
15
+ gem 'huawei_push_kit'
16
+ ```
17
+ Then execute:
18
+
19
+ ```ruby
20
+ bundle install
21
+ ```
22
+
23
+ Or install it yourself as:
24
+ ```ruby
25
+ gem install huawei_push_kit
26
+ ```
27
+
28
+ # âš™ Configuration
29
+ Before using the client, you need to configure your access keys obtained from the Huawei Developer Console and add it to the `.env` file.
30
+
31
+ ```
32
+ HUAWEI_PUSH_KIT_CLIENT_ID=<HUAWEI_PUSH_KIT_CLIENT_ID>
33
+ HUAWEI_PUSH_KIT_CLIENT_SECRET=<HUAWEI_PUSH_KIT_CLIENT_SECRET>
34
+ ```
35
+
36
+ # 🚀 Usage
37
+
38
+ ```ruby
39
+ device_token = "<huawei_device_token>"
40
+ huawei_push_kit_client = HuaweiPushKit::Client.new
41
+
42
+ # Send notification to a single Huawei device
43
+ client.send_push_notification(device_token, "Title", "Message")
44
+
45
+ # Subscribe to a topic
46
+ huawei_push_kit_client.subscribe_to_topic("topic_name", device_token)
47
+
48
+ # Show all topics
49
+ huawei_push_kit_client.topic_list(device_token)
50
+
51
+ # Send notification to a topic
52
+ client.send_push_notification_to_topic(topic_name, "Title", "Message", "topic_name")
53
+
54
+ # Unsubscribe from a topic
55
+ huawei_push_kit_client.unsubscribe_from_topic("topic_name", device_token)
56
+
57
+ ```
58
+
59
+ # 🔓 License
60
+ This gem is available as open source under the terms of the MIT License. See the LICENSE file for more details.
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HuaweiPushKit
4
+ class ApiResponse
5
+ def initialize(response)
6
+ @response = JSON.parse(response.body)
7
+ end
8
+
9
+ def successful?
10
+ response["code"] == "80000000"
11
+ end
12
+
13
+ def error
14
+ response["errors"]&.join(", ")
15
+ end
16
+
17
+ def body
18
+ JSON.parse(response.body)
19
+ end
20
+
21
+ private
22
+
23
+ attr_reader :response
24
+ end
25
+ end
@@ -0,0 +1,138 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "faraday"
4
+ require "json"
5
+
6
+ module HuaweiPushKit
7
+ class Client
8
+ PUSH_API_URI = "https://push-api.cloud.huawei.com"
9
+ OAUTH_URI = "https://oauth-login.cloud.huawei.com"
10
+ CONTENT_JSON = "application/json"
11
+ CONTENT_URL_ENCODED = "application/x-www-form-urlencoded"
12
+
13
+ def initialize(client_id = ENV.fetch("HUAWEI_PUSH_KIT_CLIENT_ID"), client_secret = ENV.fetch("HUAWEI_PUSH_KIT_CLIENT_SECRET"))
14
+ @client_id = client_id
15
+ @client_secret = client_secret
16
+ end
17
+
18
+ def send_push_notification(device_token, title, body, type = nil, badge = nil, post_id = nil, comment_id = nil,
19
+ chat_id = nil, avatar = nil)
20
+ response = push_api_connection.post("/v1/#{client_id}/messages:send") do |req|
21
+ req.body = build_notification_body(device_token, title, body, type, badge, post_id, comment_id, chat_id, avatar)
22
+ end
23
+
24
+ ApiResponse.new(response)
25
+ end
26
+
27
+ def send_push_notification_to_topic(topic_name, title, body, type = nil, badge = nil, post_id = nil,
28
+ comment_id = nil, chat_id = nil, avatar = nil)
29
+ response = push_api_connection.post("/v1/#{client_id}/messages:send") do |req|
30
+ req.body = build_topic_notification_body(topic_name, title, body, type, badge, post_id, comment_id, chat_id,
31
+ avatar)
32
+ end
33
+
34
+ ApiResponse.new(response)
35
+ end
36
+
37
+ def subscribe_to_topic(topic_name, device_tokens)
38
+ response = push_api_connection.post("/v1/#{client_id}/topic:subscribe") do |req|
39
+ req.body = { topic: topic_name, tokenArray: Array(device_tokens) }.to_json
40
+ end
41
+
42
+ ApiResponse.new(response)
43
+ end
44
+
45
+ def unsubscribe_from_topic(topic_name, device_tokens)
46
+ response = push_api_connection.post("/v1/#{client_id}/topic:unsubscribe") do |req|
47
+ req.body = { topic: topic_name, tokenArray: Array(device_tokens) }.to_json
48
+ end
49
+
50
+ ApiResponse.new(response)
51
+ end
52
+
53
+ def topic_list(device_token)
54
+ response = push_api_connection.post("/v1/#{client_id}/topic:list") do |req|
55
+ req.body = { token: device_token }.to_json
56
+ end
57
+
58
+ ApiResponse.new(response)
59
+ end
60
+
61
+ private
62
+
63
+ attr_reader :client_id, :client_secret
64
+
65
+ def build_topic_notification_body(topic_name, title, text, type = nil, badge = nil, post_id = nil,
66
+ comment_id = nil, chat_id = nil, avatar = nil)
67
+ {
68
+ validate_only: false,
69
+ message: {
70
+ data: {
71
+ title:,
72
+ message: text,
73
+ type:,
74
+ badge:,
75
+ post_id:,
76
+ comment_id:,
77
+ chat_id:,
78
+ avatar:
79
+ }.to_json,
80
+ topic: topic_name
81
+ }
82
+ }.to_json
83
+ end
84
+
85
+ def build_notification_body(device_token, title, text, type = nil, badge = nil, post_id = nil, comment_id = nil,
86
+ chat_id = nil, avatar = nil)
87
+ {
88
+ validate_only: false,
89
+ message: {
90
+ data: {
91
+ title:,
92
+ message: text,
93
+ type:,
94
+ badge:,
95
+ post_id:,
96
+ comment_id:,
97
+ chat_id:,
98
+ avatar:
99
+ }.to_json,
100
+ token: [
101
+ device_token
102
+ ]
103
+ }
104
+ }.to_json
105
+ end
106
+
107
+ def access_token
108
+ response = oauth_connection.post("/oauth2/v3/token") do |req|
109
+ req.body = URI.encode_www_form({
110
+ grant_type: "client_credentials",
111
+ client_id:,
112
+ client_secret:
113
+ })
114
+ end
115
+
116
+ JSON.parse(response.body)["access_token"]
117
+ end
118
+
119
+ def push_api_connection
120
+ Faraday.new(PUSH_API_URI) do |faraday|
121
+ setup_connection(faraday, CONTENT_JSON, "Bearer #{access_token}")
122
+ end
123
+ end
124
+
125
+ def oauth_connection
126
+ Faraday.new(OAUTH_URI) do |faraday|
127
+ setup_connection(faraday, CONTENT_URL_ENCODED)
128
+ end
129
+ end
130
+
131
+ def setup_connection(faraday, content_type, authorization = nil)
132
+ faraday.headers["Content-Type"] = content_type
133
+ faraday.headers["Authorization"] = authorization if authorization
134
+ faraday.response :raise_error
135
+ faraday.adapter Faraday.default_adapter
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,2 @@
1
+ require_relative 'huawei_push_kit/client'
2
+ require_relative 'huawei_push_kit/api_response'
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: huawei-push-kit
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Karim Marabet
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-05-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '13.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '13.0'
69
+ description: Huawei Push Kit Client for Ruby
70
+ email: newrabgo@gmail.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - LICENSE.txt
76
+ - README.md
77
+ - lib/huawei_push_kit.rb
78
+ - lib/huawei_push_kit/api_response.rb
79
+ - lib/huawei_push_kit/client.rb
80
+ homepage: https://github.com/mrabets/huawei-push-kit
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubygems_version: 3.5.3
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Huawei Push Kit Client for Ruby
103
+ test_files: []