khl 1.1.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 +4 -4
 - data/CHANGELOG.md +5 -2
 - data/README.md +6 -7
 - data/lib/khl/version.rb +1 -1
 - data/lib/khl/webhook.rb +31 -0
 - data/lib/khl.rb +1 -1
 - metadata +4 -4
 - data/lib/khl/webhook/client.rb +0 -98
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA256:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 3110943f00d66a4139d82f1f13798cde8f4ffddc0462cffeb3c47c5e131a2b47
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: bc8cb50c6876fbbc769667525f84bcbda3c2a9aa1d1e76bfdb00a2525a443ea8
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 8b4be1d65e2cb54e3d1b1a762253bc2fa3578bf448b056d9905edc7a0823c216802a857122b6904b93484f016585b2c5d5e314500b38ad0839765526ac6f0a43
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 99916625ffb6cb60f9613785167cae2209ad6e0f9f61af09a2f22e4d84605d4041aefaf0d6630612bb45adc22f6cb01881e13debe15cb1be5b4701596e36d480
         
     | 
    
        data/CHANGELOG.md
    CHANGED
    
    | 
         @@ -1,3 +1,7 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            ### 1.2.0 - 2022-04-08
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            * Fix 开黑啦 Webhook API
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
       1 
5 
     | 
    
         
             
            ### 1.1.0 - 2022-04-02
         
     | 
| 
       2 
6 
     | 
    
         | 
| 
       3 
7 
     | 
    
         
             
            * Add 开黑啦 Webhook API
         
     | 
| 
         @@ -7,9 +11,8 @@ 
     | 
|
| 
       7 
11 
     | 
    
         | 
| 
       8 
12 
     | 
    
         
             
            * Fix params type
         
     | 
| 
       9 
13 
     | 
    
         | 
| 
       10 
     | 
    
         
            -
             
     | 
| 
       11 
14 
     | 
    
         
             
            ### 1.0.0 - 2022-03-29
         
     | 
| 
       12 
15 
     | 
    
         | 
| 
       13 
16 
     | 
    
         
             
            * Add 开黑啦 HTTP API
         
     | 
| 
       14 
17 
     | 
    
         
             
            * Add 开黑啦 WebSocket API
         
     | 
| 
       15 
     | 
    
         
            -
            * Add GitHub CI
         
     | 
| 
      
 18 
     | 
    
         
            +
            * 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 
     | 
    
         
            -
             
     | 
| 
       46 
     | 
    
         
            -
             
     | 
| 
       47 
     | 
    
         
            -
             
     | 
| 
       48 
     | 
    
         
            -
             
     | 
| 
       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,  
     | 
| 
      
 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/version.rb
    CHANGED
    
    
    
        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.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- 
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2022-04-08 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies:
         
     | 
| 
       13 
13 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       14 
14 
     | 
    
         
             
              name: activesupport
         
     | 
| 
         @@ -79,7 +79,7 @@ files: 
     | 
|
| 
       79 
79 
     | 
    
         
             
            - lib/khl/message.rb
         
     | 
| 
       80 
80 
     | 
    
         
             
            - lib/khl/version.rb
         
     | 
| 
       81 
81 
     | 
    
         
             
            - lib/khl/web_socket/client.rb
         
     | 
| 
       82 
     | 
    
         
            -
            - lib/khl/webhook 
     | 
| 
      
 82 
     | 
    
         
            +
            - lib/khl/webhook.rb
         
     | 
| 
       83 
83 
     | 
    
         
             
            homepage: https://github.com/DessertShop/KHL
         
     | 
| 
       84 
84 
     | 
    
         
             
            licenses:
         
     | 
| 
       85 
85 
     | 
    
         
             
            - MIT
         
     | 
| 
         @@ -103,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement 
     | 
|
| 
       103 
103 
     | 
    
         
             
                - !ruby/object:Gem::Version
         
     | 
| 
       104 
104 
     | 
    
         
             
                  version: '0'
         
     | 
| 
       105 
105 
     | 
    
         
             
            requirements: []
         
     | 
| 
       106 
     | 
    
         
            -
            rubygems_version: 3. 
     | 
| 
      
 106 
     | 
    
         
            +
            rubygems_version: 3.3.10
         
     | 
| 
       107 
107 
     | 
    
         
             
            signing_key:
         
     | 
| 
       108 
108 
     | 
    
         
             
            specification_version: 4
         
     | 
| 
       109 
109 
     | 
    
         
             
            summary: Ruby SDK for 开黑啦
         
     | 
    
        data/lib/khl/webhook/client.rb
    DELETED
    
    | 
         @@ -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
         
     |