khl 1.1.0 → 1.3.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
2
  SHA256:
3
- metadata.gz: 9299dfafd8b0c827311f73bc349ef23f8e8d6e2c87a664dc94c52c8b8fee74de
4
- data.tar.gz: 2089da69756876c4d9f5e2afaebdc92e71c381e2fbf6c6f09d50fdac3a476817
3
+ metadata.gz: 8c30c3cd4f5ee8a0651ab2d9d54d72aececf1b9e5dba9fc9436404c5a8dcb313
4
+ data.tar.gz: 686559adab9d55c78d98c141e9a1210600eefaa004db809418e073e10386dda2
5
5
  SHA512:
6
- metadata.gz: f1f1607a954fd9ff6f3d0516d79cb79502db44b7c675cc45d68ff4f1c4bcec264f5872b3b7c6b8fceda363787bcf298932a561e5aab39e7e4da401c5ade753a4
7
- data.tar.gz: 974ebd1222e12470b06effe666df2aa522c93e53061f4bb8f540826bc2076c9e43b54db4a37245381bb653f7300979b6f9f82f7af6040a7b4f1b64c597e2dfd1
6
+ metadata.gz: 66eef5f3b1ffc6f2c5918071b2d4185622c3eb0f1592e18ea27d0033d86b4c5cf32ce9de32f77dceb32620399c081011469f37ad1d2342a09a6c4b0dcdd8d171
7
+ data.tar.gz: 665b6eeee9390e920fde7438482dfea57dc09b227c3095be2a1a1e0313989169ff08257ebf9332dd10c5db2a02786e25a6bc42904168cc7ae2bbbb49b2d0bbe9
data/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ ### 1.3.0 - 2022-04-18
2
+
3
+ * Remove KHL::Event
4
+
5
+ ### 1.2.1 - 2022-04-18
6
+
7
+ * Add hash support for KHL::Message.parse
8
+
9
+ ### 1.2.0 - 2022-04-08
10
+
11
+ * Fix 开黑啦 Webhook API
12
+
1
13
  ### 1.1.0 - 2022-04-02
2
14
 
3
15
  * Add 开黑啦 Webhook API
@@ -7,9 +19,8 @@
7
19
 
8
20
  * Fix params type
9
21
 
10
-
11
22
  ### 1.0.0 - 2022-03-29
12
23
 
13
24
  * Add 开黑啦 HTTP API
14
25
  * Add 开黑啦 WebSocket API
15
- * Add GitHub CI
26
+ * Add GitHub CI
data/README.md CHANGED
@@ -41,19 +41,18 @@ 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
43
 
