class AntiCaptcha::Client

Public Class Methods

new(options = {}) click to toggle source
# File lib/anti-captcha/client.rb, line 8
def initialize(options = {})
  @retries_count = options.delete(:retries_count) || 10
  @sleep = options.delete(:sleep) || 5
  @options = AntiCaptcha.configuration.options.merge(options)
end

Public Instance Methods

check() click to toggle source
# File lib/anti-captcha/client.rb, line 28
def check
  return if @captcha_id.blank?
  begin
    get_status
  rescue CaptchaNotReady => e
    attempt ||= @retries_count
    raise e if (attempt -= 1) < 0
    sleep @sleep
    retry
  end
end
decode(image, type = :bin) click to toggle source

type: file data (bin) type: base64 (base64)

# File lib/anti-captcha/client.rb, line 16
def decode(image, type = :bin)
  case request_image(image, type)
  when /OK\|(.+)/
    @captcha_id = $1
    check
  when /^ERROR_(.+)/
    raise error_class($1)
  else
    raise UnknownResponse
  end
end
get_balance() click to toggle source
# File lib/anti-captcha/client.rb, line 45
def get_balance
  request_balance
end
get_stats(date = Date.today) click to toggle source
# File lib/anti-captcha/client.rb, line 49
def get_stats(date = Date.today)
  request_stats date
end
report_bad() click to toggle source
# File lib/anti-captcha/client.rb, line 40
def report_bad
  return if @captcha_id.blank?
  request_report_bad
end

Private Instance Methods

error_class(string) click to toggle source
# File lib/anti-captcha/client.rb, line 111
def error_class(string)
  "AntiCaptcha::#{string.downcase.classify}".constantize
rescue NameError
  UnknownErrorResponse
end
get_status() click to toggle source
# File lib/anti-captcha/client.rb, line 55
def get_status
  case request_status
  when /^OK\|(.+)/
    $1
  when 'CAPCHA_NOT_READY'
    raise CaptchaNotReady
  when /^ERROR_(.+)/
    raise error_class($1)
  else
    raise UnknownResponse
  end
end
request(url, params) click to toggle source
# File lib/anti-captcha/client.rb, line 103
def request(url, params)
  request = HTTPI::Request.new.tap do |request|
    request.url = url
    request.body = params
  end
  HTTPI.post(request).body
end
request_balance() click to toggle source
# File lib/anti-captcha/client.rb, line 90
def request_balance
  request 'http://anti-captcha.com/res.php',
    key: AntiCaptcha.configuration.key,
    action: 'getbalance'
end
request_image(image, type = :bin) click to toggle source

type: file data (bin) type: base64 (base64)

# File lib/anti-captcha/client.rb, line 77
def request_image(image, type = :bin)
  body = (type == :bin ? Base64.encode64(image) : image)
  request 'http://anti-captcha.com/in.php',
    @options.merge(method: 'base64', body: body)
end
request_report_bad() click to toggle source
# File lib/anti-captcha/client.rb, line 83
def request_report_bad
  request 'http://anti-captcha.com/res.php',
    key: AntiCaptcha.configuration.key,
    action: 'reportbad',
    id: @captcha_id
end
request_stats(date) click to toggle source
# File lib/anti-captcha/client.rb, line 96
def request_stats(date)
  request 'http://anti-captcha.com/res.php',
    key: AntiCaptcha.configuration.key,
    action: 'getstats',
    date: date.strftime('%Y-%m-%d')
end
request_status() click to toggle source
# File lib/anti-captcha/client.rb, line 68
def request_status
  request 'http://anti-captcha.com/res.php',
    key: AntiCaptcha.configuration.key,
    action: 'get',
    id: @captcha_id
end