converse 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/api.rb +7 -0
- data/lib/broker.rb +13 -0
- data/lib/comms/conversation.rb +39 -0
- data/lib/comms/html/html_conversation.rb +57 -0
- data/lib/comms/mysql/mysql_conversation.rb +39 -0
- data/lib/interaction.rb +58 -0
- metadata +70 -0
data/lib/api.rb
ADDED
data/lib/broker.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
class Conversation
|
2
|
+
attr_accessor :uri
|
3
|
+
attr_accessor :body
|
4
|
+
attr_accessor :headers
|
5
|
+
attr_accessor :connection
|
6
|
+
attr_accessor :host
|
7
|
+
attr_accessor :port
|
8
|
+
attr_accessor :path
|
9
|
+
attr_accessor :request
|
10
|
+
attr_accessor :response
|
11
|
+
attr_accessor :subscribers
|
12
|
+
|
13
|
+
def initialize(uri)
|
14
|
+
@uri = uri
|
15
|
+
parsed = URI.parse(uri)
|
16
|
+
@host = parsed.host
|
17
|
+
@port = parsed.port
|
18
|
+
@path = parsed.path
|
19
|
+
@subscribers = []
|
20
|
+
end
|
21
|
+
|
22
|
+
def ask
|
23
|
+
raise NotImplementedError.new
|
24
|
+
end
|
25
|
+
|
26
|
+
def say
|
27
|
+
raise NotImplementedError.new
|
28
|
+
end
|
29
|
+
|
30
|
+
def subscribe(subscriber)
|
31
|
+
@subscribers << subscriber
|
32
|
+
end
|
33
|
+
|
34
|
+
def notify_subscribers(data)
|
35
|
+
@subscribers.each do |subscriber|
|
36
|
+
subscriber.notify(data)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require "comms/conversation"
|
3
|
+
require "json"
|
4
|
+
|
5
|
+
class HTMLConversation < Conversation
|
6
|
+
attr_accessor :use_ssl
|
7
|
+
|
8
|
+
def initialize(uri)
|
9
|
+
super(uri)
|
10
|
+
@use_ssl = false
|
11
|
+
@port = @use_ssl ? 443 : 80 if @port == nil
|
12
|
+
end
|
13
|
+
|
14
|
+
def notify_subscribers_of_request(path)
|
15
|
+
output = "HTTP=>\n"
|
16
|
+
output += "#{path}\n"
|
17
|
+
output += "#{@request.body}\n"
|
18
|
+
notify_subscribers(output)
|
19
|
+
end
|
20
|
+
|
21
|
+
def notify_subscribers_of_response
|
22
|
+
output = "<=HTTP\n"
|
23
|
+
output += "#{@response.body}\n"
|
24
|
+
notify_subscribers(output)
|
25
|
+
end
|
26
|
+
|
27
|
+
def populate_request(path, data)
|
28
|
+
@request.body = data if not data.nil?
|
29
|
+
@request.basic_auth "hetzner_api", "secret"
|
30
|
+
notify_subscribers_of_request(path)
|
31
|
+
end
|
32
|
+
|
33
|
+
def converse(path, data = nil)
|
34
|
+
populate_request(path, data)
|
35
|
+
@response = connect.request @request
|
36
|
+
notify_subscribers_of_response
|
37
|
+
return @response
|
38
|
+
end
|
39
|
+
|
40
|
+
def connect
|
41
|
+
if (@use_ssl)
|
42
|
+
Net::HTTP.start(@host, @port, :use_ssl => @use_ssl ? "yes" : "no")
|
43
|
+
else
|
44
|
+
Net::HTTP.start(@host, @port)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def ask(path, data = nil)
|
49
|
+
@request = Net::HTTP::Get.new(path)
|
50
|
+
converse(path, data)
|
51
|
+
end
|
52
|
+
|
53
|
+
def say(path, data = nil)
|
54
|
+
@request = Net::HTTP::Post.new(path)
|
55
|
+
converse(path, data)
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require "comms/conversation"
|
2
|
+
require "mysql2"
|
3
|
+
|
4
|
+
class MysqlConversation < Conversation
|
5
|
+
attr_writer :username
|
6
|
+
attr_writer :password
|
7
|
+
attr_writer :database
|
8
|
+
attr_accessor :query
|
9
|
+
|
10
|
+
def initialize(uri)
|
11
|
+
super(uri)
|
12
|
+
parsed = URI.parse(uri)
|
13
|
+
@username = parsed.user
|
14
|
+
@password = parsed.password
|
15
|
+
@database = parsed.path.gsub(/\//, "")
|
16
|
+
end
|
17
|
+
|
18
|
+
def connect()
|
19
|
+
@client = Mysql2::Client.new(:host => @host,
|
20
|
+
:port => @port.to_f,
|
21
|
+
:username => @username,
|
22
|
+
:password => @password,
|
23
|
+
:database => @database)
|
24
|
+
end
|
25
|
+
|
26
|
+
def converse
|
27
|
+
connect()
|
28
|
+
@client.query(@query)
|
29
|
+
end
|
30
|
+
|
31
|
+
def ask
|
32
|
+
converse
|
33
|
+
end
|
34
|
+
|
35
|
+
def say
|
36
|
+
converse
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
data/lib/interaction.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require "comms/simple_logger"
|
2
|
+
|
3
|
+
class Interaction
|
4
|
+
attr_accessor :broker
|
5
|
+
attr_accessor :concern
|
6
|
+
attr_accessor :action
|
7
|
+
attr_accessor :substance
|
8
|
+
attr_accessor :conversation
|
9
|
+
attr_accessor :should_i_ask
|
10
|
+
|
11
|
+
def discuss_with_broker_concerning(broker, concern)
|
12
|
+
discuss_with(broker)
|
13
|
+
concerning(concern)
|
14
|
+
end
|
15
|
+
|
16
|
+
def discuss_with(broker)
|
17
|
+
@broker = broker
|
18
|
+
end
|
19
|
+
|
20
|
+
def concerning(concern)
|
21
|
+
@concern = concern.dup
|
22
|
+
end
|
23
|
+
|
24
|
+
def about(action)
|
25
|
+
@action = action.dup
|
26
|
+
end
|
27
|
+
|
28
|
+
def detailed_by(substance)
|
29
|
+
@substance = substance
|
30
|
+
end
|
31
|
+
|
32
|
+
def by_asking
|
33
|
+
@should_i_ask = true
|
34
|
+
end
|
35
|
+
|
36
|
+
def by_saying
|
37
|
+
@should_i_ask = false
|
38
|
+
end
|
39
|
+
|
40
|
+
def ask
|
41
|
+
@conversation.ask
|
42
|
+
end
|
43
|
+
|
44
|
+
def say
|
45
|
+
@conversation.say
|
46
|
+
end
|
47
|
+
|
48
|
+
def discuss
|
49
|
+
@conversation = broker.broker_conversation(@broker.open_topic(@concern, @action))
|
50
|
+
@conversation.subscribe(SimpleLogger.new)
|
51
|
+
@should_i_ask ? response = ask : response = say
|
52
|
+
interpret_conversation(response)
|
53
|
+
end
|
54
|
+
|
55
|
+
def interpret_conversation(response)
|
56
|
+
response
|
57
|
+
end
|
58
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: converse
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 19
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 2
|
10
|
+
version: 1.0.2
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Ernst van Graan
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-08-23 00:00:00 Z
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description:
|
22
|
+
email:
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- lib/api.rb
|
31
|
+
- lib/interaction.rb
|
32
|
+
- lib/broker.rb
|
33
|
+
- lib/comms/conversation.rb
|
34
|
+
- lib/comms/html/html_conversation.rb
|
35
|
+
- lib/comms/mysql/mysql_conversation.rb
|
36
|
+
homepage:
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
|
42
|
+
require_paths:
|
43
|
+
- lib/comms/mysql
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
hash: 3
|
50
|
+
segments:
|
51
|
+
- 0
|
52
|
+
version: "0"
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 3
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
62
|
+
requirements: []
|
63
|
+
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.8.24
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: Converse provides Broker/Translator classes to facilitate communication across an API by means of conversations
|
69
|
+
test_files: []
|
70
|
+
|