Class: GtRubySdk::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/gt_ruby_sdk.rb

Constant Summary collapse

RESPONSE_STATUS =
{
  success: 1,
  failure: 0
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(captcha_id, private_key) ⇒ Base

构造函数



15
16
17
18
# File 'lib/gt_ruby_sdk.rb', line 15

def initialize(captcha_id, private_key)
  @captcha_id = captcha_id
  @private_key = private_key
end

Instance Attribute Details

#captcha_idObject (readonly)

Returns the value of attribute captcha_id.



7
8
9
# File 'lib/gt_ruby_sdk.rb', line 7

def captcha_id
  @captcha_id
end

#private_keyObject (readonly)

Returns the value of attribute private_key.



7
8
9
# File 'lib/gt_ruby_sdk.rb', line 7

def private_key
  @private_key
end

#responseObject (readonly)

Returns the value of attribute response.



7
8
9
# File 'lib/gt_ruby_sdk.rb', line 7

def response
  @response
end

Instance Method Details

#local_validate(challenge, pin_code, seccode) ⇒ Object

极验服务器状态宕机的二次验证接口



53
54
55
# File 'lib/gt_ruby_sdk.rb', line 53

def local_validate(challenge, pin_code, seccode)
  GtRubySdk::LocalServer.validate(challenge, pin_code, seccode)
end

#pretreat(user_id = nil) ⇒ Object

预处理接口: 判断极验服务器是否down机



21
22
23
24
25
26
27
28
29
# File 'lib/gt_ruby_sdk.rb', line 21

def pretreat(user_id = nil)
  challenge = GtRubySdk::RemoteServer.register(captcha_id, user_id)

  return failed_pretreatment if challenge.size != 32

  successful_pretreatment(challenge)
rescue Faraday::Error::ClientError
  failed_pretreatment
end

#remote_validate(challenge, pin_code, seccode, user_id = nil) ⇒ Object

极验服务器状态正常的二次验证接口



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gt_ruby_sdk.rb', line 39

def remote_validate(challenge, pin_code, seccode, user_id = nil)
  return false unless pin_code_validated?(challenge, pin_code)

  data = {
    seccode: seccode,
    sdk: GtRubySdk::VERSION
  }

  data[:user_id] = user_id if user_id.present?

  GtRubySdk::RemoteServer.validate(data) == Digest::MD5.hexdigest(seccode)
end

#response_jsonObject

获取预处理结果的接口



32
33
34
35
36
# File 'lib/gt_ruby_sdk.rb', line 32

def response_json
  return if response.blank?

  JSON.generate(response)
end