Class: ZeroCaptcha::Client

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

Overview

ZeroCaptcha::Client is a client that communicates with the ZeroCaptcha API: zerocaptcha.infosimples.com/.

Constant Summary collapse

BASE_URL =
'https://zerocaptcha.infosimples.com/api/v1/captcha/:action.json'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, options = {}) ⇒ ZeroCaptcha::Client

Create a ZeroCaptcha API client.

Parameters:

  • token (String)

    Token of the ZeroCaptcha account.

  • options (Hash) (defaults to: {})

    Options hash.

Options Hash (options):

  • :timeout (Integer) — default: 60

    Seconds before giving up of a captcha being solved.



19
20
21
22
# File 'lib/zero_captcha/client.rb', line 19

def initialize(token, options = {})
  self.token      = token
  self.timeout    = options[:timeout] || 60
end

Instance Attribute Details

#timeoutObject

Returns the value of attribute timeout.



8
9
10
# File 'lib/zero_captcha/client.rb', line 8

def timeout
  @timeout
end

#tokenObject

Returns the value of attribute token.



8
9
10
# File 'lib/zero_captcha/client.rb', line 8

def token
  @token
end

Instance Method Details

#decode(options = {}) ⇒ ZeroCaptcha::Captcha

Decode the text from an image (i.e. solve a captcha).

Parameters:

  • options (Hash) (defaults to: {})

    Options hash.

Options Hash (options):

  • :url (String)

    URL of the image to be decoded.

  • :path (String)

    File path of the image to be decoded.

  • :file (File)

    File instance with image to be decoded.

  • :raw (String)

    Binary content of the image to be decoded.

  • :raw64 (String)

    Binary content encoded in base64 of the image to be decoded.

Returns:



38
39
40
41
42
# File 'lib/zero_captcha/client.rb', line 38

def decode(options = {})
  decode!(options)
rescue ZeroCaptcha::Error
  ZeroCaptcha::Captcha.new
end

#decode!(options = {}) ⇒ ZeroCaptcha::Captcha

Decode the text from an image (i.e. solve a captcha).

Parameters:

  • options (Hash) (defaults to: {})

    Options hash.

Options Hash (options):

  • :url (String)

    URL of the image to be decoded.

  • :path (String)

    File path of the image to be decoded.

  • :file (File)

    File instance with image to be decoded.

  • :raw (String)

    Binary content of the image to be decoded.

  • :raw64 (String)

    Binary content encoded in base64 of the image to be decoded.

Returns:



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/zero_captcha/client.rb', line 58

def decode!(options = {})
  raw64   = load_captcha(options)
  solver  = options[:solver]

  response = request(solver, :multipart, image64: raw64)
  captcha = ZeroCaptcha::Captcha.new(response)

  fail(ZeroCaptcha::IncorrectSolution) unless captcha.correct?

  captcha
end

#report_correct(id) ⇒ ZeroCaptcha::Captcha

Report correctly solved captcha for statistics.

Parameters:

  • id (Integer)

    Numeric ID of the captcha.

Returns:



87
88
89
90
# File 'lib/zero_captcha/client.rb', line 87

def report_correct(id)
  response = request('report_correct', :post, id: id)
  ZeroCaptcha::Captcha.new(response)
end

#report_incorrect(id) ⇒ ZeroCaptcha::Captcha

Report incorrectly solved captcha for refund.

Parameters:

  • id (Integer)

    Numeric ID of the captcha.

Returns:



76
77
78
79
# File 'lib/zero_captcha/client.rb', line 76

def report_incorrect(id)
  response = request('report_incorrect', :post, id: id)
  ZeroCaptcha::Captcha.new(response)
end