Class: DeathByCaptcha::Client

Inherits:
Object
  • Object
show all
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.

Direct Known Subclasses

HTTP, Socket

Defined Under Namespace

Classes: HTTP, Socket

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, options = {}) ⇒ DeathByCaptcha::Client

Create a DeathByCaptcha client.

Parameters:

  • username (String)

    Username of the DeathByCaptcha account.

  • password (String)

    Password of the DeathByCaptcha account.

  • options (Hash) (defaults to: {})

    Options hash.

Options Hash (options):

  • :timeout (Integer) — default: 60

    Seconds before giving up.

  • :polling (Integer) — default: 5

    Seconds for polling the solution.

  • :hostname (String) — default: 'api.dbcapi.me'

    Custom API hostname.



50
51
52
53
54
55
56
# File 'lib/deathbycaptcha/client.rb', line 50

def initialize(username, password, options = {})
  self.username   = username
  self.password   = password
  self.timeout    = options[:timeout] || 60
  self.polling    = options[:polling] || 5
  self.hostname   = options[:hostname] || 'api.dbcapi.me'
end

Instance Attribute Details

#hostnameObject

Returns the value of attribute hostname.



15
16
17
# File 'lib/deathbycaptcha/client.rb', line 15

def hostname
  @hostname
end

#passwordObject

Returns the value of attribute password.



15
16
17
# File 'lib/deathbycaptcha/client.rb', line 15

def password
  @password
end

#pollingObject

Returns the value of attribute polling.



15
16
17
# File 'lib/deathbycaptcha/client.rb', line 15

def polling
  @polling
end

#timeoutObject

Returns the value of attribute timeout.



15
16
17
# File 'lib/deathbycaptcha/client.rb', line 15

def timeout
  @timeout
end

#usernameObject

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

Parameters:

  • username (String)

    Username of the DeathByCaptcha account.

  • password (String)

    Password of the DeathByCaptcha account.

  • connection (Symbol) (defaults to: :socket)

    Connection type (:socket, :http)

  • options (Hash) (defaults to: {})

    Options hash.

Options Hash (options):

  • :timeout (Integer) — default: 60

    Seconds before giving up.

  • :polling (Integer) — default: 5

    Seconds for polling the solution.

Returns:



28
29
30
31
32
33
34
35
36
37
# File 'lib/deathbycaptcha/client.rb', line 28

def self.create(username, password, connection = :socket, options = {})
  case connection
  when :socket
    DeathByCaptcha::Client::Socket.new(username, password, options)
  when :http
    DeathByCaptcha::Client::HTTP.new(username, password, options)
  else
    raise DeathByCaptcha::InvalidClientConnection
  end
end

Instance Method Details

#captcha(captcha_id) ⇒ DeathByCaptcha::Captcha

Retrieve information from an uploaded captcha.

Parameters:

  • captcha_id (Integer)

    Numeric ID of the captcha.

Returns:

Raises:

  • (NotImplementedError)


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).

Parameters:

  • options (Hash) (defaults to: {})

    Options hash.

Options Hash (options):

  • :url (String)

    URL of the image to be decoded.

  • :path (String)

    File path of the image to be decoded.

  • :file (File)

    File instance with image to be decoded.

  • :raw (String)

    Binary content of the image to be decoded.

  • :raw64 (String)

    Binary content encoded in base64 of the image to be decoded.

Returns:



69
70
71
72
73
# File 'lib/deathbycaptcha/client.rb', line 69

def decode(options = {})
  decode!(options)
rescue DeathByCaptcha::Error
  DeathByCaptcha::Captcha.new
end

#decode!(options = {}) ⇒ DeathByCaptcha::Captcha

Decode the text from an image (i.e. solve a captcha).

Parameters:

  • options (Hash) (defaults to: {})

    Options hash.

Options Hash (options):

  • :url (String)

    URL of the image to be decoded.

  • :path (String)

    File path of the image to be decoded.

  • :file (File)

    File instance with image to be decoded.

  • :raw (String)

    Binary content of the image to be decoded.

  • :raw64 (String)

    Binary content encoded in base64 of the image to be decoded.

Returns:

Raises:



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!(options = {})
  started_at = Time.now

  # don't load image data for Token API
  raw64 = nil
  unless options[:type] == 4
    raw64 = load_captcha(options)
    raise DeathByCaptcha::InvalidCaptcha if raw64.to_s.empty?
  end

  decoded_captcha = self.upload(options.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.

Parameters:

  • captcha_id (Integer)

    Numeric ID of the captcha.

Returns:

Raises:

  • (NotImplementedError)


125
126
127
# File 'lib/deathbycaptcha/client.rb', line 125

def report!(captcha_id)
  raise NotImplementedError
end

#statusDeathByCaptcha::ServerStatus

Retrieve DeathByCaptcha server status.

Returns:

Raises:

  • (NotImplementedError)


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.

Returns:

Raises:

  • (NotImplementedError)


152
153
154
# File 'lib/deathbycaptcha/client.rb', line 152

def upload(raw64)
  raise NotImplementedError
end

#userDeathByCaptcha::User

Retrieve your user information (which has the current credit balance).

Returns:

Raises:

  • (NotImplementedError)


133
134
135
# File 'lib/deathbycaptcha/client.rb', line 133

def user
  raise NotImplementedError
end