cbot 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6f2284e0ce375daa686449df11bb3b42d1e98c4c
4
+ data.tar.gz: c472c0c65a239321704bcf365836f81c1faf135d
5
+ SHA512:
6
+ metadata.gz: f4e276e3d5f6faa8caf2d57522902861f9a765d5a54da4e802cf20bc18a169bc5ea660b45812812f794518776ea659a63f31a0105b0e39a0d3ccb23ef865d9da
7
+ data.tar.gz: 755418c33ec2807561e31f191a20abd302a57d38b53fb804feb6bace59df93eca57474775aa87d7329d071097feb4ccab59c8728f5698204123b45b4376cf3dd
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cbot (1.0.0)
5
+ json (~> 1.8, >= 1.8.6)
6
+ rest-client
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ domain_name (0.5.20170404)
12
+ unf (>= 0.0.5, < 1.0.0)
13
+ http-cookie (1.0.3)
14
+ domain_name (~> 0.5)
15
+ json (1.8.6)
16
+ mime-types (3.1)
17
+ mime-types-data (~> 3.2015)
18
+ mime-types-data (3.2016.0521)
19
+ netrc (0.11.0)
20
+ rest-client (2.0.2)
21
+ http-cookie (>= 1.0.2, < 2.0)
22
+ mime-types (>= 1.16, < 4.0)
23
+ netrc (~> 0.8)
24
+ unf (0.1.4)
25
+ unf_ext
26
+ unf_ext (0.0.7.4)
27
+
28
+ PLATFORMS
29
+ ruby
30
+
31
+ DEPENDENCIES
32
+ cbot!
33
+
34
+ BUNDLED WITH
35
+ 1.15.3
data/LICENSE.md ADDED
@@ -0,0 +1,25 @@
1
+ # The MIT License (MIT)
2
+
3
+ Copyright © 2016 Eli Foster
4
+
5
+ Permission is hereby granted, free of charge, to any person
6
+ obtaining a copy of this software and associated documentation
7
+ files (the “Software”), to deal in the Software without
8
+ restriction, including without limitation the rights to use,
9
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the
11
+ Software is furnished to do so, subject to the following
12
+ conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
18
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24
+ OTHER DEALINGS IN THE SOFTWARE.
25
+
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # cleverbot
2
+
3
+ A Ruby wrapper for the cleverbot.com API.
4
+ Adapted from cleverbot.io API for cleverbot.com
5
+
6
+ ## Installation
7
+ ### Bundler
8
+ Add this line to your Gemfile:
9
+ ```ruby
10
+ gem 'cleverbot_rb', git: 'git@github.com:Reaver01/cleverbot_rb.git',
11
+ branch: 'master'
12
+ ```
13
+
14
+ And then execute:
15
+ ```shell
16
+ $ bundle install
17
+ ```
18
+
19
+ ## Usage
20
+ Be sure to look at the actual documentation for the cleverbot.com API when you are not sure about something. Or ask for documentation clarification in the issue tracker here.
21
+
22
+ Before using this module, please get your API keys at http://www.cleverbot.com/api/.
23
+
24
+ ```ruby
25
+ require 'cleverbot'
26
+
27
+ # Create a new Cleverbot instance, with an optional reference nick set.
28
+ CLEVER = Cleverbot.new('api_key')
29
+
30
+ # Talk with your pal, Cleverbot.
31
+ CLEVER.say('Why am I still talking to you?')
32
+ # => 'Because you have a beautiful soul.'
33
+ ```
data/cleverbot.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cleverbot/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'cbot'
8
+ spec.version = Cleverbot::VERSION
9
+ spec.authors = ['Reaver01']
10
+ spec.email = ['jawmac@gmail.com']
11
+
12
+ spec.summary = 'A Ruby wrapper for the Cleverbot.com web API.'
13
+ spec.description = 'A fully featured wrapper to the Cleverbot.com web API.'
14
+ spec.homepage = 'https://github.com/elieltavares/cleverbot'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test)/}) }
18
+ spec.require_paths = ['lib']
19
+
20
+
21
+ spec.add_dependency('rest-client')
22
+ spec.add_dependency('json', '~> 1.8', '>= 1.8.6')
23
+ end
data/lib/cleverbot.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'cleverbot/version'
2
+ require 'json'
3
+
4
+ # Cleverbot
5
+ module Cleverbot
6
+ require 'cleverbot/api'
7
+ require 'cleverbot/client'
8
+ require 'cleverbot/conversation'
9
+ extend self
10
+ end
@@ -0,0 +1,51 @@
1
+ require 'rest-client'
2
+ require 'json'
3
+
4
+ module Cleverbot
5
+ # Wrapper for Cleverbot's REST API
6
+ module API
7
+ # Base API url
8
+ API_URL = 'http://cleverbot.com'.freeze
9
+
10
+ # Name of the library
11
+ WRAPPER = 'cleverbot'.freeze
12
+
13
+ # The most time to wait to retry for a response
14
+ # from the API in seconds. This is because, at the time of
15
+ # writing, the API will sometimes return an empty response
16
+ # and you must retry your request.
17
+ MAX_BACKOFF = 60
18
+
19
+ module_function
20
+
21
+ # Executre a GET request.
22
+ # @param route [String] the route to request
23
+ # @param params [Hash] the params to query
24
+ def get_request(route = '', params = {})
25
+ params[:wrapper] = WRAPPER
26
+ response = RestClient.get "#{API_URL}/#{route}", params: params
27
+ JSON.parse response
28
+ end
29
+
30
+ # @param key [String] API key
31
+ # @param input [String] the phrase to pass to the conversation
32
+ # @param conversation [String] the conversation ID to pass input to
33
+ # @param retry_empty [true, false] whether to retry if we get an empty response
34
+ # @param backoff [Integer] how long to wait before retrying if retry_empty
35
+ # @return [String] the reply
36
+ def get_reply(key, input, conversation = nil, retry_empty: true, backoff: 1)
37
+ return if backoff > MAX_BACKOFF
38
+
39
+ reply = get_request(
40
+ 'getreply',
41
+ key: key, input: input, cs: conversation
42
+ )
43
+
44
+ return reply unless retry_empty && reply['output'].nil?
45
+ puts "Reponse empty! Retrying after #{backoff} seconds."
46
+ sleep backoff
47
+
48
+ get_reply(key, input, conversation, retry_empty: true, backoff: backoff * 2)
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,62 @@
1
+ require 'cleverbot/conversation'
2
+
3
+ module Cleverbot
4
+ # A client that wraps basic API functionality and can maintain
5
+ # multiple conversations associated with an identifier
6
+ # @example Basic conversing
7
+ # client = Cleverbot::Client.new 'your api key'
8
+ # puts client.say 'hello' #=> *witty response*
9
+ # puts client.say 'okay' #=> *another witty response*
10
+ # @example Multiple conversations
11
+ # client = Cleverbot::Client.new 'your api key'
12
+ #
13
+ # # Client#say takes an arbitray identifier as a second arguement
14
+ # # that will start a new conversation with each unique object
15
+ # # given, and continue that conversation when passed the same identifier.
16
+ #
17
+ # # Start a new conversation with Mike the symbol
18
+ # puts client.say('hello', :mike) #=> 'hello'
19
+ # puts client.say('my name is mike', :mike) #=> 'ok'
20
+ #
21
+ # # Start a new conversation with Zac the string.
22
+ # puts client.say('hello', 'Zac') #=> 'hello'
23
+ # puts client.say('my name is mike', 'Zac') #=> 'no it is zac'
24
+ class Client
25
+ # @return [String] API key used to make requests with this Client
26
+ attr_reader :key
27
+
28
+ # @return [Hash<thing, Conversation>] the conversations initiated by this client
29
+ attr_reader :conversations
30
+
31
+ # Create a new Client
32
+ # @param key [String] API key
33
+ def initialize(key)
34
+ @key = key
35
+ @conversations = {}
36
+ end
37
+
38
+ # @param identifier [thing] identifier to search for
39
+ # @return [Conversation] the conversation associated with this identifier
40
+ def conversation(identifier = :default)
41
+ @conversations[identifier]
42
+ end
43
+
44
+ # Say something to the API
45
+ # @param message [String] what to say
46
+ # @param identifier [thing] identifier to associate this converation with
47
+ # @return [String] the response
48
+ def say(message, identifier = :default)
49
+ convo = conversation(identifier) || Conversation.new(key)
50
+ @conversations[identifier] = convo
51
+
52
+ convo.reply message
53
+ end
54
+
55
+ # Deletes a conversation
56
+ # @param identifier [thing] identifier associated with the conversation
57
+ # @return [Conversation] the removed conversation
58
+ def delete(identifier = :default)
59
+ @conversations.delete identifier
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,86 @@
1
+ require 'cleverbot/api'
2
+
3
+ module Cleverbot
4
+ # A conversation with the API
5
+ class Conversation
6
+ # @return [String] this conversation's hash, assigned by cleverbot
7
+ attr_reader :cs
8
+
9
+ # @return [String] the input given to this interaction
10
+ attr_reader :input
11
+
12
+ # @return [String] the output to this interaction
13
+ attr_reader :output
14
+ alias response output
15
+
16
+ # @return [Time] timestamp of this interaction
17
+ attr_reader :timestamp
18
+
19
+ # @return [Array<Hash>] interaction history in this conversation
20
+ attr_reader :interactions
21
+
22
+ # @return [Integer] number of interactions
23
+ attr_reader :interaction_count
24
+
25
+ # The API returns a lot of other arbitray information that may or may not
26
+ # be useful. The most common pieces of data have direct instance methods ;
27
+ # for anything else, look inside this hash.
28
+ # @return [Hash] all raw data about this conversation
29
+ attr_reader :data
30
+
31
+ # Start a new conversation with the API
32
+ # @param key [String] the API key used for this conversation
33
+ def initialize(key)
34
+ @key = key
35
+ end
36
+
37
+ # Replies to this conversation thread.
38
+ # @param message [String] the message to send
39
+ # @raise [ArgumentError] if message is empty
40
+ def reply(message)
41
+ raise ArgumentError, 'message cannot be empty' if message.empty?
42
+ update_data API.get_reply @key, message, cs
43
+ output
44
+ end
45
+
46
+ # Internally updates this conversation's data
47
+ # @param data [Hash] an API response hash to update from
48
+ private def update_data(data)
49
+ @data = data
50
+
51
+ @cs = data['cs']
52
+
53
+ @input = data['input']
54
+
55
+ @output = data['output']
56
+
57
+ @timestamp = Time.new(
58
+ data['time_year'],
59
+ data['time_month'],
60
+ data['time_day'],
61
+ data['time_hour'],
62
+ data['time_minute'],
63
+ data['time_second'],
64
+ 0
65
+ )
66
+
67
+ @interactions = []
68
+
69
+ data.select { |k, _| k.start_with? 'interaction_' }.each do |k, v|
70
+ next if v.empty? || k == 'interaction_count'
71
+
72
+ index = k[/\d+/].to_i - 1
73
+
74
+ @interactions[index] ||= {}
75
+
76
+ if k =~ (/interaction_\d+$/)
77
+ @interactions[index][:input] = v
78
+ elsif k =~ (/interaction_\d+_other$/)
79
+ @interactions[index][:output] = v
80
+ end
81
+ end
82
+
83
+ @interaction_count = data['interaction_count'].to_i
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,3 @@
1
+ module Cleverbot
2
+ VERSION = '1.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cbot
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Reaver01
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-07-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.8'
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 1.8.6
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '1.8'
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 1.8.6
47
+ description: A fully featured wrapper to the Cleverbot.com web API.
48
+ email:
49
+ - jawmac@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - Gemfile
55
+ - Gemfile.lock
56
+ - LICENSE.md
57
+ - README.md
58
+ - cleverbot.gemspec
59
+ - lib/cleverbot.rb
60
+ - lib/cleverbot/api.rb
61
+ - lib/cleverbot/client.rb
62
+ - lib/cleverbot/conversation.rb
63
+ - lib/cleverbot/version.rb
64
+ homepage: https://github.com/elieltavares/cleverbot
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.6.12
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: A Ruby wrapper for the Cleverbot.com web API.
88
+ test_files: []