Class: DeathByCaptcha::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/deathbycaptcha/client.rb

Overview

DeathByCaptcha API Client

Direct Known Subclasses

HTTPClient, SocketClient

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password, extra = {}) ⇒ Client

Returns a new instance of Client.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/deathbycaptcha/client.rb', line 14

def initialize(username, password, extra = {})
  data = {
    :is_verbose => false, # If true, prints messages during execution
    :logger_output => STDOUT, # Logger output path or IO instance
    :api_version => API_VERSION, # API version (used as user-agent with http requests)
    :software_vendor_id => 0, # API unique software ID
    :max_captcha_file_size => 64 * 1024, # Maximum CAPTCHA image filesize, currently 64K
    :default_timeout => 60, # Default CAPTCHA timeout
    :polls_interval => 5, # Default decode polling interval
    :http_base_url => 'http://api.deathbycaptcha.com/api', # Base HTTP API url
    :http_response_type => 'application/json', # Preferred HTTP API server's response content type, do not change
    :socket_host => 'api.deathbycaptcha.com', # Socket API server's host
    :socket_port => (8123..8130).map { |p| p }, # Socket API server's ports range
    :username => username, # DeathByCaptcha username
    :password => password, # DeathByCaptcha user's password not encrypted
  }.merge(extra)
  
  @config = DeathByCaptcha::Config.new(data) # Config instance
  @logger = Logger.new(@config.logger_output) # Logger
  
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



12
13
14
# File 'lib/deathbycaptcha/client.rb', line 12

def config
  @config
end

Instance Method Details

#decode(captcha, options = {}) ⇒ Object

Try to solve a CAPTCHA.

See Client.upload() for arguments details.

Uploads a CAPTCHA, polls for its status periodically with arbitrary timeout (in seconds). Removes unsolved CAPTCHAs. Returns CAPTCHA details if (correctly) solved.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/deathbycaptcha/client.rb', line 91

def decode(captcha, options = {})
  options = {
    :timeout => config.default_timeout,
    :is_case_sensitive => false,
    :is_raw_content => false
  }.merge(options)
  
  deadline = Time.now + options[:timeout]
  c = upload(captcha, options)
  if c
    
    while deadline > Time.now and (c['text'].nil? or c['text'].empty?)
      sleep(config.polls_interval)
      c = get_captcha(c['captcha'])
    end
    
    if c['text']
      return c if c['is_correct']
    else
      remove(c['captcha'])
    end
    
  end
  
end

#get_balanceObject

Fetch the user’s balance (in US cents)



46
47
48
# File 'lib/deathbycaptcha/client.rb', line 46

def get_balance
  raise DeathByCaptcha::Errors::NotImplemented
end

#get_captcha(cid) ⇒ Object

Fetch a CAPTCHA details – its numeric ID, text and correctness



53
54
55
# File 'lib/deathbycaptcha/client.rb', line 53

def get_captcha(cid)
  raise DeathByCaptcha::Errors::NotImplemented
end

#get_text(cid) ⇒ Object

Fetch a CAPTCHA text



60
61
62
# File 'lib/deathbycaptcha/client.rb', line 60

def get_text(cid)
  raise DeathByCaptcha::Errors::NotImplemented
end

#get_userObject

Fetch the user’s details – balance, rate and banned status



39
40
41
# File 'lib/deathbycaptcha/client.rb', line 39

def get_user
  raise DeathByCaptcha::Errors::NotImplemented
end

#report(cid) ⇒ Object

Report a CAPTCHA as incorrectly solved



67
68
69
# File 'lib/deathbycaptcha/client.rb', line 67

def report(cid)
  raise DeathByCaptcha::Errors::NotImplemented
end

#upload(captcha, options = {}) ⇒ Object

Upload a CAPTCHA

Accepts file names, file objects or urls, and an optional flag telling whether the CAPTCHA is case-sensitive or not. Returns CAPTCHA details on success.



78
79
80
# File 'lib/deathbycaptcha/client.rb', line 78

def upload(captcha, options = {})
  raise DeathByCaptcha::Errors::NotImplemented
end