Skip to content

Commit

Permalink
Update to be compatibled with ruby3
Browse files Browse the repository at this point in the history
  • Loading branch information
IChou committed Jul 5, 2023
1 parent df42a05 commit 9c69e88
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 20 deletions.
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ source 'https://rubygems.org'
gemspec

gem 'logger'
gem 'openssl', '~> 2.2'
gem 'rest-client'

group :test do
Expand Down
6 changes: 1 addition & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
geetest_ruby_sdk (4.0.3)
geetest_ruby_sdk (4.0.4)

GEM
remote: https://rubygems.org/
Expand All @@ -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)
Expand Down Expand Up @@ -83,7 +80,6 @@ DEPENDENCIES
bundler (~> 2.0)
geetest_ruby_sdk!
logger
openssl (~> 2.2)
rake (~> 12.0)
rest-client
rspec (~> 3.0)
Expand Down
13 changes: 9 additions & 4 deletions lib/geetest/v3/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 17 additions & 9 deletions lib/geetest/v4/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/geetest_ruby_sdk/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module GeetestRubySdk
VERSION = '4.0.3'
VERSION = '4.0.4'
end

0 comments on commit 9c69e88

Please sign in to comment.