Class: DeathByCaptcha::Recaptcha

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

Defined Under Namespace

Classes: CaptchaInvalid, ChallengeFieldMissing, ImageMissing, ResolveFailed, Response

Constant Summary collapse

@@default_options =
{ report: true }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site_id) ⇒ Recaptcha

Returns a new instance of Recaptcha.



13
14
15
# File 'lib/death_by_captcha/recaptcha.rb', line 13

def initialize( site_id )
  @site_id = site_id
end

Class Method Details

.default_optionsObject



9
10
11
# File 'lib/death_by_captcha/recaptcha.rb', line 9

def self.default_options
  @@default_options
end

Instance Method Details

#resolve(opts = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/death_by_captcha/recaptcha.rb', line 17

def resolve( opts = {} )
  options = @@default_options.merge( opts )
  response = nil

  begin
    request = Net::HTTP::Get.new( "/recaptcha/api/challenge?k=#{@site_id}" )
    result = send_request( request )
    matches = result.body.match( /challenge : '([^\']+)',/ )

    # get recaptcha_challenge_field value
    raise ChallengeFieldMissing if matches.nil?
    recaptcha_challenge_field = matches[1]

    # get image
    data = Net::HTTP.get( URI.parse( "http://www.google.com/recaptcha/api/image?c=#{recaptcha_challenge_field}" ) )
    captcha_status = DeathByCaptcha.decode!( data )
    code = captcha_status["text"]
  rescue CaptchaInvalid
    DeathByCaptcha.report( captcha_status["captcha"] ) if options[:report] == true
    raise CaptchaInvalid
  end
  
  return Response.new( captcha_status["text"], recaptcha_challenge_field )
end

#resolve_with_certification(opts = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/death_by_captcha/recaptcha.rb', line 42

def resolve_with_certification( opts = {} )
  options = @@default_options.merge( opts )

  begin
    request = Net::HTTP::Get.new( "/recaptcha/api/noscript?k=#{@site_id}" )
    result = send_request( request )
    doc = Nokogiri::HTML.parse( result.body )
  
    # get recaptcha_challenge_field value
    recaptcha_challenge_field_input = doc.xpath( "//input[@name='recaptcha_challenge_field']" ).first
    raise ChallengeFieldMissing if recaptcha_challenge_field_input.nil? || recaptcha_challenge_field_input.attributes["value"].nil?
  
    # get image
    image_node = doc.xpath( "//center/img" ).first
    raise ImageMissing if image_node.nil? || image_node.attributes["src"].nil?
    data = Net::HTTP.get( URI.parse( "http://www.google.com/recaptcha/api/#{image_node.attributes["src"].value}" ) )
    captcha_status = nil
    code = nil
    begin
      captcha_status = DeathByCaptcha.decode!( data )
      code = captcha_status["text"]
    rescue DeathByCaptcha::RequestError => e
      raise ResolveFailed, e.class.to_s
    rescue DeathByCaptcha::CaptchaDecodeFailed => e
      raise ResolveFailed, e.class.to_s
    end
  
    raise ResolveFailed, "no captcha" if code.nil?
  
    recaptcha_challenge_field = recaptcha_challenge_field_input.attributes["value"].value
    params = { recaptcha_challenge_field: recaptcha_challenge_field, recaptcha_response_field: code, submit: "Je ne suis pas un robot" }
  
    request = Net::HTTP::Post.new( "/recaptcha/api/noscript?k=#{@site_id}" )
    request.set_form_data( params )
    result = send_request( request )
    doc = Nokogiri::HTML.parse( result.body )
    textarea_node = doc.xpath( "//textarea" ).first
  
    raise CaptchaInvalid if textarea_node.nil?
  rescue CaptchaInvalid
    DeathByCaptcha.report( captcha_status["captcha"] ) if options[:report] == true
    raise CaptchaInvalid
  end
  
  return Response.new( captcha_status["text"], textarea_node.inner_text )
end