monkeyhelper-sameasdotorg_fu 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/.document +5 -0
- data/.gitignore +6 -0
- data/LICENSE +20 -0
- data/README.rdoc +67 -0
- data/Rakefile +56 -0
- data/VERSION +1 -0
- data/examples/term.rb +12 -0
- data/examples/uri.rb +10 -0
- data/lib/sameas/base.rb +135 -0
- data/lib/sameas/results.rb +21 -0
- data/lib/sameasdotorg_fu.rb +48 -0
- data/test/sameasdotorg_fu_test.rb +7 -0
- data/test/test_helper.rb +10 -0
- metadata +69 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Rattle (http://www.rattlecentral.com)
|
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,67 @@
|
|
1
|
+
= sameasdotorg_fu
|
2
|
+
|
3
|
+
Allows querying of the http://sameas.org site using ruby
|
4
|
+
|
5
|
+
== Sample Usage
|
6
|
+
|
7
|
+
Querying a URI/resource
|
8
|
+
|
9
|
+
#!/usr/bin/ruby
|
10
|
+
|
11
|
+
require 'rubygems'
|
12
|
+
require 'sameasdotorg_fu'
|
13
|
+
|
14
|
+
sameas = Sameas.new()
|
15
|
+
results = sameas.find_by_uri('http://dbpedia.org/resource/Otley')
|
16
|
+
|
17
|
+
puts "For #{results.uri} :"
|
18
|
+
pp results.duplicates
|
19
|
+
|
20
|
+
Outputs:
|
21
|
+
|
22
|
+
For http://dbpedia.org/resource/Otley :
|
23
|
+
["http://dbpedia.org/resource/Otley",
|
24
|
+
"http://mpii.de/yago/resource/Otley",
|
25
|
+
"http://rdf.freebase.com/ns/guid.9202a8c04000641f800000000011e4f9",
|
26
|
+
"http://sws.geonames.org/2640857/",
|
27
|
+
"http://umbel.org/umbel/ne/wikipedia/Otley"]
|
28
|
+
|
29
|
+
Querying a term
|
30
|
+
|
31
|
+
#!/usr/bin/ruby
|
32
|
+
|
33
|
+
require 'rubygems'
|
34
|
+
require 'sameasdotorg_fu'
|
35
|
+
|
36
|
+
sameas = Sameas.new()
|
37
|
+
results = sameas.find_by_term('Otley')
|
38
|
+
|
39
|
+
results.each do |result|
|
40
|
+
puts "For #{result.label} :"
|
41
|
+
pp result.duplicates
|
42
|
+
end
|
43
|
+
|
44
|
+
Outputs:
|
45
|
+
|
46
|
+
For http://dbpedia.org/resource/Otley :
|
47
|
+
["http://dbpedia.org/resource/Otley",
|
48
|
+
"http://mpii.de/yago/resource/Otley",
|
49
|
+
"http://rdf.freebase.com/ns/guid.9202a8c04000641f800000000011e4f9",
|
50
|
+
"http://sws.geonames.org/2640857/",
|
51
|
+
"http://umbel.org/umbel/ne/wikipedia/Otley"]
|
52
|
+
For Otley R.U.F.C. :
|
53
|
+
["http://dbpedia.org/resource/Otley_R.U.F.C.",
|
54
|
+
"http://mpii.de/yago/resource/Otley_R.U.F.C.",
|
55
|
+
"http://rdf.freebase.com/ns/guid.9202a8c04000641f8000000000d0130d",
|
56
|
+
"http://umbel.org/umbel/ne/wikipedia/Otley_R.U.F.C."]
|
57
|
+
For Otley Run :
|
58
|
+
["http://dbpedia.org/resource/Otley_Run",
|
59
|
+
"http://mpii.de/yago/resource/Otley_Run",
|
60
|
+
"http://rdf.freebase.com/ns/guid.9202a8c04000641f800000000098bef8"]
|
61
|
+
<snip></snip>
|
62
|
+
|
63
|
+
For further details see the examples dir
|
64
|
+
|
65
|
+
== Copyright
|
66
|
+
|
67
|
+
Copyright (c) 2009 Rattle (http://www.rattlecentral.com). See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "sameasdotorg_fu"
|
8
|
+
gem.summary = %Q{TODO}
|
9
|
+
gem.email = "robl@monkeyhelper.com"
|
10
|
+
gem.homepage = "http://github.com/monkeyhelper/sameasdotorg_fu"
|
11
|
+
gem.authors = ["robl"]
|
12
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
13
|
+
end
|
14
|
+
|
15
|
+
rescue LoadError
|
16
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'rake/testtask'
|
20
|
+
Rake::TestTask.new(:test) do |test|
|
21
|
+
test.libs << 'lib' << 'test'
|
22
|
+
test.pattern = 'test/**/*_test.rb'
|
23
|
+
test.verbose = true
|
24
|
+
end
|
25
|
+
|
26
|
+
begin
|
27
|
+
require 'rcov/rcovtask'
|
28
|
+
Rcov::RcovTask.new do |test|
|
29
|
+
test.libs << 'test'
|
30
|
+
test.pattern = 'test/**/*_test.rb'
|
31
|
+
test.verbose = true
|
32
|
+
end
|
33
|
+
rescue LoadError
|
34
|
+
task :rcov do
|
35
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
task :default => :test
|
41
|
+
|
42
|
+
require 'rake/rdoctask'
|
43
|
+
Rake::RDocTask.new do |rdoc|
|
44
|
+
if File.exist?('VERSION.yml')
|
45
|
+
config = YAML.load(File.read('VERSION.yml'))
|
46
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
47
|
+
else
|
48
|
+
version = ""
|
49
|
+
end
|
50
|
+
|
51
|
+
rdoc.rdoc_dir = 'rdoc'
|
52
|
+
rdoc.title = "sameasdotorg_fu #{version}"
|
53
|
+
rdoc.rdoc_files.include('README*')
|
54
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
55
|
+
end
|
56
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/examples/term.rb
ADDED
data/examples/uri.rb
ADDED
data/lib/sameas/base.rb
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
module Sameas
|
2
|
+
|
3
|
+
def self.new(options = nil)
|
4
|
+
Sameas::Base.new(options)
|
5
|
+
end
|
6
|
+
|
7
|
+
class Base
|
8
|
+
class_attr_accessor :http_open_timeout
|
9
|
+
class_attr_accessor :http_read_timeout
|
10
|
+
attr_accessor :api_endpoint
|
11
|
+
|
12
|
+
@@http_open_timeout = 60
|
13
|
+
@@http_read_timeout = 60
|
14
|
+
|
15
|
+
API_ENDPOINT = 'http://www.sameas.org/json'
|
16
|
+
|
17
|
+
# create a new sameas object
|
18
|
+
#
|
19
|
+
def initialize(config_hash_or_file)
|
20
|
+
return if config_hash_or_file.nil?
|
21
|
+
if config_hash_or_file.is_a? Hash
|
22
|
+
config_hash_or_file.nested_symbolize_keys!
|
23
|
+
@api_endpoint = config_hash_or_file[:api_endpoint]
|
24
|
+
else
|
25
|
+
config = YAML.load_file(config_hash_or_file)
|
26
|
+
config.nested_symbolize_keys!
|
27
|
+
@api_endpoint = config[:api_endpoint]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# sends a request to the sameas api
|
32
|
+
#
|
33
|
+
# Params
|
34
|
+
# * api_url (Required)
|
35
|
+
# the request url (uri.path)
|
36
|
+
# * http_method (Optional)
|
37
|
+
# choose between GET (default), POST, PUT, DELETE http request.
|
38
|
+
# * options (Optional)
|
39
|
+
# hash of query parameters
|
40
|
+
#
|
41
|
+
def send_request(api_url, http_method = :get, options= {})
|
42
|
+
|
43
|
+
raise 'no api_url supplied' unless api_url
|
44
|
+
|
45
|
+
res = request_over_http(api_url, http_method, options)
|
46
|
+
# Strip any js wrapping methods
|
47
|
+
#puts res.body
|
48
|
+
if res.body =~ /^.+\((.+)\)$/
|
49
|
+
r = JSON.parse($1)
|
50
|
+
else
|
51
|
+
r = JSON.parse(res.body)
|
52
|
+
end
|
53
|
+
|
54
|
+
return r
|
55
|
+
end
|
56
|
+
|
57
|
+
def find(identifier, typeof)
|
58
|
+
raise "no identifier specified" if identifier.nil?
|
59
|
+
raise "no type specified" if typeof.nil?
|
60
|
+
|
61
|
+
case typeof
|
62
|
+
when :uri
|
63
|
+
api_url = "#{API_ENDPOINT}?uri=#{identifier}"
|
64
|
+
when :term
|
65
|
+
api_url = "#{API_ENDPOINT}?q=#{identifier}"
|
66
|
+
else
|
67
|
+
raise "Unknown type specified"
|
68
|
+
end
|
69
|
+
|
70
|
+
results = []
|
71
|
+
response = send_request(api_url, :get, {})
|
72
|
+
response.each do |result|
|
73
|
+
results.push Sameas::Results.new(result)
|
74
|
+
end
|
75
|
+
|
76
|
+
case typeof
|
77
|
+
when :uri
|
78
|
+
return results.first
|
79
|
+
when :term
|
80
|
+
return results
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def find_by_uri(uri)
|
85
|
+
find(uri, :uri)
|
86
|
+
end
|
87
|
+
|
88
|
+
def find_by_term(term)
|
89
|
+
find(term, :term)
|
90
|
+
end
|
91
|
+
|
92
|
+
protected
|
93
|
+
|
94
|
+
# For easier testing. You can mock this method with a XML file you re expecting to receive
|
95
|
+
def request_over_http(api_url, http_method, options)
|
96
|
+
|
97
|
+
req = nil
|
98
|
+
http_opts = { "Accept" => "application/json", "User-Agent" => "sameasdotorg_fu" }
|
99
|
+
url = URI.parse(api_url)
|
100
|
+
|
101
|
+
case http_method
|
102
|
+
when :get
|
103
|
+
u = url.query.nil? ? url.path : url.path+"?"+url.query
|
104
|
+
req = Net::HTTP::Get.new(u, http_opts)
|
105
|
+
when :post
|
106
|
+
req = Net::HTTP::Post.new(url.path, http_opts)
|
107
|
+
when :put
|
108
|
+
req = Net::HTTP::Put.new(url.path, http_opts)
|
109
|
+
when :delete
|
110
|
+
req = Net::HTTP::Delete.new(url.path, http_opts)
|
111
|
+
else
|
112
|
+
raise 'invalid http method specified'
|
113
|
+
end
|
114
|
+
|
115
|
+
req.set_form_data(options) unless options.keys.empty?
|
116
|
+
#req.basic_auth @username, @password
|
117
|
+
|
118
|
+
http = Net::HTTP.new(url.host, url.port)
|
119
|
+
http.open_timeout = @@http_open_timeout
|
120
|
+
http.read_timeout = @@http_read_timeout
|
121
|
+
http.start do |http|
|
122
|
+
res = http.request(req)
|
123
|
+
case res
|
124
|
+
when Net::HTTPSuccess
|
125
|
+
return res
|
126
|
+
else
|
127
|
+
raise Sameas::Errors.error_for(res.code, 'HTTP Error')
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
end
|
132
|
+
|
133
|
+
|
134
|
+
end
|
135
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class Sameas::Results < Sameas::Base
|
2
|
+
|
3
|
+
attr_accessor :attributes
|
4
|
+
|
5
|
+
def initialize(attributes = {})
|
6
|
+
@attributes = attributes.nested_symbolize_keys!
|
7
|
+
end
|
8
|
+
|
9
|
+
def method_missing(method, args = nil)
|
10
|
+
unless @attributes.has_key?(method.to_sym)
|
11
|
+
raise "No method named #{method.to_s}"
|
12
|
+
end
|
13
|
+
if args.nil?
|
14
|
+
@attributes[method.to_sym]
|
15
|
+
else
|
16
|
+
@attributes[method.to_sym] = args
|
17
|
+
return true
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'net/http'
|
3
|
+
require 'cgi'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
require 'pp'
|
7
|
+
|
8
|
+
class Module
|
9
|
+
def class_attr_accessor(attribute_name)
|
10
|
+
class_eval <<-CODE
|
11
|
+
def self.#{attribute_name}
|
12
|
+
@@#{attribute_name} ||= nil
|
13
|
+
end
|
14
|
+
def self.#{attribute_name}=(value)
|
15
|
+
@@#{attribute_name} = value
|
16
|
+
end
|
17
|
+
CODE
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class Hash
|
22
|
+
# File merb/core_ext/hash.rb, line 166
|
23
|
+
def nested_symbolize_keys!
|
24
|
+
each do |k,v|
|
25
|
+
sym = k.respond_to?(:to_sym) ? k.to_sym : k
|
26
|
+
self[sym] = Hash === v ? v.nested_symbolize_keys! : v
|
27
|
+
delete(k) unless k == sym
|
28
|
+
end
|
29
|
+
self
|
30
|
+
end
|
31
|
+
|
32
|
+
def nested_stringify_keys!
|
33
|
+
each do |k,v|
|
34
|
+
s = k.respond_to?(:to_s) ? k.to_s : k
|
35
|
+
self[s] = Hash === v ? v.nested_stringify_keys! : v
|
36
|
+
delete(k) unless k == s
|
37
|
+
end
|
38
|
+
self
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
# base must load first
|
44
|
+
%w(base results).each do |file|
|
45
|
+
require File.join(File.dirname(__FILE__), 'sameas', file)
|
46
|
+
end
|
47
|
+
|
48
|
+
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: monkeyhelper-sameasdotorg_fu
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- robl
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-06-04 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: robl@monkeyhelper.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- .document
|
27
|
+
- .gitignore
|
28
|
+
- LICENSE
|
29
|
+
- README.rdoc
|
30
|
+
- Rakefile
|
31
|
+
- VERSION
|
32
|
+
- examples/term.rb
|
33
|
+
- examples/uri.rb
|
34
|
+
- lib/sameas/base.rb
|
35
|
+
- lib/sameas/results.rb
|
36
|
+
- lib/sameasdotorg_fu.rb
|
37
|
+
- test/sameasdotorg_fu_test.rb
|
38
|
+
- test/test_helper.rb
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: http://github.com/monkeyhelper/sameasdotorg_fu
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options:
|
43
|
+
- --charset=UTF-8
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
requirements: []
|
59
|
+
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.2.0
|
62
|
+
signing_key:
|
63
|
+
specification_version: 2
|
64
|
+
summary: TODO
|
65
|
+
test_files:
|
66
|
+
- test/sameasdotorg_fu_test.rb
|
67
|
+
- test/test_helper.rb
|
68
|
+
- examples/uri.rb
|
69
|
+
- examples/term.rb
|