From 9c69e889afb49d8abb3017db58f1931fc6317b85 Mon Sep 17 00:00:00 2001 From: IChou Date: Wed, 5 Jul 2023 17:25:07 +0800 Subject: [PATCH] Update to be compatibled with ruby3 --- Gemfile | 1 - Gemfile.lock | 6 +----- lib/geetest/v3/account.rb | 13 +++++++++---- lib/geetest/v4/account.rb | 26 +++++++++++++++++--------- lib/geetest_ruby_sdk/version.rb | 2 +- 5 files changed, 28 insertions(+), 20 deletions(-) diff --git a/Gemfile b/Gemfile index 2da71ce..925af5c 100644 --- a/Gemfile +++ b/Gemfile @@ -4,7 +4,6 @@ source 'https://rubygems.org' gemspec gem 'logger' -gem 'openssl', '~> 2.2' gem 'rest-client' group :test do diff --git a/Gemfile.lock b/Gemfile.lock index 94cab12..806d618 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - geetest_ruby_sdk (4.0.3) + geetest_ruby_sdk (4.0.4) GEM remote: https://rubygems.org/ @@ -18,14 +18,11 @@ GEM http-accept (1.7.0) http-cookie (1.0.5) domain_name (~> 0.5) - ipaddr (1.2.4) logger (1.5.1) mime-types (3.4.1) mime-types-data (~> 3.2015) mime-types-data (3.2022.0105) netrc (0.11.0) - openssl (2.2.2) - ipaddr parallel (1.22.1) parser (3.1.2.1) ast (~> 2.4.1) @@ -83,7 +80,6 @@ DEPENDENCIES bundler (~> 2.0) geetest_ruby_sdk! logger - openssl (~> 2.2) rake (~> 12.0) rest-client rspec (~> 3.0) diff --git a/lib/geetest/v3/account.rb b/lib/geetest/v3/account.rb index 8c5fc37..66fd755 100644 --- a/lib/geetest/v3/account.rb +++ b/lib/geetest/v3/account.rb @@ -33,12 +33,17 @@ def initialize(captcha_id, captcha_key, **options) @digest_mod = options.fetch(:digest_mod, Account::DEFAULT_DIGEST_MOD) end - def register(**options) - Register.new(account: self).register(options) + def register(options = {}) + Register.new(account: self).register(**options) end - def validate?(challenge: nil, validate: nil, seccode: nil, **options) - Validator.new(account: self).valid?(challenge, validate, seccode, options) + def validate?(data = {}) + options = data.transform_keys(&:to_sym) + challenge = options.delete(:challenge) + validate = options.delete(:validate) + seccode = options.delete(:seccode) + + Validator.new(account: self).valid?(challenge, validate, seccode, **options) end end end diff --git a/lib/geetest/v4/account.rb b/lib/geetest/v4/account.rb index 096e5a3..630eef5 100644 --- a/lib/geetest/v4/account.rb +++ b/lib/geetest/v4/account.rb @@ -16,16 +16,10 @@ def initialize(captcha_id, captcha_key, **options) @api_server = URI.parse options.fetch(:api_server, DEFAULT_API_SERVER) end - def validate?(lot_number: nil, captcha_output: nil, pass_token: nil, gen_time: nil, **options) - return false unless lot_number && captcha_output && pass_token && gen_time + def validate?(data = {}) + payload, options = prepare_payload(data) + return false if payload.nil? - payload = { - lot_number: lot_number, - captcha_output: captcha_output, - pass_token: pass_token, - gen_time: gen_time, - sign_token: Encryptor.encrypt(lot_number).with(captcha_key).by(DIGEST_MOD).to_s - } response = request! payload, options Geetest.logger.debug "Geetest validate failed, reason: #{response['reason']}" if response['result'] != 'success' response['result'] == 'success' @@ -36,6 +30,20 @@ def validate?(lot_number: nil, captcha_output: nil, pass_token: nil, gen_time: n private + def prepare_payload(data) + options = data.transform_keys(&:to_sym) + payload = { + lot_number: options.delete(:lot_number), + captcha_output: options.delete(:captcha_output), + pass_token: options.delete(:pass_token), + gen_time: options.delete(:gen_time) + }.compact + return [nil, nil] unless payload.size == 4 + + payload[:sign_token] = Encryptor.encrypt(payload[:lot_number]).with(captcha_key).by(DIGEST_MOD).to_s + [payload, options] + end + def request!(payload, options = {}) response = RestClient.post "#{api_server}/validate?captcha_id=#{captcha_id}", options.merge(payload) JSON.parse response.body diff --git a/lib/geetest_ruby_sdk/version.rb b/lib/geetest_ruby_sdk/version.rb index 79a0933..449d677 100644 --- a/lib/geetest_ruby_sdk/version.rb +++ b/lib/geetest_ruby_sdk/version.rb @@ -1,3 +1,3 @@ module GeetestRubySdk - VERSION = '4.0.3' + VERSION = '4.0.4' end