majortom_connector 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.
- data/.gitignore +5 -0
- data/Gemfile +4 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +43 -0
- data/Rakefile +2 -0
- data/examples/majortom-server.yml +7 -0
- data/lib/majortom_connector/request.rb +76 -0
- data/lib/majortom_connector/result.rb +43 -0
- data/lib/majortom_connector/version.rb +3 -0
- data/lib/majortom_connector.rb +64 -0
- data/majortom_connector.gemspec +24 -0
- metadata +93 -0
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Sven Windisch
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
= MaJorToM-Connector
|
2
|
+
|
3
|
+
This gem provides easy access to any MaJorToM-Server.
|
4
|
+
|
5
|
+
== Requirements
|
6
|
+
|
7
|
+
* HTTParty http://github.com/jnunemaker/httparty/ - For the HTTP requests.
|
8
|
+
|
9
|
+
== Install
|
10
|
+
|
11
|
+
* sudo gem install majortom_connector
|
12
|
+
* Copy the majortom-server.yml from the examples folder into the config folder of your Rails app and configure it to your needs.
|
13
|
+
|
14
|
+
== Usage
|
15
|
+
|
16
|
+
1. Before executing any request, you must connect to the server with
|
17
|
+
MajortomConnector.connect "base_iri_or_map_id"
|
18
|
+
2. Start the topic map madness.
|
19
|
+
* Get the _id_ of the map you are connected with
|
20
|
+
MajortomConnector.topic_map_id
|
21
|
+
* Get all maps that are on the server
|
22
|
+
MajortomConnector.list_maps
|
23
|
+
* Find the id of a map with given base locator
|
24
|
+
MajortomConnector.find_topic_map_id_by_base_iri(base_iri)
|
25
|
+
* Get all topics of the connected map*
|
26
|
+
MajortomConnector.topics
|
27
|
+
* Fire a TMQL query against the server*
|
28
|
+
MajortomConnector.tmql(query)
|
29
|
+
* Fire a SPARQL query against the server
|
30
|
+
MajortomConnector.sparql(query)
|
31
|
+
* Do a search in the fulltext index of the map that was created while loading
|
32
|
+
MajortomConnector.search(query)
|
33
|
+
3. Have fun!
|
34
|
+
|
35
|
+
Note: All methods (except those that just return ids) return a +Result+ object, that carries several values:
|
36
|
+
* +code+ says, whether all went fine (if it's 0) or not
|
37
|
+
* +message+ has a message from the server for you ('OK' or any error message)
|
38
|
+
* +data+ contains the actual data that was returned from the server if no error occured
|
39
|
+
* +jtmqr+ contains the JTMQR that comes with all methods marked with * above
|
40
|
+
|
41
|
+
== Documentation
|
42
|
+
|
43
|
+
* You can find the JTMQR result format there: https://code.google.com/p/tmql/wiki/JTMQR
|
data/Rakefile
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
|
3
|
+
module MajortomConnector
|
4
|
+
class Request
|
5
|
+
|
6
|
+
AvailableCommands = [
|
7
|
+
"topics",
|
8
|
+
"topicmaps",
|
9
|
+
"resolvetm",
|
10
|
+
"xtm",
|
11
|
+
"ctm",
|
12
|
+
"tmql",
|
13
|
+
"sparql",
|
14
|
+
"beru"
|
15
|
+
]
|
16
|
+
|
17
|
+
attr_reader :result
|
18
|
+
|
19
|
+
def self.call(config, command, query)
|
20
|
+
new(config).run(command, query)
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.available_commands
|
24
|
+
const_get(:AvailableCommands)
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.command_available?(command)
|
28
|
+
available_commands.include?(command)
|
29
|
+
end
|
30
|
+
|
31
|
+
def initialize(config)
|
32
|
+
@mts_config = config
|
33
|
+
end
|
34
|
+
|
35
|
+
def run(command, query = "")
|
36
|
+
raise ArgumentError, "Command #{command} not available. Try one of the following: #{const_get(:AvailableCommands).join(',')}" unless self.class.command_available?(command)
|
37
|
+
@mts_result = Result.new
|
38
|
+
post(command, query) if %w[tmql sparql beru].include?(command)
|
39
|
+
get(command, query) if %w[topics topicmaps resolvetm clearcache xtm ctm].include?(command)
|
40
|
+
return @mts_result
|
41
|
+
end
|
42
|
+
|
43
|
+
def successful?
|
44
|
+
@mts_result.successful?
|
45
|
+
end
|
46
|
+
|
47
|
+
protected
|
48
|
+
|
49
|
+
def get(command, query = "")
|
50
|
+
@mts_result.parse(HTTParty.get("#{server_uri}/tm/#{command}#{parameter_builder(command, query)}"))
|
51
|
+
end
|
52
|
+
|
53
|
+
def post(command, query)
|
54
|
+
post_options = {:body => {:query => query, :apikey => @mts_config['user']['api_key']}}
|
55
|
+
@mts_result.parse(HTTParty.post("#{server_uri}/tm/#{command}/#{@mts_config['map']['id']}/", post_options))
|
56
|
+
end
|
57
|
+
|
58
|
+
def xtm
|
59
|
+
end
|
60
|
+
|
61
|
+
def ctm
|
62
|
+
end
|
63
|
+
|
64
|
+
def server_uri
|
65
|
+
"#{@mts_config['server']['host']}:#{@mts_config['server']['port']}/#{@mts_config['server']['context']}"
|
66
|
+
end
|
67
|
+
|
68
|
+
def parameter_builder(command, query = "")
|
69
|
+
parameter = case command
|
70
|
+
when 'topics' then "/#{@mts_config['map']['id']}?"
|
71
|
+
when 'resolvetm' then "?bl=#{query}&"
|
72
|
+
else "?"
|
73
|
+
end << "apikey=#{@mts_config['user']['api_key']}"
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module MajortomConnector
|
2
|
+
class Result
|
3
|
+
|
4
|
+
attr_reader :code
|
5
|
+
|
6
|
+
attr_reader :message
|
7
|
+
|
8
|
+
attr_reader :data
|
9
|
+
|
10
|
+
attr_reader :jtmqr
|
11
|
+
|
12
|
+
def successful?
|
13
|
+
@code == "0" ? true : false
|
14
|
+
end
|
15
|
+
|
16
|
+
def parse (result)
|
17
|
+
@code = result['code']
|
18
|
+
@message = result['msg']
|
19
|
+
@data = result['data']
|
20
|
+
|
21
|
+
if @data.kind_of?(Hash) && @data.has_key?('version')
|
22
|
+
send("handle_jtmqr_v#{@data['version'].to_i}")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
protected
|
27
|
+
|
28
|
+
def handle_jtmqr_v1
|
29
|
+
@jtmqr = Array.new
|
30
|
+
@data['seq'].each do |tupel|
|
31
|
+
cells = Array.new
|
32
|
+
tupel['t'].each do |c|
|
33
|
+
cells << c[c.keys.first]
|
34
|
+
end
|
35
|
+
@jtmqr << cells
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def handle_jtmqr_v2
|
40
|
+
# JTMQR v2 is currently not supported by MaJorToM-Server
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
|
3
|
+
dir = Pathname(__FILE__).dirname.expand_path
|
4
|
+
|
5
|
+
require dir + 'majortom_connector/result'
|
6
|
+
require dir + 'majortom_connector/request'
|
7
|
+
|
8
|
+
module MajortomConnector
|
9
|
+
def self.connect(id_or_base_iri = "")
|
10
|
+
raise ArgumentError, 'Parameter must not be blank' if id_or_base_iri.blank?
|
11
|
+
id = id_or_base_iri.match(/^http/) ? find_topic_map_id_by_base_iri(id_or_base_iri) : id_or_base_iri
|
12
|
+
config.merge!({'map' => {'id' => id}})
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.topic_map_id
|
16
|
+
config['map']['id'] ||= ""
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.list_maps
|
20
|
+
request.run('topicmaps')
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.find_topic_map_id_by_base_iri(base_iri)
|
24
|
+
request.run('resolvetm', base_iri).data
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.topics
|
28
|
+
request.run('topics')
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.tmql(query)
|
32
|
+
request.run('tmql', query)
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.sparql(query)
|
36
|
+
request.run('sparql', query)
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.search(query = "")
|
40
|
+
request.run('beru', query)
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.to_xtm
|
44
|
+
request.run('xtm')
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.to_ctm
|
48
|
+
request.run('ctm')
|
49
|
+
end
|
50
|
+
|
51
|
+
protected
|
52
|
+
|
53
|
+
def self.request
|
54
|
+
@mts_request ||= Request.new(config)
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.config
|
58
|
+
@mts_config ||= load_server_config
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.load_server_config
|
62
|
+
YAML.load_file(File.join(::Rails.root, 'config', 'majortom-server.yml'))
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "majortom_connector/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "majortom_connector"
|
7
|
+
s.version = MajortomConnector::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Sven Windisch"]
|
10
|
+
s.email = ["sven.windisch@googlemail.com"]
|
11
|
+
s.homepage = "https://github.com/semantosoph/majortom_connector"
|
12
|
+
s.summary = %q{Connector gem for any MaJorToM-Server}
|
13
|
+
s.description = %q{Provides easy and configurable access to MaJorToM-Server data stores.}
|
14
|
+
|
15
|
+
s.rubyforge_project = "majortom_connector"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
s.add_dependency('httparty', '>= 0.7.4')
|
23
|
+
s.post_install_message = %q{You have successfully installed the majortom_connector gem. Don't forget to put the majortom-server.yml into rails' config folder.}
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: majortom_connector
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Sven Windisch
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-04-13 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: httparty
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 11
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
- 7
|
33
|
+
- 4
|
34
|
+
version: 0.7.4
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
description: Provides easy and configurable access to MaJorToM-Server data stores.
|
38
|
+
email:
|
39
|
+
- sven.windisch@googlemail.com
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files: []
|
45
|
+
|
46
|
+
files:
|
47
|
+
- .gitignore
|
48
|
+
- Gemfile
|
49
|
+
- MIT-LICENSE
|
50
|
+
- README.rdoc
|
51
|
+
- Rakefile
|
52
|
+
- examples/majortom-server.yml
|
53
|
+
- lib/majortom_connector.rb
|
54
|
+
- lib/majortom_connector/request.rb
|
55
|
+
- lib/majortom_connector/result.rb
|
56
|
+
- lib/majortom_connector/version.rb
|
57
|
+
- majortom_connector.gemspec
|
58
|
+
has_rdoc: true
|
59
|
+
homepage: https://github.com/semantosoph/majortom_connector
|
60
|
+
licenses: []
|
61
|
+
|
62
|
+
post_install_message: You have successfully installed the majortom_connector gem. Don't forget to put the majortom-server.yml into rails' config folder.
|
63
|
+
rdoc_options: []
|
64
|
+
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
hash: 3
|
73
|
+
segments:
|
74
|
+
- 0
|
75
|
+
version: "0"
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
hash: 3
|
82
|
+
segments:
|
83
|
+
- 0
|
84
|
+
version: "0"
|
85
|
+
requirements: []
|
86
|
+
|
87
|
+
rubyforge_project: majortom_connector
|
88
|
+
rubygems_version: 1.3.7
|
89
|
+
signing_key:
|
90
|
+
specification_version: 3
|
91
|
+
summary: Connector gem for any MaJorToM-Server
|
92
|
+
test_files: []
|
93
|
+
|