44
- # Use Webhook API
45
- webhook_client = KHL::Webhook::Client.new("webhook_url", "challenge_token")
46
- webhook_client.online? # => false
47
- webhook_client.challenge # Do challenge
48
- webhook_client.online? # => true
49
- webhook_client.parse_message("data from webhook")
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)
50
49
  ```
51
50
 
52
51
  ## Development
53
52
 
54
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.
55
54
 
56
- 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).
57
56
 
58
57
  ## Contributing
59
58
 
data/lib/khl/message.rb CHANGED
@@ -3,8 +3,6 @@
3
3
  require "active_support/core_ext/hash/indifferent_access"
4
4
  require "active_support/hash_with_indifferent_access"
5
5
 
6
- require_relative "event"
7
-
8
6
  module KHL
9
7
  class Message
10
8
  TYPES = %i[
@@ -22,14 +20,14 @@ module KHL
22
20
  def self.parse(raw)
23
21
  return unless raw
24
22
 
25
- data = JSON.parse(raw)
23
+ data = raw.is_a?(String) ? JSON.parse(raw) : raw
26
24
  data = ActiveSupport::HashWithIndifferentAccess.new(data)
27
25
 
28
- message = Message.new
29
- message.type = TYPES[data[:s]]
30
- message.data = data[:d]
31
- message.sn = data[:sn]
32
- message
26
+ Message.new(
27
+ type: TYPES[data[:s]],
28
+ data: data[:d],
29
+ sn: data[:sn]
30
+ )
33
31
  end
34
32
 
35
33
  def initialize(type: nil, data: nil, sn: nil)
@@ -38,12 +36,6 @@ module KHL
38
36
  @sn = sn
39
37
  end
40
38
 
41
- def event
42
- return unless event?
43
-
44
- Event.new(@data)
45
- end
46
-
47
39
  def to_h
48
40
  ActiveSupport::HashWithIndifferentAccess.new(
49
41
  s: TYPES.index(type),
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.1.0"
4
+ VERSION = "1.3.0"
5
5
  end
@@ -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,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "khl/http/client"
4
- require_relative "khl/webhook/client"
4
+ require_relative "khl/webhook"
5
5
  require_relative "khl/web_socket/client"
6
6
 
7
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.1.0
4
+ version: 1.3.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-04-02 00:00:00.000000000 Z
11
+ date: 2022-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -54,7 +54,6 @@ files:
54
54
  - CHANGELOG.md
55
55
  - README.md
56
56
  - lib/khl.rb
57
- - lib/khl/event.rb
58
57
  - lib/khl/http/asset.rb
59
58
  - lib/khl/http/badge.rb
60
59
  - lib/khl/http/base.rb
@@ -79,7 +78,7 @@ files:
79
78
  - lib/khl/message.rb
80
79
  - lib/khl/version.rb
81
80
  - lib/khl/web_socket/client.rb
82
- - lib/khl/webhook/client.rb
81
+ - lib/khl/webhook.rb
83
82
  homepage: https://github.com/DessertShop/KHL
84
83
  licenses:
85
84
  - MIT
@@ -103,7 +102,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
102
  - !ruby/object:Gem::Version
104
103
  version: '0'
105
104
  requirements: []
106
- rubygems_version: 3.2.33
105
+ rubygems_version: 3.3.11
107
106
  signing_key:
108
107
  specification_version: 4
109
108
  summary: Ruby SDK for 开黑啦
data/lib/khl/event.rb DELETED
@@ -1,24 +0,0 @@
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,98 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module KHL
4
- module Webhook
5
- # Client for the KHL Webhook API
6
- # @example
7
- # client = KHL::Webhook::Client.new("https://foo.bar/callback", "your_challenge_token")
8
- # client.online? # => false
9
- # client.challenge # => true
10
- # client.online? # => true
11
- # data = client.parse_message(data_from_webhook)
12
- class Client
13
- attr_reader :key
14
-
15
- # @param [String] challenge_url The source url from the request
16
- # @param [String] challenge_token The challenge param from the request
17
- # @param [Hash] options Additional options
18
- # @option options [Boolean] :compress (true) Enable/disable compression
19
- # @option options [encrypt] :encrypt (false) Enable/disable encryption
20
- # @option options [String] :key (nil) Encryption key
21
- def initialize(challenge_url, challenge_token, options = {})
22
- @challenge_url = challenge_url
23
- @challenge_token = challenge_token
24
-
25
- @compress = options[:compress] || true
26
- @encrypt = options[:encrypt] || false
27
- @key = options[:key]
28
-
29
- raise ArgumentError, "key is required" if @encrypt && @key.nil?
30
- end
31
-
32
- # Do the challenge
33
- # @return [Boolean] The challenge success state
34
- def challenge
35
- uri = URI.parse(@challenge_url)
36
- res = Net::HTTP.post_form(uri, challenge: @challenge_token)
37
- if res.code == "200"
38
- @online = true
39
- return true
40
- end
41
-
42
- false
43
- end
44
-
45
- def compress?
46
- @compress
47
- end
48
-
49
- def encrypt?
50
- @encrypt
51
- end
52
-
53
- def online?
54
- @online || false
55
- end
56
-
57
- # Parse message from raw data
58
- # @param [String] data The raw data from the webhook
59
- # @return [KHL::Message] The parsed message
60
- def parse_message(data)
61
- if encrypt?
62
- data = decrypt(data)
63
- end
64
-
65
- if compress?
66
- data = decompress(data)
67
- end
68
-
69
- Message.parse(data)
70
- end
71
-
72
- private
73
-
74
- # Decode the data
75
- # @param [String] encrypted_data Encrypted data
76
- # @return [String] Decrypted data
77
- def decode(encrypted_data)
78
- return nil unless encrypt?
79
-
80
- encrypted_data = Base64.strict_decode64(encrypted_data)
81
- cipher = OpenSSL::Cipher.new("aes-256-cbc")
82
- cipher.decrypt
83
- cipher.iv = encrypted_data[0..15]
84
- cipher.key = key.ljust(32, "\0")
85
- cipher.update(encrypted_data) + cipher.final
86
- end
87
-
88
- # Decompress the data
89
- # @param [String] data Compressed data
90
- # @return [String] Decompressed data
91
- def decompress(data)
92
- return nil unless compress?
93
-
94
- Zlib::Inflate.inflate(data.pack("C*"))
95
- end
96
- end
97
- end
98
- end