Class: RussianPost::Captcha

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

Defined Under Namespace

Classes: Pattern, Patterns, RecognitionError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#patternsObject (readonly)

Returns the value of attribute patterns.



10
11
12
# File 'lib/russian_post/captcha.rb', line 10

def patterns
  @patterns
end

Class Method Details

.for_data(data, patterns = nil) ⇒ Object



18
19
20
# File 'lib/russian_post/captcha.rb', line 18

def for_data(data, patterns = nil)
  RussianPost::Captcha.new(nil, data, patterns)
end

.for_url(url, patterns = nil) ⇒ Object



14
15
16
# File 'lib/russian_post/captcha.rb', line 14

def for_url(url, patterns = nil)
  RussianPost::Captcha.new(url, nil, patterns)
end

Instance Method Details

#imageObject



24
25
26
# File 'lib/russian_post/captcha.rb', line 24

def image
  @data ||= fetch_image
end

#recognizeObject



36
37
38
# File 'lib/russian_post/captcha.rb', line 36

def recognize
  @recognize ||= recognize!
end

#recognize!Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/russian_post/captcha.rb', line 40

def recognize!
  captcha_image = grayscale(image)

  results = {}

  for x in 0..captcha_image.width - 2
    for y in 0..captcha_image.height - 2
      result = patterns.find(captcha_image, x, y)

      results[x] = result if result
    end
  end

  Hash[results.sort].values
end

#textObject



28
29
30
# File 'lib/russian_post/captcha.rb', line 28

def text
  @recognized_text ||= prepare_text(recognize)
end

#valid?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/russian_post/captcha.rb', line 32

def valid?
  text.size == 5
end