Class: RussianPost::Captcha::Pattern

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

Constant Summary collapse

TARGET_MATCH_RATE =
1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(points, character) ⇒ Pattern

Returns a new instance of Pattern.



8
9
10
# File 'lib/russian_post/captcha/pattern.rb', line 8

def initialize(points, character)
  @points, @character = points, character
end

Instance Attribute Details

#characterObject (readonly)

Returns the value of attribute character.



6
7
8
# File 'lib/russian_post/captcha/pattern.rb', line 6

def character
  @character
end

#pointsObject (readonly)

Returns the value of attribute points.



6
7
8
# File 'lib/russian_post/captcha/pattern.rb', line 6

def points
  @points
end

Instance Method Details

#match?(image, x, y) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/russian_post/captcha/pattern.rb', line 12

def match?(image, x, y)
  matching = 0

  points.each do |px, py|
    cx, cy = x + px, y + py

    break if cx >= image.width || cy >= image.height || image[cx, cy] == 255

    matching += 1
  end

  matching / points.size >= TARGET_MATCH_RATE
end