converse 1.0.3 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
data/lib/converse/api.rb
CHANGED
data/lib/converse/broker.rb
CHANGED
@@ -1,13 +1,15 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module Converse
|
2
|
+
class Broker
|
3
|
+
attr_accessor :domain_language
|
3
4
|
|
4
|
-
|
5
|
-
|
5
|
+
def open_topic(concern, action)
|
6
|
+
end
|
6
7
|
|
7
|
-
|
8
|
-
|
8
|
+
def broker_conversation(topic)
|
9
|
+
end
|
9
10
|
|
10
|
-
|
11
|
-
|
11
|
+
def discuss(concern, action)
|
12
|
+
broker_conversation(open_topic(concern, action))
|
13
|
+
end
|
12
14
|
end
|
13
|
-
end
|
15
|
+
end
|
@@ -1,39 +1,41 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
1
|
+
module Converse
|
2
|
+
class Conversation
|
3
|
+
attr_accessor :uri
|
4
|
+
attr_accessor :body
|
5
|
+
attr_accessor :headers
|
6
|
+
attr_accessor :connection
|
7
|
+
attr_accessor :host
|
8
|
+
attr_accessor :port
|
9
|
+
attr_accessor :path
|
10
|
+
attr_accessor :request
|
11
|
+
attr_accessor :response
|
12
|
+
attr_accessor :subscribers
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
14
|
+
def initialize(uri)
|
15
|
+
@uri = uri
|
16
|
+
parsed = URI.parse(uri)
|
17
|
+
@host = parsed.host
|
18
|
+
@port = parsed.port
|
19
|
+
@path = parsed.path
|
20
|
+
@subscribers = []
|
21
|
+
end
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
|
23
|
+
def ask
|
24
|
+
raise NotImplementedError.new
|
25
|
+
end
|
25
26
|
|
26
|
-
|
27
|
-
|
28
|
-
|
27
|
+
def say
|
28
|
+
raise NotImplementedError.new
|
29
|
+
end
|
29
30
|
|
30
|
-
|
31
|
-
|
32
|
-
|
31
|
+
def subscribe(subscriber)
|
32
|
+
@subscribers << subscriber
|
33
|
+
end
|
33
34
|
|
34
|
-
|
35
|
-
|
36
|
-
|
35
|
+
def notify_subscribers(data)
|
36
|
+
@subscribers.each do |subscriber|
|
37
|
+
subscriber.notify(data)
|
38
|
+
end
|
37
39
|
end
|
38
40
|
end
|
39
|
-
end
|
41
|
+
end
|
@@ -2,56 +2,58 @@ require 'net/http'
|
|
2
2
|
require "comms/conversation"
|
3
3
|
require "json"
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
5
|
+
module Converse
|
6
|
+
class HTMLConversation < Conversation
|
7
|
+
attr_accessor :use_ssl
|
8
|
+
|
9
|
+
def initialize(uri)
|
10
|
+
super(uri)
|
11
|
+
@use_ssl = false
|
12
|
+
@port = @use_ssl ? 443 : 80 if @port == nil
|
13
|
+
end
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
15
|
+
def notify_subscribers_of_request(path)
|
16
|
+
output = "HTTP=>\n"
|
17
|
+
output += "#{path}\n"
|
18
|
+
output += "#{@request.body}\n"
|
19
|
+
notify_subscribers(output)
|
20
|
+
end
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
def notify_subscribers_of_response
|
23
|
+
output = "<=HTTP\n"
|
24
|
+
output += "#{@response.body}\n"
|
25
|
+
notify_subscribers(output)
|
26
|
+
end
|
26
27
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
28
|
+
def populate_request(path, data)
|
29
|
+
@request.body = data if not data.nil?
|
30
|
+
@request.basic_auth "hetzner_api", "secret"
|
31
|
+
notify_subscribers_of_request(path)
|
32
|
+
end
|
32
33
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
34
|
+
def converse(path, data = nil)
|
35
|
+
populate_request(path, data)
|
36
|
+
@response = connect.request @request
|
37
|
+
notify_subscribers_of_response
|
38
|
+
return @response
|
39
|
+
end
|
39
40
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
41
|
+
def connect
|
42
|
+
if (@use_ssl)
|
43
|
+
Net::HTTP.start(@host, @port, :use_ssl => @use_ssl ? "yes" : "no")
|
44
|
+
else
|
45
|
+
Net::HTTP.start(@host, @port)
|
46
|
+
end
|
45
47
|
end
|
46
|
-
end
|
47
48
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
49
|
+
def ask(path, data = nil)
|
50
|
+
@request = Net::HTTP::Get.new(path)
|
51
|
+
converse(path, data)
|
52
|
+
end
|
52
53
|
|
53
|
-
|
54
|
-
|
55
|
-
|
54
|
+
def say(path, data = nil)
|
55
|
+
@request = Net::HTTP::Post.new(path)
|
56
|
+
converse(path, data)
|
57
|
+
end
|
56
58
|
end
|
57
59
|
end
|
@@ -1,39 +1,41 @@
|
|
1
1
|
require "comms/conversation"
|
2
2
|
require "mysql2"
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
4
|
+
module Converse
|
5
|
+
class MysqlConversation < Conversation
|
6
|
+
attr_writer :username
|
7
|
+
attr_writer :password
|
8
|
+
attr_writer :database
|
9
|
+
attr_accessor :query
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
11
|
+
def initialize(uri)
|
12
|
+
super(uri)
|
13
|
+
parsed = URI.parse(uri)
|
14
|
+
@username = parsed.user
|
15
|
+
@password = parsed.password
|
16
|
+
@database = parsed.path.gsub(/\//, "")
|
17
|
+
end
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
19
|
+
def connect()
|
20
|
+
@client = Mysql2::Client.new(:host => @host,
|
21
|
+
:port => @port.to_f,
|
22
|
+
:username => @username,
|
23
|
+
:password => @password,
|
24
|
+
:database => @database)
|
25
|
+
end
|
25
26
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
27
|
+
def converse
|
28
|
+
connect()
|
29
|
+
@client.query(@query)
|
30
|
+
end
|
30
31
|
|
31
|
-
|
32
|
-
|
33
|
-
|
32
|
+
def ask
|
33
|
+
converse
|
34
|
+
end
|
34
35
|
|
35
|
-
|
36
|
-
|
37
|
-
|
36
|
+
def say
|
37
|
+
converse
|
38
|
+
end
|
38
39
|
|
40
|
+
end
|
39
41
|
end
|
data/lib/converse/interaction.rb
CHANGED
@@ -1,58 +1,61 @@
|
|
1
1
|
require "comms/simple_logger"
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
3
|
+
module Converse
|
4
|
+
|
5
|
+
class Interaction
|
6
|
+
attr_accessor :broker
|
7
|
+
attr_accessor :concern
|
8
|
+
attr_accessor :action
|
9
|
+
attr_accessor :substance
|
10
|
+
attr_accessor :conversation
|
11
|
+
attr_accessor :should_i_ask
|
12
|
+
|
13
|
+
def discuss_with_broker_concerning(broker, concern)
|
14
|
+
discuss_with(broker)
|
15
|
+
concerning(concern)
|
16
|
+
end
|
17
|
+
|
18
|
+
def discuss_with(broker)
|
19
|
+
@broker = broker
|
20
|
+
end
|
21
|
+
|
22
|
+
def concerning(concern)
|
23
|
+
@concern = concern.dup
|
24
|
+
end
|
25
|
+
|
26
|
+
def about(action)
|
27
|
+
@action = action.dup
|
28
|
+
end
|
29
|
+
|
30
|
+
def detailed_by(substance)
|
31
|
+
@substance = substance
|
32
|
+
end
|
33
|
+
|
34
|
+
def by_asking
|
35
|
+
@should_i_ask = true
|
36
|
+
end
|
37
|
+
|
38
|
+
def by_saying
|
39
|
+
@should_i_ask = false
|
40
|
+
end
|
41
|
+
|
42
|
+
def ask
|
43
|
+
@conversation.ask
|
44
|
+
end
|
45
|
+
|
46
|
+
def say
|
47
|
+
@conversation.say
|
48
|
+
end
|
49
|
+
|
50
|
+
def discuss
|
51
|
+
@conversation = broker.broker_conversation(@broker.open_topic(@concern, @action))
|
52
|
+
@conversation.subscribe(SimpleLogger.new)
|
53
|
+
@should_i_ask ? response = ask : response = say
|
54
|
+
interpret_conversation(response)
|
55
|
+
end
|
56
|
+
|
57
|
+
def interpret_conversation(response)
|
58
|
+
response
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|