majortom_connector 0.0.2 → 0.0.3

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/README.rdoc CHANGED
@@ -4,40 +4,52 @@ This gem provides easy access to any MaJorToM-Server.
4
4
 
5
5
  == Requirements
6
6
 
7
- * HTTParty http://github.com/jnunemaker/httparty/ - For the HTTP requests.
7
+ * JSON for ruby - http://flori.github.com/json/
8
8
 
9
9
  == Install
10
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.
11
+ * sudo gem install majortom_connector (mandatory)
12
+ * Copy the majortom-server.yml from the examples folder into the config folder of your Rails app and configure it to your needs (optional).
13
13
 
14
14
  == Usage
15
15
 
16
+ This documentation describes the use of majortom_connector version 0.0.3.
17
+
16
18
  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
+ c = MajortomConnector.connect "base_iri_or_map_id"
20
+ 2. In case you did not provide and edit the config/majortom-server.yml you can configure the connector by calling
21
+ c.config.(host|port|context|api_key|map_id) = your_value
22
+ 3. Check the completeness of your configuration with
23
+ c.ready?
24
+ or check, whether the configured server actually responds with
25
+ c.established?
26
+ 4. Start the topic map madness.
19
27
  * Get the _id_ of the map you are connected with
20
- MajortomConnector.topic_map_id
28
+ c.topic_map_id
21
29
  * Get all maps that are on the server
22
- MajortomConnector.list_maps
30
+ c.list_maps
23
31
  * Find the id of a map with given base locator
24
- MajortomConnector.find_topic_map_id_by_base_iri(base_iri)
32
+ c.find_topic_map_id_by_base_iri(base_iri)
25
33
  * Get all topics of the connected map*
26
- MajortomConnector.topics
34
+ c.topics
27
35
  * Fire a TMQL query against the server*
28
- MajortomConnector.tmql(query)
36
+ c.tmql(query)
29
37
  * Fire a SPARQL query against the server
30
- MajortomConnector.sparql(query)
38
+ c.sparql(query)
31
39
  * 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:
40
+ c.search(query)
41
+ * Receive an XTM or CTM serialization of your map with
42
+ c.to_xtm and c.to_ctm
43
+ * In case you want to clear the query cache, you can call
44
+ c.clear_cache
45
+ 5. Get your data from the connector by calling
46
+ data = c.request.result
47
+ The result object has several values:
48
+ * +http_status+, +http_message+ and +http_body+ represent the data from the HTTP response
36
49
  * +code+ says, whether all went fine (if it's 0) or not
37
50
  * +message+ has a message from the server for you ('OK' or any error message)
38
51
  * +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
52
 
41
- == Documentation
53
+ == External documentation
42
54
 
43
55
  * You can find the JTMQR result format there: https://code.google.com/p/tmql/wiki/JTMQR
@@ -0,0 +1,29 @@
1
+ module MajortomConnector
2
+ class Config
3
+
4
+ attr_accessor :host, :port, :context, :api_key, :map_id
5
+
6
+ def initialize
7
+ load_server_config
8
+ end
9
+
10
+ def ready?
11
+ !(@host.blank? || @port.blank? || @context.blank? || @api_key.blank?)
12
+ end
13
+
14
+ protected
15
+
16
+ def load_server_config
17
+ begin
18
+ # Usual way of loading the configuration is from file.
19
+ file_config = YAML.load_file(File.join(::Rails.root, 'config', 'majortom-server.yml'))
20
+ @host = file_config['server']['host']
21
+ @port = file_config['server']['port']
22
+ @context = file_config['server']['context']
23
+ @api_key = file_config['user']['api_key']
24
+ rescue Exception
25
+ # Wow! The config file was not there!
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,65 @@
1
+ module MajortomConnector
2
+ class Connector
3
+
4
+ def initialize(id_or_base_iri)
5
+ return unless ready?
6
+ config.map_id = id_or_base_iri.match(/^http/) ? find_topic_map_id_by_base_iri(id_or_base_iri) : id_or_base_iri
7
+ end
8
+
9
+ def topic_map_id
10
+ config.map_id ||= ""
11
+ end
12
+
13
+ def list_maps
14
+ request.run('topicmaps') if ready?
15
+ end
16
+
17
+ def find_topic_map_id_by_base_iri(base_iri)
18
+ request.run('resolvetm', base_iri).data if ready?
19
+ end
20
+
21
+ def topics
22
+ request.run('topics') if ready?
23
+ end
24
+
25
+ def tmql(query)
26
+ request.run('tmql', query) if ready?
27
+ end
28
+
29
+ def sparql(query)
30
+ request.run('sparql', query) if ready?
31
+ end
32
+
33
+ def search(query = "")
34
+ request.run('beru', query) if ready?
35
+ end
36
+
37
+ def to_xtm
38
+ request.run('xtm') if ready?
39
+ end
40
+
41
+ def to_ctm
42
+ request.run('ctm') if ready?
43
+ end
44
+
45
+ def clear_cache
46
+ request.run('clearcache') if ready?
47
+ end
48
+
49
+ def ready?
50
+ config.ready?
51
+ end
52
+
53
+ def established?
54
+ ready? && request.run('connectiontest').code == "0"
55
+ end
56
+
57
+ def request
58
+ @request ||= Request.new(config)
59
+ end
60
+
61
+ def config
62
+ @config ||= Config.new
63
+ end
64
+ end
65
+ end
@@ -1,83 +1,82 @@
1
- require 'httparty'
2
- require 'net/http'
3
-
4
1
  module MajortomConnector
5
2
  class Request
6
-
7
- AVAILABLE_COMMANDS = [
8
- "topics",
9
- "topicmaps",
10
- "resolvetm",
11
- "xtm",
12
- "ctm",
13
- "tmql",
14
- "sparql",
15
- "beru",
16
- "clearcache"
17
- ]
3
+
4
+ AVAILABLE_COMMANDS = {
5
+ 'topics' => 'topics',
6
+ 'topicmaps' => 'list_maps',
7
+ 'resolvetm' => 'find_topic_map_id_by_base_iri',
8
+ 'xtm' => 'to_xtm',
9
+ 'ctm' => 'to_ctm',
10
+ 'tmql' => 'tmql',
11
+ 'sparql' => 'sparql',
12
+ 'beru' => 'search',
13
+ 'clearcache' => 'clear_cache',
14
+ 'connectiontest' => 'established?'
15
+ }
18
16
 
19
17
  attr_reader :result
20
18
 
21
- def self.call(config, command, query)
22
- new(config).run(command, query)
23
- end
24
-
19
+
25
20
  def self.available_commands
26
21
  AVAILABLE_COMMANDS
27
22
  end
28
23
 
29
24
  def self.command_available?(command)
30
- available_commands.include?(command)
25
+ available_commands.keys.include?(command)
31
26
  end
32
27
 
33
28
  def initialize(config)
34
- @mts_config = config
29
+ @config = config
35
30
  end
36
31
 
37
32
  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)
