ai 0.0.2 → 0.0.3

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: 9aa72c3d7d57c0ac6ee419dcba9852763d906a93
4
- data.tar.gz: 24885f0576dc51585c41fa1a6968e71e6d1ce25d
3
+ metadata.gz: 2321eb74fc2b7bc0ea2ee72d48742dfa9194db42
4
+ data.tar.gz: 7c21fa8cfa4629e9ed77d274bc59f407387fd2d0
5
5
  SHA512:
6
- metadata.gz: ac0361bc12ac255195918c7063a02da6e15b3668b8fa645f22f82340e7a93007d59cb854bcc5206692aac462e91d4fb1ff0184b6af6e5b599700eba43e660d8e
7
- data.tar.gz: a688d7d6c7249549f97742fc5ccf7caa8e156ccfb4d8e1203e6feb21d6132561703eee5734e62eda3f8214166815a7050d303b3149e5af290e2ca67997239fbb
6
+ metadata.gz: 976579cca8578a64ef0cb6fad2ffa13d20d63437378480ab61005619a57e12e6058074c21e8a7d2af1d6a2c9ed6f74fabff13993c7be877ac0ac82792c95d76d
7
+ data.tar.gz: c45b2c774393cc1a040b0c643a3dcffe8145b36a8f40d7377d753c1babec5d4dcf3da90bf1e5b7aa15e27c31d4a5804df19729e662d8067923755d293721be1a
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ require 'ai'
3
+
4
+ answers = Ai.list_answers
5
+
6
+ answers.each do |answer|
7
+ puts "#{answer['utterance']} - #{answer['response']}"
8
+ end
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ require 'ai'
3
+
4
+ scripts = Ai.list_scripts
5
+
6
+ scripts.each do |script|
7
+ puts "#{script['utterance']} - #{script['script']}"
8
+ end
data/lib/ai.rb CHANGED
@@ -14,7 +14,7 @@ class Ai
14
14
  end
15
15
 
16
16
  def self.post(utterance)
17
- uri = URI.parse(ENV['REPLY_URL'] || "http://milligan-acceptance.herokuapp.com/milligans/reply")
17
+ uri = URI.parse((ENV['PIVOTAL_API_URL'] || "http://milligan-acceptance.herokuapp.com") + "/milligans/reply")
18
18
 
19
19
  data = { utterance: utterance }.to_json
20
20
 
@@ -35,5 +35,13 @@ class Ai
35
35
  def self.learn_script(questions, answer)
36
36
  ::AiClient.learn_script(questions, answer)
37
37
  end
38
+
39
+ def self.list_answers
40
+ ::AiClient.list_answers
41
+ end
42
+
43
+ def self.list_scripts
44
+ ::AiClient.list_scripts
45
+ end
38
46
  end
39
47
 
@@ -35,4 +35,12 @@ class AiClient
35
35
 
36
36
  WebClient.post(BASE_URL + '/api/v1/direct_responses/import', body)
37
37
  end
38
+
39
+ def self.list_answers
40
+ WebClient.get(BASE_URL + '/api/v1/direct_responses')
41
+ end
42
+
43
+ def self.list_scripts
44
+ WebClient.get(BASE_URL + '/api/v1/script_responses')
45
+ end
38
46
  end
@@ -19,4 +19,21 @@ class WebClient
19
19
  {}
20
20
  end
21
21
  end
22
+
23
+ def self.get(url)
24
+ uri = URI.parse(url)
25
+ http = Net::HTTP.new(uri.host, uri.port)
26
+
27
+ request = Net::HTTP::Get.new(
28
+ uri.request_uri,
29
+ 'Content-Type' => 'application/json'
30
+ )
31
+
32
+ response = http.request(request)
33
+ unless response.body.nil?
34
+ JSON.parse(response.body)
35
+ else
36
+ {}
37
+ end
38
+ end
22
39
  end
@@ -1,59 +1,87 @@
1
+ require 'ai'
1
2
  require 'ai_client'
2
- require 'web_client'
3
3
 
4
- RSpec.describe AiClient do
5
- describe 'self#import' do
6
- it 'submits import file to the server' do
7
- url = "http://localhost:4040/api/v1/direct_responses/import"
4
+ RSpec.describe Ai do
5
+ describe '#process' do
6
+ context 'given general utterance' do
7
+ it 'makes a post request' do
8
+ http = double("Http")
9
+ expect(Net::HTTP).to receive(:new).and_return(http)
10
+ response = double('Response')
11
+ expect(response).to receive(:body).and_return({ 'type' => 'script', 'script' => 'ls -a' }.to_json)
12
+ expect(http).to receive(:request_post).and_return(response)
13
+ expect(Kernel).to receive(:system).with('ls -a').and_return(true)
8
14
 
