Class: ApiSolveCaptcha::Client

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

Constant Summary collapse

DEFAULT_DOMAIN =
"api.solvecaptcha.com"
BASE_URL_FORMAT =
"https://%s"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, callback = nil) ⇒ Client

Returns a new instance of Client.



18
19
20
21
22
23
24
25
26
# File 'lib/api_solvecaptcha/client.rb', line 18

def initialize(api_key, callback = nil)
  @api_key = api_key
  @soft_id = nil
  @callback = callback
  @default_timeout = 120
  @recaptcha_timeout = 600
  @polling_interval = 5
  @domain = DEFAULT_DOMAIN
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



14
15
16
# File 'lib/api_solvecaptcha/client.rb', line 14

def api_key
  @api_key
end

#callbackObject

Returns the value of attribute callback.



14
15
16
# File 'lib/api_solvecaptcha/client.rb', line 14

def callback
  @callback
end

#default_timeoutObject

Returns the value of attribute default_timeout.



14
15
16
# File 'lib/api_solvecaptcha/client.rb', line 14

def default_timeout
  @default_timeout
end

#domainObject

Returns the value of attribute domain.



14
15
16
# File 'lib/api_solvecaptcha/client.rb', line 14

def domain
  @domain
end

#polling_intervalObject

Returns the value of attribute polling_interval.



14
15
16
# File 'lib/api_solvecaptcha/client.rb', line 14

def polling_interval
  @polling_interval
end

#recaptcha_timeoutObject

Returns the value of attribute recaptcha_timeout.



14
15
16
# File 'lib/api_solvecaptcha/client.rb', line 14

def recaptcha_timeout
  @recaptcha_timeout
end

#soft_idObject

Returns the value of attribute soft_id.



14
15
16
# File 'lib/api_solvecaptcha/client.rb', line 14

def soft_id
  @soft_id
end

Instance Method Details

#get_result(captcha_id) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/api_solvecaptcha/client.rb', line 46

def get_result(captcha_id)
  uri = URI("#{base_url}/res.php?key=#{@api_key}&action=get&id=#{captcha_id}&json=1")
  start_time = Time.now

  loop do
    response = make_request(uri)

    if response.is_a?(Net::HTTPSuccess)
      response_json = JSON.parse(response.body.strip)

      case response_json["request"]
      when "CAPCHA_NOT_READY"
        sleep @polling_interval
      when /[a-zA-Z0-9]+/
        return response_json
      else
        raise "API Error: #{response_json["request"]}"
      end
    else
      raise "Network Error: #{response.code}"
    end

    raise "Timeout" if Time.now - start_time > @default_timeout
  end
end

#normal(params) ⇒ Object



42
43
44
# File 'lib/api_solvecaptcha/client.rb', line 42

def normal(params)
  solve("post", **params)
end

#report(captcha_id, is_correct) ⇒ Object



72
73
74
75
76
# File 'lib/api_solvecaptcha/client.rb', line 72

def report(captcha_id, is_correct)
  action = is_correct ? "reportgood" : "reportbad"
  uri = URI("#{base_url}/res.php?key=#{@api_key}&action=#{action}&id=#{captcha_id}")
  make_request(uri)
end

#solve(method, return_id: false, **params) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/api_solvecaptcha/client.rb', line 28

def solve(method, return_id: false, **params)
  params["method"] = method
  params["key"] = @api_key
  params["json"] = 1

  params["pingback"] = @callback if @callback

  complete_params = get_params(params)
  captcha_id = send_request(complete_params)
  return captcha_id if return_id

  get_result(captcha_id)
end