cleverbotrb 0.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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/cleverbotrb.rb +116 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a81a7481aff69c205d4601e000e95ad2004ff635
4
+ data.tar.gz: a39102217f0adb71c19e8d5244e393d23430df19
5
+ SHA512:
6
+ metadata.gz: ad73e070ce16fecb5c08a2be9cc828911ed42d4a9df8fc970dc208af5cd51b18c41b61a1ddf8d54273f9e563434b753d355d71b5a59702bb7e807c14717f6db8
7
+ data.tar.gz: 02362496a4d7dbb8f04939cac192673c109cd44cf96fbad9fb7e9f4943de0cc8bc245687e99b72dfc35fef652198d5637bb37513361337b3cbd17d360adeafc4
@@ -0,0 +1,116 @@
1
+ # Copyright (c) 2016 d0p1 <d0p1@yahoo.com>
2
+ # Author: d0p1 <d0p1@yahoo.com>
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of
5
+ # this software and associated documentation files (the "Software"), to deal in
6
+ # the Software without restriction, including without limitation the rights to
7
+ # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8
+ # the Software, and to permit persons to whom the Software is furnished to do so,
9
+ # subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in all
12
+ # copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLU
16
+ # DING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ # FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ require 'uri'
23
+ require 'net/http'
24
+ require 'digest'
25
+
26
+ ##
27
+ # This class represent cleverbot session
28
+ #
29
+ # @author d0p1
30
+ class Cleverbot
31
+
32
+ HOST = "www.cleverbot.com"
33
+ ENDPOINT = "/webservicemin?uc=255&"
34
+
35
+ PARAMS = {
36
+ 'stimulus' => '', 'start' => 'y', 'sessionid' => '',
37
+ 'vText8' => '', 'vText7' => '', 'vText6' => '', 'vText5' => '',
38
+ 'vText4' => '', 'vText3' => '', 'vText2' => '',
39
+ 'icognoid' => 'wsf', 'icognocheck' => '', 'fno' => '0',
40
+ 'prevref' => '', 'emotionaloutput' => '', 'asbotname' => '',
41
+ 'ttsvoice' => '', 'typing' => '', 'lineref' => '',
42
+ 'sub' => 'Say', 'islearning' => '1', 'cleanslate' => 'false'
43
+ }
44
+
45
+ RESPONSE_KEY = [
46
+ 'message', 'sessionid', 'logurl', 'vText8',
47
+ 'vText7', 'vText6', 'vText5', 'vText4',
48
+ 'vText3', 'vText2', 'prevref', '',
49
+ 'emotionalhistory', 'ttsLocMP3', 'ttsLocTXT', 'ttsLocTXT3',
50
+ 'ttsText', 'lineref', 'lineURL', 'linePOST',
51
+ 'lineChoices', 'lineChoicesAbbrev', 'typingData', 'divert'
52
+ ]
53
+
54
+ ##
55
+ # Create a new session
56
+ def initialize
57
+ @cookies = {}
58
+ @params = PARAMS
59
+ prepare
60
+ end
61
+
62
+ ##
63
+ # send a message to cleverbot and get an answer
64
+ #
65
+ # @param message [String]
66
+ # @return [String] the answer statement
67
+ def send(message)
68
+ body = @params
69
+ body["stimulus"] = message
70
+ body["icognocheck"] = digest(URI.encode_www_form(body)[9...35])
71
+ body_str = URI.encode_www_form(body) #params_encode(body)
72
+
73
+ headers = {
74
+ 'Content-Type' => 'application/x-www-form-urlencoded',
75
+ 'Content-Length' => body_str.size.to_s,
76
+ 'Cache-Control' => 'no-cache',
77
+ 'Cookie' => cookies_encode(@cookies)
78
+ }
79
+
80
+ req = Net::HTTP.new(HOST, 80)
81
+ resp = req.post(ENDPOINT, body_str, headers)
82
+ if resp.code != "200"
83
+ return nil
84
+ end
85
+ response = resp.body.split("\r")
86
+ save_data(response)
87
+ return response[0]
88
+ end
89
+
90
+ private
91
+
92
+ def prepare
93
+ req = Net::HTTP.new(HOST, 80)
94
+ resp = req.get('/')
95
+ raw_cookies = resp.response['set-cookie']
96
+ if not raw_cookies.nil?
97
+ raw_cookies.scan(/(\w+)=([\w|\/]+)/) do |key, value|
98
+ @cookies[key] = value
99
+ end
100
+ end
101
+ end
102
+
103
+ def digest(data)
104
+ Digest::MD5.hexdigest data
105
+ end
106
+
107
+ def save_data(response)
108
+ RESPONSE_KEY.each_with_index do |key, index|
109
+ @params[key] = response[index]
110
+ end
111
+ end
112
+
113
+ def cookies_encode(cookies)
114
+ cookies.to_a.map { |cookie| "#{cookie[0]}=#{cookie[1]}" }.join(";")
115
+ end
116
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cleverbotrb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - d0p1
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple cleverbot gem
14
+ email: d0p1@yahoo.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/cleverbotrb.rb
20
+ homepage: https://github.com/d0p1s4m4/cleverbotrb
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.2.5
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Cleverbot
44
+ test_files: []