Class: Decaptcha::Provider::DeathByCaptcha::HTTP

Inherits:
Base
  • Object
show all
Defined in:
lib/decaptcha/provider/death_by_captcha/http.rb,
lib/decaptcha/provider/death_by_captcha/http_old.rb

Constant Summary collapse

API_SERVER_URL =
'http://www.deathbycaptcha.com/api/captcha'
API_VERSION =
'DBC/Ruby v3.0'
SOFTWARE_VENDOR_ID =
0
DEFAULT_TIMEOUT =
60
POLLS_PERIOD =
15

Instance Attribute Summary

Attributes inherited from Base

#login, #password, #timeout

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods inherited from Base

#mime_type

Constructor Details

This class inherits a constructor from Decaptcha::Provider::DeathByCaptcha::Base

Instance Method Details

#delete(response_obj) ⇒ Object


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/decaptcha/provider/death_by_captcha/http.rb', line 68

def delete(response_obj)
  params = {
    :username    => @login, 
    :password    => Digest::SHA1.hexdigest(@password),
    :is_hashed   => 1,
    :swid        => SOFTWARE_VENDOR_ID,
    :version     => API_VERSION
  }
  
  delete_url = "#{response_obj.location}/remove"
  
  RestClient.post delete_url, params, :accept => :json do |response, request, result|
    if response.code == 200
      response_obj.deleted = true
    else
      raise DeletionError
    end
  end
  
  response_obj
end

#invalid(response_obj) ⇒ Object


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/decaptcha/provider/death_by_captcha/http.rb', line 90

def invalid(response_obj)
  params = {
    :username    => @login, 
    :password    => Digest::SHA1.hexdigest(@password),
    :is_hashed   => 1,
    :swid        => SOFTWARE_VENDOR_ID,
    :version     => API_VERSION
  }
  
  invalidation_url = "#{response_obj.location}/report"
  
  RestClient.post invalidation_url, params, :accept => :json do |response, request, result|
    if response.code == 200
      response_obj.invalid = true
    else
      raise InvalidationError, response
    end
  end
  
  response_obj
end

#remove(cid) ⇒ Object


70
71
# File 'lib/decaptcha/provider/death_by_captcha/http_old.rb', line 70

def remove(cid)
end

#result(response_obj) ⇒ Object


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

def result(response_obj)
  RestClient.get response_obj.location, :accept => :json do |response, request, result|
    if response.code == 200
      data = JSON.parse response
      response_obj.text    = data['text']
      response_obj.invalid = !data['is_correct']
      return response_obj
    else
      raise ResultError
    end
  end
end

#scan(cid) ⇒ Object


61
62
63
64
65
66
67
68
# File 'lib/decaptcha/provider/death_by_captcha/http_old.rb', line 61

def scan(cid)
   request = Net::HTTP::Post.new url.path, 
      'username'    => @login, 
      'password'    => Digest::SHA1.hexdigest(@password),
      'is_hashed'   => 1,
      'swid'        => SOFTWARE_VENDOR_ID,
      'version'     => API_VERSION
end

#upload(args = {}) ⇒ Object


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/decaptcha/provider/death_by_captcha/http.rb', line 16

def upload(args = {})
  filename = args[:filename]

  params = {
    :captchafile => File.new(filename, 'rb'),
    :username    => @login, 
    :password    => Digest::SHA1.hexdigest(@password),
    :is_hashed   => 1,
    :swid        => SOFTWARE_VENDOR_ID,
    :version     => API_VERSION
  }
  
  RestClient.post API_SERVER_URL, params, :accept => :json do |response, request, result|
    if response.code == 303
      return Decaptcha::Response.new_from_location response.headers[:location]
    else
      raise UploadError
    end
  end
end

#upload_and_poll(args = {}) ⇒ Object


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/decaptcha/provider/death_by_captcha/http.rb', line 37

def upload_and_poll(args = {})
  user_timeout = args[:timeout] || DEFAULT_TIMEOUT
  response = upload(args)
  kill_time = Time.now + user_timeout
  
  while (Time.now < kill_time) do
    if result = result(response)
      break
    else
      sleep POLLS_PERIOD
    end
  end
  
  # remove...
  
  result
end