khl 1.0.0 → 1.2.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
2
  SHA256:
3
- metadata.gz: ed901cab84178a2961285b975bf703bb034b998e0d70e7e8c8e06c5f27187137
4
- data.tar.gz: 0447d12135db7697c59aadfd7f358e64982db782af00c2fdb90c4ea1bb2123a7
3
+ metadata.gz: 3110943f00d66a4139d82f1f13798cde8f4ffddc0462cffeb3c47c5e131a2b47
4
+ data.tar.gz: bc8cb50c6876fbbc769667525f84bcbda3c2a9aa1d1e76bfdb00a2525a443ea8
5
5
  SHA512:
6
- metadata.gz: 2b71ec8d6a9f2e46c412a0552f2f6484d37a273ce141386ea9b94c8756ab54d15774d857cc42a25f1b4ea6be32b1a5e03d4544d04b1911dfc121778bed1ef4c5
7
- data.tar.gz: 97ee22265dd77c70c3d432418e2a89410b2eb14984a81df83a08206fe1344f6c3479090afcb488e8f43bf9ba363ffaa8c33ff8dbe8804b3ec6932fdd4bb0e05e
6
+ metadata.gz: 8b4be1d65e2cb54e3d1b1a762253bc2fa3578bf448b056d9905edc7a0823c216802a857122b6904b93484f016585b2c5d5e314500b38ad0839765526ac6f0a43
7
+ data.tar.gz: 99916625ffb6cb60f9613785167cae2209ad6e0f9f61af09a2f22e4d84605d4041aefaf0d6630612bb45adc22f6cb01881e13debe15cb1be5b4701596e36d480
data/CHANGELOG.md CHANGED
@@ -1,3 +1,16 @@
1
+ ### 1.2.0 - 2022-04-08
2
+
3
+ * Fix 开黑啦 Webhook API
4
+
5
+ ### 1.1.0 - 2022-04-02
6
+
7
+ * Add 开黑啦 Webhook API
8
+ * Add Event Wrapper
9
+
10
+ ### 1.0.1 - 2022-03-29
11
+
12
+ * Fix params type
13
+
1
14
  ### 1.0.0 - 2022-03-29
2
15
 
3
16
  * Add 开黑啦 HTTP API
data/README.md CHANGED
@@ -32,21 +32,27 @@ require "khl"
32
32
  token = "your_bot_token"
33
33
 
34
34
  # Call HTTP API
35
- http_client = KHL::HTTP::Client.new(token)
35
+ http_client = KHL::HTTP::Client.new(token: token)
36
36
  http_client.guild.list # Call guild/list API
37
37
 
38
38
  # Connect to WebSocket API
39
- ws_client = KHL::WebSocket::Client.new(token)
39
+ 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, update the version number in `version.rb`, 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).
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
@@ -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["data"]
19
+ @body[:data]
17
20
  end
18
21
 
19
22
  # The code in the response body
20
23
  def code
21
- @body["code"]
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["message"]
56
+ @body[:message]
54
57
  end
55
58
 
56
59
  def page
57
- data.dig("meta", "page") || 0
60
+ data.dig(:meta, :page) || 0
58
61
  end
59
62
 
60
63
  def page_size
61
- data.dig("meta", "page_size") || 0
64
+ data.dig(:meta, :page_size) || 0
62
65
  end
63
66
 
64
67
  def page_total
65
- data.dig("meta", "total") || 0
68
+ data.dig(:meta, :total) || 0
66
69
  end
67
70
  end
68
71
  end
@@ -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 = JSON.parse(raw)
26
+ data = ActiveSupport::HashWithIndifferentAccess.new(data)
27
+
28
+ message = Message.new
29
+ message.type = TYPES[data[:s]]
30
+ message.data = data[:d]
31
+ message.sn = data[:sn]
32
+ message
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module KHL
4
- VERSION = "1.0.0"
4
+ VERSION = "1.2.0"
5
5
  end
@@ -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
@@ -19,11 +19,12 @@ module KHL
19
19
  class Client
20
20
  attr_reader :config, :url, :messages, :state
21
21
 
22
- # @param config [String] :token Bot token
22
+ # @param config [Hash] :config Config
23
+ # @option config [String] :token Bot token (required)
23
24
  # @option config [String] :token_type Token type
24
25
  # @option config [String] :language Language
25
26
  # @option config [Boolean] :compress Compress
26
- def initialize(config)
27
+ def initialize(config = {})
27
28
  config[:compress] ||= false
28
29
 
29
30
  @config = config
@@ -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
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "khl/http/client"
4
+ require_relative "khl/webhook"
4
5
  require_relative "khl/web_socket/client"
5
6
 
6
7
  module KHL; end
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.0.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dounx
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-28 00:00:00.000000000 Z
11
+ date: 2022-04-08 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/web_socket/message.rb
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.2.33
106
+ rubygems_version: 3.3.10
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