39
- @mts_result = Result.new
33
+ raise ArgumentError, "Command #{MajortomConnector::Request.available_commands[command]} not available. Try one of the following: #{MajortomConnector::Request.available_commands.values.join(', ')}" unless self.class.command_available?(command)
34
+ @result = Result.new
40
35
  post(command, param) if %w[tmql sparql beru].include?(command)
41
36
  get(command, param) if %w[topics topicmaps resolvetm clearcache].include?(command)
42
37
  stream(command) if %w[xtm ctm].include?(command)
43
- return @mts_result
38
+ test if %w[connectiontest].include?(command)
39
+ return @result
44
40
  end
45
41
 
46
42
  def successful?
47
- @mts_result.successful?
43
+ @result.result_successful?
48
44
  end
49
-
50
- protected
51
-
45
+
52
46
  def get(command, query = "")
53
- @mts_result.parse(HTTParty.get("#{server_uri}/tm/#{command}#{parameter_builder(command, query)}"))
47
+ @result.parse(Net::HTTP.get_response(URI.parse("#{server_uri}/tm/#{command}#{parameter_builder(command, query)}")))
54
48
  end
55
49
 
56
50
  def post(command, query)
57
- post_options = {:body => {:query => query, :apikey => @mts_config['user']['api_key']}}
58
- @mts_result.parse(HTTParty.post("#{server_uri}/tm/#{command}/#{@mts_config['map']['id']}/", post_options))
51
+ @result.parse(Net::HTTP.post_form(URI.parse("#{server_uri}/tm/#{command}/#{@config.map_id}/"), {:query => query, :apikey => @config.api_key}))
59
52
  end
60
53
 
61
54
  def stream(command)
62
55
  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|
56
+ response = Net::HTTP.get_response(URI.parse("#{server_uri}/tm/#{command}/#{@config.map_id}?apikey=#{@config.api_key}")) do |res|
57
+ res.read_body do |segment|
65
58
  buffer << segment
66
59
  end
67
60
  end
68
- @mts_result.parse({'code' => response.code, 'msg' => response.message, 'data' => buffer})
61
+ @result.parse(response, command, buffer)
62
+ end
63
+
64
+ def test
65
+ @result.parse(Net::HTTP.get_response(URI.parse("#{server_uri}/")), 'html')
69
66
  end
70
67
 
68
+ protected
69
+
71
70
  def server_uri
72
- "#{@mts_config['server']['host']}:#{@mts_config['server']['port']}/#{@mts_config['server']['context']}"
71
+ "#{@config.host}:#{@config.port}/#{@config.context}"
73
72
  end
