active_triple 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Rob Nichols
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,119 @@
1
+ ActiveTriple: a tool to search a triple store
2
+ =============================================
3
+
4
+ Configuration
5
+ -------------
6
+ Before you can use ActiveTriple, you must set up a connector and
7
+ associate it with ActiveTriple.
8
+
9
+ ActiveTriple.set_connector(MyConnector)
10
+
11
+ Two connector templates are provided:
12
+
13
+ ActiveTriple::Connectors::TripleStoreConnector
14
+ ---------------------------------------------
15
+
16
+ A very basic connector, that shows the minimum requirement for a connector.
17
+
18
+ ActiveTriple::Connectors::PostToUrlConnector
19
+ ----------------------------------------------
20
+ This connector provides a connection to a server via HTTP post. If you have
21
+ a server that will return json data to a posted request, you can inherit
22
+ from this class to make a simple connector. For example:
23
+
24
+ class MyConnector < ActiveTriple::Connectors::PostToUrlConnector
25
+ def path
26
+ @path = 'http://path/to/the/server'
27
+ end
28
+ end
29
+
30
+ ActiveTriple.set_connector MyConnector
31
+
32
+
33
+ Usage
34
+ -----
35
+
36
+ To search for items:
37
+
38
+ near a given location:
39
+
40
+ ActiveTriple.location('London')
41
+
42
+ within 30km of a location
43
+
44
+ ActiveTriple.location('London', '30km')
45
+
46
+ about a resource
47
+
48
+ ActiveTriple.about('London')
49
+
50
+ mentions a resource
51
+
52
+ ActiveTriple.mentions('London')
53
+
54
+ with a title of "This article"
55
+
56
+ ActiveTriple.title('This article')
57
+
58
+ Using where
59
+ -----------
60
+
61
+ The title query can also be made using a where clause, with the key matching
62
+ the predicate, and the value matching the object. The where method accepts
63
+ both colon delimited style triples and bracketed url style triples
64
+
65
+ ActiveTriple.where('dc:terms:title' => 'text:en:"This article"')
66
+
67
+ or
68
+
69
+ ActiveTriple.where('<http://purl.org/dc/terms/title>' => '"This article"@en')
70
+
71
+ Combining queries
72
+ -----------------
73
+
74
+ ActiveTriple.location('Evesham', '200km').mentions('London')
75
+
76
+ Limiting queries
77
+ ----------------
78
+
79
+ By default, queries are limited to return ten items. This can be overwritten.
80
+
81
+ ActiveTriple.location('London').limit(2)
82
+ ActiveTriple.limit(20).location('London')
83
+
84
+ Accessing the data
85
+ ------------------
86
+
87
+ Each of the above searches will return an ActiveTriple instance. To access
88
+ the data returned by the query, either use 'all' or use an Array method.
89
+
90
+ Get all the items:
91
+
92
+ ActiveTriple.location('London').all
93
+
94
+ Get the title of each of the items:
95
+
96
+ ActiveTriple.location('London').collect{|a| a.title}
97
+
98
+ Items returned as objects
99
+ -------------------------
100
+
101
+ Notice in the last example, that the item is an object with a method title.
102
+ Hashie is used to convert the data from it's native hash to an object. The
103
+ native hash is nested and this is reflected in the structure of the object.
104
+
105
+ To return the body text of the first item:
106
+
107
+ ActiveTriple.location('London').first.full_data.body
108
+
109
+ To return the original hash:
110
+
111
+ ActiveTriple.location('London').first.to_hash
112
+
113
+ No items found
114
+ --------------
115
+ If the query returns no items, an empty array will be returned when an attempt
116
+ is made to access the data.
117
+
118
+ ActiveTriple.mentions('XXXXYYYY').all ---> []
119
+
@@ -0,0 +1,83 @@
1
+ require_relative 'active_triple/search'
2
+ require_relative 'active_triple/connectors/connectors.rb'
3
+ require 'json'
4
+ require 'triple_parser'
5
+
6
+ class ActiveTriple
7
+ def initialize
8
+
9
+ end
10
+
11
+ @@connector = Connectors::PostToUrlConnector
12
+
13
+ def method_missing(m, *args, &block)
14
+ if Search.respond_to?(m)
15
+ statements = Search.send(m, *args)
16
+ add_triple(statements)
17
+ elsif Array.new.respond_to?(m)
18
+ get_data.send(m, *args, &block)
19
+ else
20
+ super
21
+ end
22
+ end
23
+
24
+ def self.method_missing(m, *args)
25
+ if Search.respond_to?(m) || m.to_sym == :limit
26
+ active_triple = new
27
+ active_triple.send(m, *args)
28
+ else
29
+ super
30
+ end
31
+ end
32
+
33
+ def self.set_connector(connector)
34
+ @@connector = connector
35
+ end
36
+
37
+ def self.connector
38
+ @@connector
39
+ end
40
+
41
+
42
+ def add_triple(statement)
43
+ @triples ||= Array.new
44
+ @triples << statement
45
+ @triples.flatten!
46
+ return self
47
+ end
48
+
49
+ def triples
50
+ TripleParser.to_rdf(@triples).join("\n")
51
+ end
52
+
53
+ def self.binding_id
54
+ "output"
55
+ end
56
+
57
+ def self.binding_variable
58
+ "?#{binding_id}"
59
+ end
60
+
61
+ def limit(number_of_items)
62
+ @number_of_items = number_of_items
63
+ return self
64
+ end
65
+
66
+ def number_of_items
67
+ @number_of_items || '10'
68
+ end
69
+
70
+ def all
71
+ get_data
72
+ end
73
+
74
+ def get_data
75
+ connection = self.class.connector.send_data(
76
+ :binding => self.class.binding_id,
77
+ :limit => number_of_items,
78
+ :triples => triples
79
+ )
80
+ connection.response
81
+ end
82
+
83
+ end
@@ -0,0 +1,3 @@
1
+ module Connectors
2
+ Dir[File.dirname(__FILE__) + '/*.rb'].each {|file| require file }
3
+ end
@@ -0,0 +1,64 @@
1
+ require_relative 'triple_store_connector'
2
+ require 'typhoeus'
3
+ require 'hashie'
4
+
5
+ class ActiveTriple
6
+ module Connectors
7
+ class PostToUrlConnector < TripleStoreConnector
8
+ attr_reader :url_variables, :triples
9
+
10
+
11
+ def self.send_data(args)
12
+ connector = new
13
+ connector.send_by_post(args)
14
+ return connector
15
+ end
16
+
17
+ def response
18
+ resp = by_post
19
+ begin
20
+ json = JSON.parse(resp.body)
21
+ results = json.first[1]
22
+ results.collect!{|a| Hashie::Mash.new(a)}
23
+ return results
24
+ rescue JSON::ParserError => e
25
+ if /No stories found for query/ =~ e.message
26
+ return Array.new
27
+ else
28
+ raise e
29
+ end
30
+ end
31
+ end
32
+
33
+ def send_by_post(args)
34
+ @triples = args.delete(:triples)
35
+ set_url_variables(args)
36
+ end
37
+
38
+ def path
39
+ raise 'Need to define path to server'
40
+ end
41
+
42
+ def set_url_variables(variables = {})
43
+ @url_variables = build_varible_string(variables)
44
+ end
45
+
46
+ def url
47
+ [path, url_variables].join
48
+ end
49
+
50
+ private
51
+ def by_post
52
+ Typhoeus::Request.post(url, {
53
+ :headers => { 'Content-Type' => 'text/plain' },
54
+ :body => triples
55
+ })
56
+ end
57
+
58
+ def build_varible_string(variables)
59
+ parts = variables.to_a.collect{|v| v.join("=")}
60
+ "?#{parts.join('&')}"
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,31 @@
1
+ class ActiveTriple
2
+ module Connectors
3
+
4
+ class TripleStoreConnector
5
+
6
+ def initialize
7
+
8
+ end
9
+
10
+ # Method used to initial a connection and send data.
11
+ def self.send_data(args = {})
12
+ required_arguments.each{|arg| raise "args must include :#{arg}" unless args.keys.include?(arg.to_sym)}
13
+ raise "Need to define how connector sends data to server"
14
+ end
15
+
16
+ # Response should be an array of objects holding the data returned by the server
17
+ # or an empty array
18
+ def response
19
+ raise "Need to define how connector handles response"
20
+ end
21
+
22
+ private
23
+ def self.required_arguments
24
+ %w{triples binding limit}
25
+ end
26
+
27
+
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,41 @@
1
+ class ActiveTriple
2
+ module Search
3
+
4
+ # At the moment, I think this is the only search method that is working.
5
+ def self.location(place_name, radius = '8km')
6
+ [
7
+ %Q{resource:#{place_name} geo-pos:lat ?Latitude .},
8
+ %Q{resource:#{place_name} geo-pos:long ?Longitude .},
9
+ %Q{?location omgeo:nearby(?Latitude ?Longitude "#{radius}") .},
10
+ %Q{#{ActiveTriple.binding_variable} ontology:about ?location .}
11
+ ]
12
+ end
13
+
14
+ def self.about(text)
15
+ %Q{#{ActiveTriple.binding_variable} ontology:about resource:#{underscore_spaces(text)} .}
16
+ end
17
+
18
+ def self.mentions(text)
19
+ %Q{#{ActiveTriple.binding_variable} ontology:mentions resource:#{underscore_spaces(text)} .}
20
+ end
21
+
22
+ def self.subject(text)
23
+ %Q{#{ActiveTriple.binding_variable} dc:terms:subject resource:#{text} .}
24
+ end
25
+
26
+ # ActiveTriple.where('dc:terms:subject' => 'resource:London') is equivalent to ActiveTriple.subject('London')
27
+ def self.where(hash)
28
+ statements = Array.new
29
+ hash.each{|predicate, object| statements << %Q{#{ActiveTriple.binding_variable} #{predicate} #{object} .}}
30
+ return statements
31
+ end
32
+
33
+ def self.title(text)
34
+ %Q{#{ActiveTriple.binding_variable} dc:terms:title text:en:"#{text}" .}
35
+ end
36
+
37
+ def self.underscore_spaces(text)
38
+ text.gsub(/\s+/, "_")
39
+ end
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_triple
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rob Nichols
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-07 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: hashie
16
+ requirement: &10204240 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.2.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *10204240
25
+ - !ruby/object:Gem::Dependency
26
+ name: json
27
+ requirement: &10203780 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *10203780
36
+ - !ruby/object:Gem::Dependency
37
+ name: triple_parser
38
+ requirement: &10203200 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *10203200
47
+ - !ruby/object:Gem::Dependency
48
+ name: typhoeus
49
+ requirement: &10202660 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *10202660
58
+ description: Active Triple - Tool to query triple stores
59
+ email: rob@undervale.co.uk
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - README.md
65
+ - LICENSE
66
+ - lib/active_triple/connectors/post_to_url_connector.rb
67
+ - lib/active_triple/connectors/connectors.rb
68
+ - lib/active_triple/connectors/triple_store_connector.rb
69
+ - lib/active_triple/search.rb
70
+ - lib/active_triple.rb
71
+ homepage: https://github.com/reggieb/active_triple
72
+ licenses: []
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 1.8.10
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: Active Triple - Tool to query triple stores
95
+ test_files: []