Class: AntigateApi::Client
- Inherits:
-
Object
- Object
- AntigateApi::Client
- Defined in:
- lib/antigate_api/client.rb
Constant Summary collapse
- DEFAULT_CONFIGS =
{ recognition_time: 5, # First waiting time sleep_time: 1, # Sleep time for every check interval timeout: 60, # Max time out for decoding captcha debug: false # Verborse or not }
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #decode(captcha_file) ⇒ Object
- #get_captcha_text(id) ⇒ Object
-
#initialize(key, opts = {}) ⇒ Client
constructor
A new instance of Client.
- #report_bad(id) ⇒ Object
- #send_captcha(captcha_file) ⇒ Object
Constructor Details
#initialize(key, opts = {}) ⇒ Client
Returns a new instance of Client.
16 17 18 19 |
# File 'lib/antigate_api/client.rb', line 16 def initialize(key, opts={}) @key = key @options = DEFAULT_CONFIGS.merge(opts) end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
6 7 8 |
# File 'lib/antigate_api/client.rb', line 6 def key @key end |
#options ⇒ Object
Returns the value of attribute options.
7 8 9 |
# File 'lib/antigate_api/client.rb', line 7 def @options end |
Instance Method Details
#decode(captcha_file) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# 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) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# 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) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# 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) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# 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 |