74
73
 
75
74
  def parameter_builder(command, query = "")
76
75
  parameter = case command
77
- when 'topics', 'clearcache' then "/#{@mts_config['map']['id']}?"
76
+ when 'topics', 'clearcache' then "/#{@config.map_id}?"
78
77
  when 'resolvetm' then "?bl=#{query}&"
79
78
  else "?"
80
- end << "apikey=#{@mts_config['user']['api_key']}"
79
+ end << "apikey=#{@config.api_key}"
81
80
  end
82
81
  end
83
82
  end
@@ -1,43 +1,43 @@
1
+ require 'json'
2
+
1
3
  module MajortomConnector
2
4
  class Result
3
5
 
4
- attr_reader :code
6
+ attr_reader :http_status
7
+ attr_reader :http_message
8
+ attr_reader :http_body
5
9
 
10
+ attr_reader :code
6
11
  attr_reader :message
7
-
8
12
  attr_reader :data
9
13
 
10
- attr_reader :jtmqr
11
-
12
- def successful?
14
+ def request_successful?
15
+ @http_status == "200" ? true : false
16
+ end
17
+
18
+ def response_successful?
13
19
  @code == "0" ? true : false
14
20
  end
15
21
 
16
- def parse (result)
17
- @code = %w[0 200].include?(result['code']) ? "0" : result['code']
18
- @message = result['msg']
19
- @data = @code == "0" ? 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
22
+ def parse(response, format = "", buffer = nil)
23
+ @http_status = response.code
24
+ @http_message = response.message
25
+ @http_body = response.read_body
25
26
 
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
27
+ return unless @http_status == "200"
28
+
29
+ case format
30
+ when 'xtm', 'ctm'
31
+ @code = "0"
32
+ @data = buffer
33
+ when 'html'
34
+ @code = "0"
35
+ else
36
+ json = JSON.parse(@http_body)
37
+ @data = json['data']
38
+ @code = json['code']
39
+ @message = json['msg']
36
40
  end
37
41
  end
38
-
39
- def handle_jtmqr_v2
40
- # JTMQR v2 is currently not supported by MaJorToM-Server
41
- end
42
42
  end
43
43
  end
@@ -1,3 +1,3 @@
1
1
  module MajortomConnector
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -1,68 +1,12 @@
1
- require 'pathname'
2
-
3
- dir = Pathname(__FILE__).dirname.expand_path
4
-
5
- require dir + 'majortom_connector/result'
6
- require dir + 'majortom_connector/request'
1
+ require 'majortom_connector/config'
2
+ require 'majortom_connector/connector'
3
+ require 'majortom_connector/result'
4
+ require 'majortom_connector/request'
7
5
 
8
6
  module MajortomConnector
7
+
9
8
  def self.connect(id_or_base_iri = "")
10
9
  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
- def self.clear_cache
52
- request.run('clearcache')
53
- end
54
-
55
- protected
56
-
57
- def self.request
58
- @mts_request ||= Request.new(config)
59
- end
60
-
61
- def self.config
62
- @mts_config ||= load_server_config
63
- end
64
-
65
- def self.load_server_config
66
- YAML.load_file(File.join(::Rails.root, 'config', 'majortom-server.yml'))
10
+ connector = Connector.new(id_or_base_iri)
67
11
  end
68
12
  end
@@ -19,6 +19,5 @@ Gem::Specification.new do |s|
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ["lib"]
21
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.}
22
+ s.add_dependency('json', '>= 1.5.1')
24
23
  end
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: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sven Windisch
@@ -15,23 +15,23 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-14 00:00:00 +02:00
18
+ date: 2011-04-17 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
- name: httparty
22
+ name: json
23
23
  prerelease: false
24
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- hash: 11
29
+ hash: 1
30
30
  segments:
31
- - 0
32
- - 7
33
- - 4
34
- version: 0.7.4
31
+ - 1
32
+ - 5
33
+ - 1
34
+ version: 1.5.1
35
35
  type: :runtime
36
36
  version_requirements: *id001
37
37
  description: Provides easy and configurable access to MaJorToM-Server data stores.
@@ -51,6 +51,8 @@ files:
51
51
  - Rakefile
52
52
  - examples/majortom-server.yml
53
53
  - lib/majortom_connector.rb
54
+ - lib/majortom_connector/config.rb
55
+ - lib/majortom_connector/connector.rb
54
56
  - lib/majortom_connector/request.rb
55
57
  - lib/majortom_connector/result.rb
56
58
  - lib/majortom_connector/version.rb
@@ -59,7 +61,7 @@ has_rdoc: true
59
61
  homepage: https://github.com/semantosoph/majortom_connector
60
62
  licenses: []
61
63
 
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.
64
+ post_install_message:
63
65
  rdoc_options: []
64
66
 
65
67
  require_paths: