cleverbot_io 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 44eeeb0cce1a6763a31117f2e1ce22c7544d69b0
4
- data.tar.gz: 010a3e52ff6cd191a9104719a45a543dfd5a80c7
3
+ metadata.gz: 03197bf5c92655289d8320102d13abd48b41a07d
4
+ data.tar.gz: 9da8d662b7a9df22882ded1098260816f1359b2d
5
5
  SHA512:
6
- metadata.gz: cf1a687301292fa6607ddf15fcc6a9ff37353453ea8f77ccafd4762f01d599bcb13a1f49bdf50018bad37e2beacf34ffe772c0e7e4a03b843291480eab3fc146
7
- data.tar.gz: a37bf1e647a12fe59afea55ea75af16ff41940e26d35d8c4917946727358f153a426f95e3cec24a8992672dbe738fd344cd8fce3e0b2b0336c600dbcb9cadebd
6
+ metadata.gz: e800d378043b80a3bf506dd785c22f4f9b73ac80f9728877e962260a50d005ade78a574b18fc2a45571c2b89ee6c776fc3e8ab0e428e44fd207c6db891cb107a
7
+ data.tar.gz: 84fef4a0be0f20dc04cae4c4c75179f8cd37c2fbcece3eb7317348c55187080b85d317212a8150ed97ca84aed6b96ec0a1bc74c88f7f4ef7d9d35074fee33746
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
  ## Version 1
3
+ ### 1.2.0
4
+ * Improve performance by using optimized JSON instead of stdlib JSON
5
+ * Implement proper error handling for both of Cleverbot's errors, as well as a fallback error which complains about being unrecognized (#3).
6
+
3
7
  ### 1.1.0
4
8
  * Merge `#create` into `#initialize`.
5
9
  * `api_user` and `api_key` are no longer accessors, but readers.
data/lib/cleverbot.rb CHANGED
@@ -1,9 +1,15 @@
1
1
  require 'httpclient'
2
- require 'json'
2
+ require 'oj'
3
+ require_relative 'cleverbot_errors'
3
4
 
4
5
  class Cleverbot
6
+ # @return [String] The API User for the instance.
5
7
  attr_reader :api_user
8
+
9
+ # @return [String] The API Key for the instance.
6
10
  attr_reader :api_key
11
+
12
+ # @return [String] The reference nick for the session.
7
13
  attr_accessor :nick
8
14
 
9
15
  # Creates a new instance of the Cleverbot.
@@ -21,14 +27,14 @@ class Cleverbot
21
27
  key: @api_key
22
28
  }
23
29
  params[:nick] = @nick unless @nick.nil?
24
- response = JSON.parse(@client.post('https://cleverbot.io/1.0/create', params).body)
25
- @nick = response['nick'] if response['status'] == 'success'
30
+ response = Oj.load(@client.post('https://cleverbot.io/1.0/create', params).body)
31
+ try_throw(response['status'])
32
+ @nick = response['nick']
26
33
  end
27
34
 
28
- # Sends the bot a message and returns its response. If the nick has not been set, it will call #{create}.
35
+ # Sends the bot a message and returns its response.
29
36
  # @param str [String] The message to send to the bot.
30
37
  # @return [String] The bot's response, or its error message.
31
- # @todo Error handling.
32
38
  def say(str)
33
39
  params = {
34
40
  user: @api_user,
@@ -36,11 +42,24 @@ class Cleverbot
36
42
  text: str,
37
43
  nick: @nick
38
44
  }
39
- response = JSON.parse(@client.post('https://cleverbot.io/1.0/ask', params).body)
40
- if response['status'] == 'success'
41
- response['response']
42
- else
43
- response['status']
45
+ response = Oj.load(@client.post('https://cleverbot.io/1.0/ask', params).body)
46
+ try_throw(response['status'])
47
+
48
+ response['response']
49
+ end
50
+
51
+ private
52
+
53
+ # Throws the relevant errors if possible.
54
+ # @param status [String] The status value from the API
55
+ # @raise [IncorrectCredentialsError] If the api_user and api_key are incorrect.
56
+ # @raise [DuplicatedReferenceNamesError] If the reference name is already in use by the instance.
57
+ def try_throw(status)
58
+ case status
59
+ when 'Error: API credentials incorrect' then fail Cleverbot::Errors::IncorrectCredentialsError
60
+ when 'Error: reference name already exists' then fail Cleverbot::Errors::DuplicatedReferenceNamesError
61
+ when 'success' then return
62
+ else fail "#{response['status']} UNRECOGNIZED ERROR! PLEASE REPORT TO CLEVERBOT RUBY ISSUE TRACKER."
44
63
  end
45
64
  end
46
65
  end
@@ -0,0 +1,15 @@
1
+ class Cleverbot
2
+ class Errors
3
+ class IncorrectCredentialsError < StandardError
4
+ def message
5
+ 'Incorrect API credentials.'
6
+ end
7
+ end
8
+
9
+ class DuplicatedReferenceNamesError < StandardError
10
+ def message
11
+ 'Reference name already exists.'
12
+ end
13
+ end
14
+ end
15
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cleverbot_io
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eli Foster
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-27 00:00:00.000000000 Z
11
+ date: 2016-03-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httpclient
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
26
  version: 2.7.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: oj
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 2.14.6
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 2.14.6
27
41
  description: A fully featured wrapper to the Cleverbot.io web API.
28
42
  email: elifosterwy@gmail.com
29
43
  executables: []
@@ -32,6 +46,7 @@ extra_rdoc_files: []
32
46
  files:
33
47
  - CHANGELOG.md
34
48
  - lib/cleverbot.rb
49
+ - lib/cleverbot_errors.rb
35
50
  homepage: https://github.com/CleverbotIO/ruby-cleverbot.io
36
51
  licenses:
37
52
  - CC-BY-NC-ND-4.0