Module: DeathByCaptcha

Defined in:
lib/death_by_captcha.rb,
lib/death_by_captcha/version.rb,
lib/death_by_captcha/recaptcha.rb,
lib/death_by_captcha/exceptions.rb

Defined Under Namespace

Classes: CaptchaDecodeFailed, CaptchaNotFound, CatchableError, Recaptcha, RequestError, ServiceOverloadedError

Constant Summary collapse

Server =
"api.dbcapi.me"
VERSION =
"0.4.2.1"
@@username =
nil
@@password =
nil
@@debug =
false
@@decode_max_tries =
10
@@decode_sleep_interval =
8

Class Method Summary collapse

Class Method Details

.account_balanceObject



76
77
78
79
80
81
82
# File 'lib/death_by_captcha.rb', line 76

def 
  req = Net::HTTP::Post.new( "/api/user" )
  req["Accept"] = "application/json"
  req.set_form_data( :username => @@username, :password => @@password )
  result = send_request( req )
  return JSON.parse( result.body )
end

.check(id) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/death_by_captcha.rb', line 54

def check( id )
  req = Net::HTTP::Get.new( "/api/captcha/#{id}" )
  req["Accept"] = "application/json"
  result = self.send_request( req )
  
  case result.code
  when "200"
    return JSON.parse( result.body )
  when "404"
    raise CaptchaNotFound
  else
    raise RequestError.new( result.code )
  end
end

.debug=(v) ⇒ Object



92
93
94
# File 'lib/death_by_captcha.rb', line 92

def debug=( v )
  @@debug = v
end

.decode(data) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/death_by_captcha.rb', line 18

def decode( data )
  req = Net::HTTP::Post.new( "/api/captcha" )
  req.set_form_data( :username => @@username, :password => @@password, :captchafile => "base64:" + Base64.encode64( data ) )
  result = send_request( req )
  
  case result.code
  when "303"
    uri = URI.parse( result["Location"] )
    return uri.path.split("/").last
  when "503"
    raise ServiceOverloadedError
  else
    raise RequestError.new( result.code )
  end
end

.decode!(data) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/death_by_captcha.rb', line 34

def decode!( data )
  captcha_status = nil
  captcha_id = decode( data )
  
  try = 0
  begin
    raise CaptchaDecodeFailed if try > @@decode_max_tries
    sleep @@decode_sleep_interval
    try += 1
    
    begin
      captcha_status = DeathByCaptcha.check( captcha_id )
    rescue RequestError, CaptchaNotFound
      captcha_status = { "text" => "" }
    end
  end while captcha_status["text"].strip == "" && captcha_status["is_correct"] == true
  
  return captcha_status
end

.decode_max_tries=(n) ⇒ Object



96
97
98
# File 'lib/death_by_captcha.rb', line 96

def decode_max_tries=( n )
  @@decode_max_tries = n
end

.decode_sleep_interval=(n) ⇒ Object



100
101
102
# File 'lib/death_by_captcha.rb', line 100

def decode_sleep_interval=( n )
  @@decode_sleep_interval = n
end

.password=(str) ⇒ Object



88
89
90
# File 'lib/death_by_captcha.rb', line 88

def password=( str )
  @@password = str
end

.report(id) ⇒ Object



69
70
71
72
73
74
# File 'lib/death_by_captcha.rb', line 69

def report( id )
  req = Net::HTTP::Post.new( "/api/captcha/#{id}/report" )
  req.set_form_data( :username => @@username, :password => @@password )
  result = send_request( req )
  return true
end

.send_request(req) ⇒ Object



104
105
106
107
108
# File 'lib/death_by_captcha.rb', line 104

def send_request( req )
  http = Net::HTTP.new( Server, 80 )
  http.set_debug_output( $stdout ) if @@debug == true
  return http.request( req )
end

.username=(str) ⇒ Object



84
85
86
# File 'lib/death_by_captcha.rb', line 84

def username=( str )
  @@username = str
end