Class: Cleverbot::Client

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

Overview

A client that wraps basic API functionality and can maintain multiple conversations associated with an identifier

Examples:

Basic conversing

client = Cleverbot::Client.new 'your api key'
puts client.say 'hello' #=> *witty response*
puts client.say 'okay' #=> *another witty response*

Multiple conversations

client = Cleverbot::Client.new 'your api key'

# Client#say takes an arbitray identifier as a second arguement
# that will start a new conversation with each unique object
# given, and continue that conversation when passed the same identifier.

# Start a new conversation with Mike the symbol
puts client.say('hello', :mike) #=> 'hello'
puts client.say('my name is mike', :mike) #=> 'ok'

# Start a new conversation with Zac the string.
puts client.say('hello', 'Zac') #=> 'hello'
puts client.say('my name is mike', 'Zac') #=> 'no it is zac'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Client

Create a new Client

Parameters:

  • key (String)

    API key



33
34
35
36
# File 'lib/cleverbot/client.rb', line 33

def initialize(key)
  @key = key
  @conversations = {}
end

Instance Attribute Details

#conversationsHash<thing, Conversation> (readonly)

Returns the conversations initiated by this client.

Returns:

  • (Hash<thing, Conversation>)

    the conversations initiated by this client



29
30
31
# File 'lib/cleverbot/client.rb', line 29

def conversations
  @conversations
end

#keyString (readonly)

Returns API key used to make requests with this Client.

Returns:

  • (String)

    API key used to make requests with this Client



26
27
28
# File 'lib/cleverbot/client.rb', line 26

def key
  @key
end

Instance Method Details

#conversation(identifier = :default) ⇒ Conversation

Returns the conversation associated with this identifier.

Parameters:

  • identifier (thing) (defaults to: :default)

    identifier to search for

Returns:

  • (Conversation)

    the conversation associated with this identifier



40
41
42
# File 'lib/cleverbot/client.rb', line 40

def conversation(identifier = :default)
  @conversations[identifier]
end

#delete(identifier = :default) ⇒ Conversation

Deletes a conversation

Parameters:

  • identifier (thing) (defaults to: :default)

    identifier associated with the conversation

Returns:



58
59
60
# File 'lib/cleverbot/client.rb', line 58

def delete(identifier = :default)
  @conversations.delete identifier
end

#say(message, identifier = :default) ⇒ String

Say something to the API

Parameters:

  • message (String)

    what to say

  • identifier (thing) (defaults to: :default)

    identifier to associate this converation with

Returns:

  • (String)

    the response



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

def say(message, identifier = :default)
  convo = conversation(identifier) || Conversation.new(key)
  @conversations[identifier] = convo

  convo.reply message
end