majortom_connector 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
@@ -1,9 +1,10 @@
|
|
1
1
|
require 'httparty'
|
2
|
+
require 'net/http'
|
2
3
|
|
3
4
|
module MajortomConnector
|
4
5
|
class Request
|
5
6
|
|
6
|
-
|
7
|
+
AVAILABLE_COMMANDS = [
|
7
8
|
"topics",
|
8
9
|
"topicmaps",
|
9
10
|
"resolvetm",
|
@@ -11,7 +12,8 @@ module MajortomConnector
|
|
11
12
|
"ctm",
|
12
13
|
"tmql",
|
13
14
|
"sparql",
|
14
|
-
"beru"
|
15
|
+
"beru",
|
16
|
+
"clearcache"
|
15
17
|
]
|
16
18
|
|
17
19
|
attr_reader :result
|
@@ -21,7 +23,7 @@ module MajortomConnector
|
|
21
23
|
end
|
22
24
|
|
23
25
|
def self.available_commands
|
24
|
-
|
26
|
+
AVAILABLE_COMMANDS
|
25
27
|
end
|
26
28
|
|
27
29
|
def self.command_available?(command)
|
@@ -32,11 +34,12 @@ module MajortomConnector
|
|
32
34
|
@mts_config = config
|
33
35
|
end
|
34
36
|
|
35
|
-
def run(command,
|
36
|
-
raise ArgumentError, "Command #{command} not available. Try one of the following: #{
|
37
|
+
def run(command, param = "")
|
38
|
+
raise ArgumentError, "Command #{command} not available. Try one of the following: #{MajortomConnector::Request.available_commands.join(', ')}" unless self.class.command_available?(command)
|
37
39
|
@mts_result = Result.new
|
38
|
-
post(command,
|
39
|
-
get(command,
|
40
|
+
post(command, param) if %w[tmql sparql beru].include?(command)
|
41
|
+
get(command, param) if %w[topics topicmaps resolvetm clearcache].include?(command)
|
42
|
+
stream(command) if %w[xtm ctm].include?(command)
|
40
43
|
return @mts_result
|
41
44
|
end
|
42
45
|
|
@@ -55,10 +58,14 @@ module MajortomConnector
|
|
55
58
|
@mts_result.parse(HTTParty.post("#{server_uri}/tm/#{command}/#{@mts_config['map']['id']}/", post_options))
|
56
59
|
end
|
57
60
|
|
58
|
-
def
|
59
|
-
|
60
|
-
|
61
|
-
|
61
|
+
def stream(command)
|
62
|
+
buffer = ""
|
63
|
+
response = Net::HTTP.get_response(URI.parse("#{@mts_config['server']['host']}:#{@mts_config['server']['port']}/#{@mts_config['server']['context']}/tm/#{command}/#{@mts_config['map']['id']}?apikey=#{@mts_config['user']['api_key']}")) do |response|
|
64
|
+
response.read_body do |segment|
|
65
|
+
buffer << segment
|
66
|
+
end
|
67
|
+
end
|
68
|
+
@mts_result.parse({'code' => response.code, 'msg' => response.message, 'data' => buffer})
|
62
69
|
end
|
63
70
|
|
64
71
|
def server_uri
|
@@ -67,7 +74,7 @@ module MajortomConnector
|
|
67
74
|
|
68
75
|
def parameter_builder(command, query = "")
|
69
76
|
parameter = case command
|
70
|
-
when 'topics' then "/#{@mts_config['map']['id']}?"
|
77
|
+
when 'topics', 'clearcache' then "/#{@mts_config['map']['id']}?"
|
71
78
|
when 'resolvetm' then "?bl=#{query}&"
|
72
79
|
else "?"
|
73
80
|
end << "apikey=#{@mts_config['user']['api_key']}"
|
@@ -14,9 +14,9 @@ module MajortomConnector
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def parse (result)
|
17
|
-
@code = result['code']
|
17
|
+
@code = %w[0 200].include?(result['code']) ? "0" : result['code']
|
18
18
|
@message = result['msg']
|
19
|
-
@data = result['data']
|
19
|
+
@data = @code == "0" ? result['data'] : ""
|
20
20
|
|
21
21
|
if @data.kind_of?(Hash) && @data.has_key?('version')
|
22
22
|
send("handle_jtmqr_v#{@data['version'].to_i}")
|
data/lib/majortom_connector.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: majortom_connector
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Sven Windisch
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-04-
|
18
|
+
date: 2011-04-14 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|