class AntigateApi::Client

Constants

DEFAULT_CONFIGS

Attributes

key[R]
options[RW]

Public Class Methods

new(key, opts={}) click to toggle source
# File lib/antigate_api/client.rb, line 16
def initialize(key, opts={})
  @key = key
  @options = DEFAULT_CONFIGS.merge(opts)
end

Public Instance Methods

decode(captcha_file) click to toggle source
# File lib/antigate_api/client.rb, line 82
def decode(captcha_file)
  captcha_id = self.send_captcha(captcha_file)
  if ["OR_NO_SLOT_AVAILABLE"].include? captcha_id
    raise AntigateApi::Errors::Error("captcha_id: #{captcha_id}")
  end
  start_time = Time.now.to_i
  sleep @options[:recognition_time]

  code = nil
  while code == nil do
    code = self.get_captcha_text( captcha_id )
    duration = Time.now.to_i - start_time
    puts "Spent time: #{duration}" if @options[:debug]
    sleep @options[:sleep_time]
    raise AntigateApi::Errors::TimeoutError.new if duration > @options[:timeout]
  end

  [captcha_id, code]
end
get_captcha_text( id ) click to toggle source
# File lib/antigate_api/client.rb, line 41
def get_captcha_text( id )
  data = { :key => @key,
           :action => 'get',
           :id => id,
           :min_len => 5,
           :max_len => 5 }
  uri = URI.parse('http://antigate.com/res.php' )
  req = Net::HTTP::Post.new( uri.path )
  http = Net::HTTP.new( uri.host, uri.port )
  req.set_form_data( data )

  begin
    resp = http.request(req)
  rescue => err
    puts err
    return nil
  end

  text = resp.body
  if text != "CAPCHA_NOT_READY"
    return text[ 3..text.size ]
  end
  nil
end
report_bad( id ) click to toggle source
# File lib/antigate_api/client.rb, line 66
def report_bad( id )
  data = { :key => @key,
           :action => 'reportbad',
           :id => id }
  uri = URI.parse('http://antigate.com/res.php' )
  req = Net::HTTP::Post.new( uri.path )
  http = Net::HTTP.new( uri.host, uri.port )
  req.set_form_data( data )

  begin
    resp = http.request(req)
  rescue => err
    puts err
  end
end
send_captcha( captcha_file ) click to toggle source
# File lib/antigate_api/client.rb, line 21
def send_captcha( captcha_file )
  uri = URI.parse( 'http://antigate.com/in.php' )
  file = File.new( captcha_file, 'rb' )
  req = Net::HTTP::Post::Multipart.new( uri.path,
                                       :method => 'post',
                                       :key => @key,
                                       :file => UploadIO.new( file, 'image/jpeg', 'image.jpg' ),
                                       :numeric => 0 )
  http = Net::HTTP.new( uri.host, uri.port )
  begin
    resp = http.request( req )
  rescue => err
    puts err
    return nil
  end

  id = resp.body
  id[ 3..id.size ]
end