Class: DeathByCaptcha::Client
- Inherits:
-
Object
- Object
- DeathByCaptcha::Client
- Defined in:
- lib/deathbycaptcha/client.rb
Overview
DeathByCaptcha::Client is a common interface inherited by DBC clients like DeathByCaptcha::Client::HTTP and DeathByCaptcha::Client::Socket.
Defined Under Namespace
Instance Attribute Summary collapse
-
#hostname ⇒ Object
Returns the value of attribute hostname.
-
#password ⇒ Object
Returns the value of attribute password.
-
#polling ⇒ Object
Returns the value of attribute polling.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
-
#username ⇒ Object
Returns the value of attribute username.
Class Method Summary collapse
-
.create(username, password, connection = :socket, options = {}) ⇒ DeathByCaptcha::Client
Create a DeathByCaptcha API client.
Instance Method Summary collapse
-
#captcha(captcha_id) ⇒ DeathByCaptcha::Captcha
Retrieve information from an uploaded captcha.
-
#decode(options = {}) ⇒ DeathByCaptcha::Captcha
Decode the text from an image (i.e. solve a captcha).
-
#decode!(options = {}) ⇒ DeathByCaptcha::Captcha
Decode the text from an image (i.e. solve a captcha).
-
#initialize(username, password, options = {}) ⇒ DeathByCaptcha::Client
constructor
Create a DeathByCaptcha client.
-
#report!(captcha_id) ⇒ DeathByCaptcha::Captcha
Report incorrectly solved captcha for refund.
-
#status ⇒ DeathByCaptcha::ServerStatus
Retrieve DeathByCaptcha server status.
-
#upload(raw64) ⇒ DeathByCaptcha::Captcha
Upload a captcha to DeathByCaptcha.
-
#user ⇒ DeathByCaptcha::User
Retrieve your user information (which has the current credit balance).
Constructor Details
#initialize(username, password, options = {}) ⇒ DeathByCaptcha::Client
Create a DeathByCaptcha client.
50 51 52 53 54 55 56 |
# File 'lib/deathbycaptcha/client.rb', line 50 def initialize(username, password, = {}) self.username = username self.password = password self.timeout = [:timeout] || 60 self.polling = [:polling] || 5 self.hostname = [:hostname] || 'api.dbcapi.me' end |
Instance Attribute Details
#hostname ⇒ Object
Returns the value of attribute hostname.
15 16 17 |
# File 'lib/deathbycaptcha/client.rb', line 15 def hostname @hostname end |
#password ⇒ Object
Returns the value of attribute password.
15 16 17 |
# File 'lib/deathbycaptcha/client.rb', line 15 def password @password end |
#polling ⇒ Object
Returns the value of attribute polling.
15 16 17 |
# File 'lib/deathbycaptcha/client.rb', line 15 def polling @polling end |
#timeout ⇒ Object
Returns the value of attribute timeout.
15 16 17 |
# File 'lib/deathbycaptcha/client.rb', line 15 def timeout @timeout end |
#username ⇒ Object
Returns the value of attribute username.
15 16 17 |
# File 'lib/deathbycaptcha/client.rb', line 15 def username @username end |
Class Method Details
.create(username, password, connection = :socket, options = {}) ⇒ DeathByCaptcha::Client
Create a DeathByCaptcha API client
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/deathbycaptcha/client.rb', line 28 def self.create(username, password, connection = :socket, = {}) case connection when :socket DeathByCaptcha::Client::Socket.new(username, password, ) when :http DeathByCaptcha::Client::HTTP.new(username, password, ) else raise DeathByCaptcha::InvalidClientConnection end end |
Instance Method Details
#captcha(captcha_id) ⇒ DeathByCaptcha::Captcha
Retrieve information from an uploaded captcha.
115 116 117 |
# File 'lib/deathbycaptcha/client.rb', line 115 def captcha(captcha_id) raise NotImplementedError end |
#decode(options = {}) ⇒ DeathByCaptcha::Captcha
Decode the text from an image (i.e. solve a captcha).
69 70 71 72 73 |
# File 'lib/deathbycaptcha/client.rb', line 69 def decode( = {}) decode!() rescue DeathByCaptcha::Error DeathByCaptcha::Captcha.new end |
#decode!(options = {}) ⇒ DeathByCaptcha::Captcha
Decode the text from an image (i.e. solve a captcha).
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/deathbycaptcha/client.rb', line 86 def decode!( = {}) started_at = Time.now # don't load image data for Token API raw64 = nil unless [:type] == 4 raw64 = load_captcha() raise DeathByCaptcha::InvalidCaptcha if raw64.to_s.empty? end decoded_captcha = self.upload(.merge(raw64: raw64)) while decoded_captcha.text.to_s.empty? sleep(self.polling) decoded_captcha = self.captcha(decoded_captcha.id) raise DeathByCaptcha::Timeout if (Time.now - started_at) > self.timeout end raise DeathByCaptcha::IncorrectSolution if !decoded_captcha.is_correct decoded_captcha end |
#report!(captcha_id) ⇒ DeathByCaptcha::Captcha
Report incorrectly solved captcha for refund.
125 126 127 |
# File 'lib/deathbycaptcha/client.rb', line 125 def report!(captcha_id) raise NotImplementedError end |
#status ⇒ DeathByCaptcha::ServerStatus
Retrieve DeathByCaptcha server status.
141 142 143 |
# File 'lib/deathbycaptcha/client.rb', line 141 def status raise NotImplementedError end |
#upload(raw64) ⇒ DeathByCaptcha::Captcha
Upload a captcha to DeathByCaptcha.
This method will not return the solution. It’s only useful if you want to implement your own “decode” function.
152 153 154 |
# File 'lib/deathbycaptcha/client.rb', line 152 def upload(raw64) raise NotImplementedError end |
#user ⇒ DeathByCaptcha::User
Retrieve your user information (which has the current credit balance).
133 134 135 |
# File 'lib/deathbycaptcha/client.rb', line 133 def user raise NotImplementedError end |