khl 1.0.1 → 1.2.1
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 +4 -4
- data/CHANGELOG.md +17 -4
- data/README.md +7 -1
- data/lib/khl/event.rb +24 -0
- data/lib/khl/http/response.rb +10 -7
- data/lib/khl/message.rb +65 -0
- data/lib/khl/version.rb +1 -1
- data/lib/khl/web_socket/client.rb +2 -2
- data/lib/khl/webhook.rb +31 -0
- data/lib/khl.rb +1 -0
- metadata +6 -4
- data/lib/khl/web_socket/message.rb +0 -54
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5b3ff60071d40b884b84c71573ec653fe5234c849e70adfa9d3527c8b790eb2e
|
4
|
+
data.tar.gz: cc54276f09718288a23b72b8f55e49cc12a19b20f15680af4f6478dffbe5153d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7f83216e039d7b3c3c088ab07d0900ecdd60b6b2f70a360933d05d41265ec6c43160c895bca64add54529dc026f5cbfdd0c8084e12afffb62b6681f87ef5fec6
|
7
|
+
data.tar.gz: 8e4558eab997c80038df0385ce67141797980c74358276dcc58ec7e56b3f5842f1d3d31eff73c8f71bb9c9a6fb86eaf13aac26075888bdd0b8bdabb9019e020f
|
data/CHANGELOG.md
CHANGED
@@ -1,9 +1,22 @@
|
|
1
|
-
### 1.
|
1
|
+
### 1.2.1 - 2022-04-18
|
2
2
|
|
3
|
-
* Add
|
4
|
-
|
5
|
-
|
3
|
+
* Add hash support for KHL::Message.parse
|
4
|
+
|
5
|
+
### 1.2.0 - 2022-04-08
|
6
|
+
|
7
|
+
* Fix 开黑啦 Webhook API
|
8
|
+
|
9
|
+
### 1.1.0 - 2022-04-02
|
10
|
+
|
11
|
+
* Add 开黑啦 Webhook API
|
12
|
+
* Add Event Wrapper
|
6
13
|
|
7
14
|
### 1.0.1 - 2022-03-29
|
8
15
|
|
9
16
|
* Fix params type
|
17
|
+
|
18
|
+
### 1.0.0 - 2022-03-29
|
19
|
+
|
20
|
+
* Add 开黑啦 HTTP API
|
21
|
+
* Add 开黑啦 WebSocket API
|
22
|
+
* Add GitHub CI
|
data/README.md
CHANGED
@@ -40,13 +40,19 @@ ws_client = KHL::WebSocket::Client.new(token: token)
|
|
40
40
|
Thread.new { ws_client.run } # Run WebSocket client
|
41
41
|
ws_clinet.state # Get current state
|
42
42
|
ws_client.messages.pop # Get message from queue
|
43
|
+
|
44
|
+
# Use Webhook API in Rails
|
45
|
+
params = KHL::Webhook.decompress(request.body.string)
|
46
|
+
encrypted_data = params[:encrypt]
|
47
|
+
raw_json = KHL::Webhook.decode("your_encrypt_key", encrypted_data)
|
48
|
+
message = KHL::Message.parse(raw_json)
|
43
49
|
```
|
44
50
|
|
45
51
|
## Development
|
46
52
|
|
47
53
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
48
54
|
|
49
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version,
|
55
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, please bump version, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
50
56
|
|
51
57
|
## Contributing
|
52
58
|
|
data/lib/khl/event.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/hash/indifferent_access"
|
4
|
+
require "active_support/hash_with_indifferent_access"
|
5
|
+
|
6
|
+
module KHL
|
7
|
+
class Event
|
8
|
+
attr_accessor :channel_type, :type, :target_id, :author_id, :content, :msg_id, :msg_timestamp, :nonce, :extra
|
9
|
+
|
10
|
+
def initialize(params = {})
|
11
|
+
params = ActiveSupport::HashWithIndifferentAccess.new(params)
|
12
|
+
|
13
|
+
@channel_type = params[:channel_type]
|
14
|
+
@type = params[:type]
|
15
|
+
@target_id = params[:target_id]
|
16
|
+
@author_id = params[:author_id]
|
17
|
+
@content = params[:content]
|
18
|
+
@msg_id = params[:msg_id]
|
19
|
+
@msg_timestamp = params[:msg_timestamp]
|
20
|
+
@nonce = params[:nonce]
|
21
|
+
@extra = params[:extra]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/khl/http/response.rb
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "active_support/core_ext/hash/indifferent_access"
|
4
|
+
require "active_support/hash_with_indifferent_access"
|
5
|
+
|
3
6
|
module KHL
|
4
7
|
module HTTP
|
5
8
|
class Response
|
@@ -9,16 +12,16 @@ module KHL
|
|
9
12
|
def initialize(raw_response)
|
10
13
|
@raw_response = raw_response
|
11
14
|
@http_code = raw_response.code.to_i
|
12
|
-
@body = JSON.parse(@raw_response.body)
|
15
|
+
@body = ActiveSupport::HashWithIndifferentAccess.new(JSON.parse(@raw_response.body))
|
13
16
|
end
|
14
17
|
|
15
18
|
def data
|
16
|
-
@body[
|
19
|
+
@body[:data]
|
17
20
|
end
|
18
21
|
|
19
22
|
# The code in the response body
|
20
23
|
def code
|
21
|
-
@body[
|
24
|
+
@body[:code]
|
22
25
|
end
|
23
26
|
|
24
27
|
def max_request_limit
|
@@ -50,19 +53,19 @@ module KHL
|
|
50
53
|
end
|
51
54
|
|
52
55
|
def message
|
53
|
-
@body[
|
56
|
+
@body[:message]
|
54
57
|
end
|
55
58
|
|
56
59
|
def page
|
57
|
-
data.dig(
|
60
|
+
data.dig(:meta, :page) || 0
|
58
61
|
end
|
59
62
|
|
60
63
|
def page_size
|
61
|
-
data.dig(
|
64
|
+
data.dig(:meta, :page_size) || 0
|
62
65
|
end
|
63
66
|
|
64
67
|
def page_total
|
65
|
-
data.dig(
|
68
|
+
data.dig(:meta, :total) || 0
|
66
69
|
end
|
67
70
|
end
|
68
71
|
end
|
data/lib/khl/message.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/hash/indifferent_access"
|
4
|
+
require "active_support/hash_with_indifferent_access"
|
5
|
+
|
6
|
+
require_relative "event"
|
7
|
+
|
8
|
+
module KHL
|
9
|
+
class Message
|
10
|
+
TYPES = %i[
|
11
|
+
event
|
12
|
+
hello
|
13
|
+
ping
|
14
|
+
pong
|
15
|
+
resume
|
16
|
+
reconnect
|
17
|
+
resume_ack
|
18
|
+
].freeze
|
19
|
+
|
20
|
+
attr_accessor :type, :data, :sn
|
21
|
+
|
22
|
+
def self.parse(raw)
|
23
|
+
return unless raw
|
24
|
+
|
25
|
+
data = raw.is_a?(String) ? JSON.parse(raw) : raw
|
26
|
+
data = ActiveSupport::HashWithIndifferentAccess.new(data)
|
27
|
+
|
28
|
+
Message.new(
|
29
|
+
type: TYPES[data[:s]],
|
30
|
+
data: data[:d],
|
31
|
+
sn: data[:sn]
|
32
|
+
)
|
33
|
+
end
|
34
|
+
|
35
|
+
def initialize(type: nil, data: nil, sn: nil)
|
36
|
+
@type = type
|
37
|
+
@data = data
|
38
|
+
@sn = sn
|
39
|
+
end
|
40
|
+
|
41
|
+
def event
|
42
|
+
return unless event?
|
43
|
+
|
44
|
+
Event.new(@data)
|
45
|
+
end
|
46
|
+
|
47
|
+
def to_h
|
48
|
+
ActiveSupport::HashWithIndifferentAccess.new(
|
49
|
+
s: TYPES.index(type),
|
50
|
+
d: data,
|
51
|
+
sn: sn
|
52
|
+
)
|
53
|
+
end
|
54
|
+
|
55
|
+
def to_json
|
56
|
+
to_h.to_json
|
57
|
+
end
|
58
|
+
|
59
|
+
TYPES.each do |type|
|
60
|
+
define_method("#{type}?") do
|
61
|
+
@type == type
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/lib/khl/version.rb
CHANGED
@@ -7,7 +7,7 @@ require "json"
|
|
7
7
|
require "uri"
|
8
8
|
require "zlib"
|
9
9
|
|
10
|
-
require_relative "message"
|
10
|
+
require_relative "../message"
|
11
11
|
|
12
12
|
module KHL
|
13
13
|
module WebSocket
|
@@ -24,7 +24,7 @@ module KHL
|
|
24
24
|
# @option config [String] :token_type Token type
|
25
25
|
# @option config [String] :language Language
|
26
26
|
# @option config [Boolean] :compress Compress
|
27
|
-
def initialize(config)
|
27
|
+
def initialize(config = {})
|
28
28
|
config[:compress] ||= false
|
29
29
|
|
30
30
|
@config = config
|
data/lib/khl/webhook.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module KHL
|
4
|
+
# @example
|
5
|
+
# # Rails Environment
|
6
|
+
# params = KHL::Webhook.decompress(request.body.string)
|
7
|
+
# encrypted_data = params[:encrypt]
|
8
|
+
# raw_json = KHL::Webhook.decode("your_encrypt_key", encrypted_data)
|
9
|
+
# message = KHL::Message.parse(raw_json)
|
10
|
+
module Webhook
|
11
|
+
# Decode the data
|
12
|
+
# @param [String] encrypt_key Encrypt Key
|
13
|
+
# @param [String] encrypted_data Encrypted data
|
14
|
+
# @return [String] Decrypted data
|
15
|
+
def self.decode(encrypt_key, encrypted_data)
|
16
|
+
encrypted_data = Base64.strict_decode64(encrypted_data)
|
17
|
+
cipher = OpenSSL::Cipher.new("aes-256-cbc")
|
18
|
+
cipher.decrypt
|
19
|
+
cipher.iv = encrypted_data[0..15]
|
20
|
+
cipher.key = encrypt_key.ljust(32, "\0")
|
21
|
+
cipher.update(encrypted_data) + cipher.final
|
22
|
+
end
|
23
|
+
|
24
|
+
# Decompress the data
|
25
|
+
# @param [String] data Compressed data
|
26
|
+
# @return [String] Decompressed data
|
27
|
+
def self.decompress(data)
|
28
|
+
Zlib::Inflate.inflate(data)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/khl.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: khl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dounx
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-04-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -54,6 +54,7 @@ files:
|
|
54
54
|
- CHANGELOG.md
|
55
55
|
- README.md
|
56
56
|
- lib/khl.rb
|
57
|
+
- lib/khl/event.rb
|
57
58
|
- lib/khl/http/asset.rb
|
58
59
|
- lib/khl/http/badge.rb
|
59
60
|
- lib/khl/http/base.rb
|
@@ -75,9 +76,10 @@ files:
|
|
75
76
|
- lib/khl/http/response.rb
|
76
77
|
- lib/khl/http/user.rb
|
77
78
|
- lib/khl/http/user_chat.rb
|
79
|
+
- lib/khl/message.rb
|
78
80
|
- lib/khl/version.rb
|
79
81
|
- lib/khl/web_socket/client.rb
|
80
|
-
- lib/khl/
|
82
|
+
- lib/khl/webhook.rb
|
81
83
|
homepage: https://github.com/DessertShop/KHL
|
82
84
|
licenses:
|
83
85
|
- MIT
|
@@ -101,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
101
103
|
- !ruby/object:Gem::Version
|
102
104
|
version: '0'
|
103
105
|
requirements: []
|
104
|
-
rubygems_version: 3.
|
106
|
+
rubygems_version: 3.3.11
|
105
107
|
signing_key:
|
106
108
|
specification_version: 4
|
107
109
|
summary: Ruby SDK for 开黑啦
|
@@ -1,54 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module KHL
|
4
|
-
module WebSocket
|
5
|
-
class Message
|
6
|
-
TYPES = %i[
|
7
|
-
event
|
8
|
-
hello
|
9
|
-
ping
|
10
|
-
pong
|
11
|
-
resume
|
12
|
-
reconnect
|
13
|
-
resume_ack
|
14
|
-
].freeze
|
15
|
-
|
16
|
-
attr_accessor :type, :data, :sn
|
17
|
-
|
18
|
-
def self.parse(raw)
|
19
|
-
return unless raw
|
20
|
-
|
21
|
-
data = JSON.parse(raw)
|
22
|
-
message = Message.new
|
23
|
-
message.type = TYPES[data["s"]]
|
24
|
-
message.data = data["d"]
|
25
|
-
message.sn = data["sn"]
|
26
|
-
message
|
27
|
-
end
|
28
|
-
|
29
|
-
def initialize(type: nil, data: nil, sn: nil)
|
30
|
-
@type = type
|
31
|
-
@data = data
|
32
|
-
@sn = sn
|
33
|
-
end
|
34
|
-
|
35
|
-
def to_h
|
36
|
-
{
|
37
|
-
s: TYPES.index(type),
|
38
|
-
d: data,
|
39
|
-
sn: sn
|
40
|
-
}
|
41
|
-
end
|
42
|
-
|
43
|
-
def to_json
|
44
|
-
to_h.to_json
|
45
|
-
end
|
46
|
-
|
47
|
-
TYPES.each do |type|
|
48
|
-
define_method("#{type}?") do
|
49
|
-
@type == type
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|