Class: Cleverbot::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/cleverbot/client.rb

Overview

Ruby wrapper for Cleverbot.com.

Constant Summary collapse

DEFAULT_PARAMS =

The default form variables for POSTing to Cleverbot.com

{
  'stimulus' => '',
  'start' => 'y',
  'sessionid' => '',
  'vText8' => '',
  'vText7' => '',
  'vText6' => '',
  'vText5' => '',
  'vText4' => '',
  'vText3' => '',
  'vText2' => '',
  'icognoid' => 'wsf',
  'icognocheck' => '',
  'fno' => '0',
  'prevref' => '',
  'emotionaloutput' => '',
  'emotionalhistory' => '',
  'asbotname' => '',
  'ttsvoice' => '',
  'typing' => '',
  'lineref' => '',
  'sub' => 'Say',
  'islearning' => '1',
  'cleanslate' => 'false',
}
PATH =

The path to the form endpoint on Cleverbot.com.

'/webservicemin'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Client

Initializes a Client with given parameters.

Parameters

params

Optional Hash holding the initial parameters. Defaults to {}.



73
74
75
# File 'lib/cleverbot/client.rb', line 73

def initialize params={}
  @params = params
end

Instance Attribute Details

#paramsObject (readonly)

Holds the parameters for an instantiated Client.



43
44
45
# File 'lib/cleverbot/client.rb', line 43

def params
  @params
end

Class Method Details

.digest(body) ⇒ Object

Creates a digest from the form parameters.

Parameters

body

String to be digested.



50
51
52
# File 'lib/cleverbot/client.rb', line 50

def self.digest body
  Digest::MD5.hexdigest body[9...35]
end

.write(message = '', params = {}) ⇒ Object

Sends a message to Cleverbot.com and returns a Hash containing the parameters received.

Parameters

message

Optional String holding the message to be sent. Defaults to ''.

params

Optional Hash with form parameters. Merged with DEFAULT_PARAMS. Defaults to {}.



60
61
62
63
64
65
66
# File 'lib/cleverbot/client.rb', line 60

def self.write message='', params={}
  body = DEFAULT_PARAMS.merge params
  body['stimulus'] = message
  body['icognocheck'] = digest HashConversions.to_params(body)

  post(PATH, :body => body).parsed_response
end

Instance Method Details

#write(message = '') ⇒ Object

Sends a message and returns a String with the message received. Updates #params to maintain state.

Parameters

message

Optional String holding the message to be sent. Defaults to ''.



82
83
84
85
86
87
88
89
# File 'lib/cleverbot/client.rb', line 82

def write message=''
  response = self.class.write message, @params
  message = response['message']
  response.keep_if { |key, value| DEFAULT_PARAMS.keys.include? key }
  @params.merge! response
  @params.delete_if { |key, value| DEFAULT_PARAMS[key] == value }
  message
end