Class: EasyCaptchaSolver

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ EasyCaptchaSolver

Returns a new instance of EasyCaptchaSolver.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/easy_captcha_solver.rb', line 7

def initialize ( options = {} )
  # Mechanize initializacion, pretends to be Mac Safari
  agent = Mechanize.new { |agent|
    agent.user_agent_alias = 'Mac Safari'
  }

  @image = options[:image_path] if options[:image_path]

  # If URL, save a file instead of trying to solve the captcha from memory because of tesseract limitations with .png images
  if options[:image_url]
    image = agent.get(options[:image_url]).save!  "./tmp_image"

    # Guess image extension and rename tmp file
    @image  = "./tmp_image.#{get_image_extension(image)}"
    File.rename( image,  @image)
  end

  unless @image
    throw Exception.new "A local image path or a image URL must be provided. Example:  easy_c = EasyCaptcha.new( image_url: 'http://www.example.com/captcha_img.jpg')"
  end

  solve_captcha ensure File.delete(@image) if options[:image_url]

end

Instance Attribute Details

#captchaObject (readonly)

Returns the value of attribute captcha.



5
6
7
# File 'lib/easy_captcha_solver.rb', line 5

def captcha
  @captcha
end