Class: ApiSolveCaptcha::Client
- Inherits:
-
Object
- Object
- ApiSolveCaptcha::Client
- Defined in:
- lib/api_solvecaptcha/client.rb
Constant Summary collapse
- DEFAULT_DOMAIN =
"api.solvecaptcha.com"- BASE_URL_FORMAT =
"https://%s"
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#callback ⇒ Object
Returns the value of attribute callback.
-
#default_timeout ⇒ Object
Returns the value of attribute default_timeout.
-
#domain ⇒ Object
Returns the value of attribute domain.
-
#polling_interval ⇒ Object
Returns the value of attribute polling_interval.
-
#recaptcha_timeout ⇒ Object
Returns the value of attribute recaptcha_timeout.
-
#soft_id ⇒ Object
Returns the value of attribute soft_id.
Instance Method Summary collapse
- #get_result(captcha_id) ⇒ Object
-
#initialize(api_key, callback = nil) ⇒ Client
constructor
A new instance of Client.
- #normal(params) ⇒ Object
- #report(captcha_id, is_correct) ⇒ Object
- #solve(method, return_id: false, **params) ⇒ Object
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_key ⇒ Object
Returns the value of attribute api_key.
14 15 16 |
# File 'lib/api_solvecaptcha/client.rb', line 14 def api_key @api_key end |
#callback ⇒ Object
Returns the value of attribute callback.
14 15 16 |
# File 'lib/api_solvecaptcha/client.rb', line 14 def callback @callback end |
#default_timeout ⇒ Object
Returns the value of attribute default_timeout.
14 15 16 |
# File 'lib/api_solvecaptcha/client.rb', line 14 def default_timeout @default_timeout end |
#domain ⇒ Object
Returns the value of attribute domain.
14 15 16 |
# File 'lib/api_solvecaptcha/client.rb', line 14 def domain @domain end |
#polling_interval ⇒ Object
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_timeout ⇒ Object
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_id ⇒ Object
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 |