Class: EFreeSMS::Captcha

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

Class Method Summary collapse

Class Method Details

.download(browser, dest) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/efreesms.rb', line 22

def self.download (browser, dest)
  File.open(dest, 'w') {|file|
    browser.get('http://www.e-freesms.com/captcha.php', {}, {'Referer' => 'http://www.e-freesms.com/sms.php'}) {|data|
      file.write(data)
    }
  }
end

.resolve(browser) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/efreesms.rb', line 30

def self.resolve (browser)
  ''.tap {|res|
    Dir.mktmpdir {|d|
      Dir.chdir(d) {|d|
        download(browser, File.join(File.realpath(d), 'captcha.jpg'))
        Magick::Image.read('captcha.jpg')[0].resize(130, 50).negate.despeckle.write('captcha.tif')
        system('tesseract', 'captcha.tif', 'captcha', 2 => '/dev/null')
        res.replace File.read('captcha.txt').strip
        %w{captcha.jpg captcha.tif captcha.txt}.each {|f|
          File.unlink(f)
        }
      }
    }
  }
end