9
- path = '/Users/apliszka/workspace/ai/spec/fixtures/import.json'
10
- body = { knowledge: JSON.parse(File.read(path)) }
15
+ Ai.process('show files')
16
+ end
11
17
 
12
- expect(WebClient).to receive(:post).with(url, body)
18
+ context 'given acton response' do
19
+ it 'runs the command' do
20
+ response = { 'type' => 'script', 'script' => 'ls -a' }
13
21
 
14
- AiClient.import(path)
22
+ expect(Kernel).to receive(:system).with('ls -a').and_return(true)
23
+ expect(Ai).to receive(:post).and_return(response)
24
+
25
+ Ai.process('show files')
26
+ end
27
+ end
28
+
29
+ context 'given text response' do
30
+ it 'prints the response' do
31
+ response = { 'type' => 'text', 'text' => 'hi' }
32
+
33
+ expect(Kernel).to_not receive(:system)
34
+ expect(Kernel).to receive(:puts).with('hi').and_return(true)
35
+ expect(Ai).to receive(:post).and_return(response)
36
+
37
+ Ai.process('Hello')
38
+ end
39
+ end
15
40
  end
16
- end
17
41
 
18
- describe 'self#learn' do
19
- it 'submits new utterance to server' do
20
- url = "http://localhost:4040/api/v1/direct_responses/import"
42
+ context 'given import' do
43
+ it 'calls AI client' do
44
+ path = '/tmp/import.json'
45
+ expect(AiClient).to receive(:import).with(path)
21
46
 
22
- body = {
23
- "knowledge" => [
24
- {
25
- "utterances" => [
26
- "What are the best AI courses?"
27
- ],
28
- "response": "Fast.ai MOOC http://course.fast.ai and Coursers"
29
- }
30
- ]
31
- }
47
+ Ai.import(path)
48
+ end
49
+ end
32
50
 
33
- expect(WebClient).to receive(:post).with(url, { knowledge: body })
51
+ context 'given learn' do
52
+ it 'calls AI client' do
53
+ utterance = 'What is TDD?'
54
+ response = "TDD stands for Test Driven Development"
55
+ expect(AiClient).to receive(:learn_answer).with([utterance], response)
34
56
 
35
- AiClient.learn_answer(["What are the best AI courses?"], "Fast.ai MOOC http://course.fast.ai and Coursers")
57
+ Ai.learn_answer([utterance], response)
58
+ end
59
+ end
60
+
61
+ context 'given learn script' do
62
+ it 'calls AI client' do
63
+ utterance = 'show changes'
64
+ script = 'git diff'
65
+ expect(AiClient).to receive(:learn_script).with(utterance, script)
66
+
67
+ Ai.learn_script(utterance, script)
68
+ end
36
69
  end
37
70
  end
38
71
 
39
- describe 'self#learn_script' do
40
- it 'submits new script to server' do
41
- url = "http://localhost:4040/api/v1/direct_responses/import"
72
+ describe '#list_answers' do
73
+ it 'calls AI client' do
74
+ expect(AiClient).to receive(:list_answers)
42
75
 
43
- body = {
44
- "scripts" => [
45
- {
46
- "utterances" => [
47
- "show changes"
48
- ],
49
- "script": "git diff"
50
- }
51
- ]
52
- }
76
+ Ai.list_answers
77
+ end
78
+ end
53
79
 
54
- expect(WebClient).to receive(:post).with(url, { knowledge: body })
80
+ describe '#list_scripts' do
81
+ it 'calls AI client' do
82
+ expect(AiClient).to receive(:list_scripts)
55
83
 
56
- AiClient.learn_script(["show changes"], "git diff")
84
+ Ai.list_scripts
57
85
  end
58
86
  end
59
87
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ai
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Pliszka
@@ -18,6 +18,8 @@ executables:
18
18
  - ai-import
19
19
  - ai-learn-script
20
20
  - ai-learn-answer
21
+ - ai-list-answers
22
+ - ai-list-scripts
21
23
  extensions: []
22
24
  extra_rdoc_files: []
23
25
  files:
@@ -25,6 +27,8 @@ files:
25
27
  - bin/ai-import
26
28
  - bin/ai-learn-answer
27
29
  - bin/ai-learn-script
30
+ - bin/ai-list-answers
31
+ - bin/ai-list-scripts
28
32
  - lib/ai.rb
29
33
  - lib/ai_client.rb
30
34
  - lib/constants.rb