monkeyhelper-dbpedialookup 0.0.2

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 ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
6
+ nbproject
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 robl
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,56 @@
1
+ = dbpedialookup
2
+
3
+ A ruby interface to the lookup.dbpedia.org service.
4
+
5
+ == Example Usage
6
+
7
+ Searching by keyword
8
+
9
+ #!/usr/bin/ruby
10
+
11
+ require 'rubygems'
12
+ require 'dbpedialookup'
13
+
14
+ lookup = DBpediaLookup.new()
15
+ results = lookup.find_by_keyword('Otley', :maxhits => 1)
16
+
17
+ results.each do |result|
18
+ puts "#{result.Label} #{result.URI}"
19
+ end
20
+
21
+ Output
22
+
23
+ Otley http://dbpedia.org/resource/Otley
24
+
25
+ Searching by prefix
26
+
27
+ #!/usr/bin/ruby
28
+
29
+ require 'rubygems'
30
+ require 'dbpedialookup'
31
+
32
+ lookup = DBpediaLookup.new()
33
+ results = lookup.find_by_prefix('Otle')
34
+
35
+ results.each do |result|
36
+ puts "#{result.Label} #{result.URI}"
37
+ end
38
+
39
+ Output
40
+
41
+ Otley http://dbpedia.org/resource/Otley
42
+ Otley R.U.F.C. http://dbpedia.org/resource/Otley_R.U.F.C.
43
+ Otley (UK Parliament constituency) http://dbpedia.org/resource/Otley_%28UK_Parliament_constituency%29
44
+ Pudsey and Otley (UK Parliament constituency) http://dbpedia.org/resource/Pudsey_and_Otley_%28UK_Parliament_constituency%29
45
+ Otley (film) http://dbpedia.org/resource/Otley_%28film%29
46
+ Prince Henry's Grammar School, Otley http://dbpedia.org/resource/Prince_Henry%27s_Grammar_School%2C_Otley
47
+ Otley, Suffolk http://dbpedia.org/resource/Otley%2C_Suffolk
48
+ Otley Run http://dbpedia.org/resource/Otley_Run
49
+ A660 road http://dbpedia.org/resource/A660_road
50
+ H. Otley Beyer http://dbpedia.org/resource/H._Otley_Beyer
51
+
52
+ For further details see the examples dir
53
+
54
+ == Copyright
55
+
56
+ Copyright (c) 2009 robl. 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 = "dbpedialookup"
8
+ gem.summary = "A ruby interface to lookup.dbpedia.org"
9
+ gem.email = "robl[at]monkeyhelper.com"
10
+ gem.homepage = "http://github.com/monkeyhelper/dbpedialookup"
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 = "dbpedialookup #{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.2
@@ -0,0 +1,54 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{dbpedialookup}
5
+ s.version = "0.0.2"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["robl"]
9
+ s.date = %q{2009-06-07}
10
+ s.email = %q{robl[at]monkeyhelper.com}
11
+ s.extra_rdoc_files = [
12
+ "LICENSE",
13
+ "README.rdoc"
14
+ ]
15
+ s.files = [
16
+ ".document",
17
+ ".gitignore",
18
+ "LICENSE",
19
+ "README.rdoc",
20
+ "Rakefile",
21
+ "VERSION",
22
+ "dbpedialookup.gemspec",
23
+ "examples/lookup-keyword.rb",
24
+ "examples/lookup-prefix.rb",
25
+ "lib/dbpedialookup.rb",
26
+ "lib/dbpedialookup/base.rb",
27
+ "lib/dbpedialookup/errors.rb",
28
+ "lib/dbpedialookup/results.rb",
29
+ "test/dbpedialookup_test.rb",
30
+ "test/test_helper.rb"
31
+ ]
32
+ s.has_rdoc = true
33
+ s.homepage = %q{http://github.com/monkeyhelper/dbpedialookup}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.1}
37
+ s.summary = %q{A ruby interface to lookup.dbpedia.org}
38
+ s.test_files = [
39
+ "test/dbpedialookup_test.rb",
40
+ "test/test_helper.rb",
41
+ "examples/lookup-keyword.rb",
42
+ "examples/lookup-prefix.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 2
48
+
49
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
50
+ else
51
+ end
52
+ else
53
+ end
54
+ end
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require 'rubygems'
4
+ require 'dbpedialookup'
5
+
6
+ lookup = DBpediaLookup.new()
7
+ results = lookup.find_by_keyword('Otley', :maxhits => 1)
8
+
9
+ results.each do |result|
10
+ puts "#{result.URI}"
11
+ puts "#{result.Label}"
12
+ puts "#{result.Description}"
13
+ puts "#{result.Refcount}"
14
+ puts "#{result.Classes.inspect}"
15
+ end
16
+
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require 'rubygems'
4
+ require 'dbpedialookup'
5
+
6
+ lookup = DBpediaLookup.new()
7
+ results = lookup.find_by_prefix('Otle')
8
+
9
+ results.each do |result|
10
+ puts "#{result.Label} #{result.URI}"
11
+ end
@@ -0,0 +1,49 @@
1
+ require 'rubygems'
2
+ require 'net/http'
3
+ require 'cgi'
4
+ require 'xmlsimple'
5
+
6
+ require 'pp'
7
+
8
+
9
+ class Module
10
+ def class_attr_accessor(attribute_name)
11
+ class_eval <<-CODE
12
+ def self.#{attribute_name}
13
+ @@#{attribute_name} ||= nil
14
+ end
15
+ def self.#{attribute_name}=(value)
16
+ @@#{attribute_name} = value
17
+ end
18
+ CODE
19
+ end
20
+ end
21
+
22
+ class Hash
23
+ # File merb/core_ext/hash.rb, line 166
24
+ def nested_symbolize_keys!
25
+ each do |k,v|
26
+ sym = k.respond_to?(:to_sym) ? k.to_sym : k
27
+ self[sym] = Hash === v ? v.nested_symbolize_keys! : v
28
+ delete(k) unless k == sym
29
+ end
30
+ self
31
+ end
32
+
33
+ def nested_stringify_keys!
34
+ each do |k,v|
35
+ s = k.respond_to?(:to_s) ? k.to_s : k
36
+ self[s] = Hash === v ? v.nested_stringify_keys! : v
37
+ delete(k) unless k == s
38
+ end
39
+ self
40
+ end
41
+
42
+ end
43
+
44
+ # base must load first
45
+ %w(base results errors).each do |file|
46
+ require File.join(File.dirname(__FILE__), 'dbpedialookup', file)
47
+ end
48
+
49
+
@@ -0,0 +1,128 @@
1
+ module DBpediaLookup
2
+
3
+ def self.new(options = nil)
4
+ DBpediaLookup::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://lookup.dbpedia.org/api/search.asmx'
16
+
17
+ # create a new dbpedialookup 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 dbpedialookup 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
+ return XmlSimple.xml_in(res.body)
47
+
48
+ end
49
+
50
+ def find(identifier, typeof, options)
51
+ raise "no identifier specified" if identifier.nil?
52
+ raise "no type specified" if typeof.nil?
53
+
54
+ case typeof
55
+ when :keyword
56
+ api_url = "#{API_ENDPOINT}/KeywordSearch"
57
+ when :prefix
58
+ api_url = "#{API_ENDPOINT}/PrefixSearch"
59
+ else
60
+ raise "Unknown type specified"
61
+ end
62
+
63
+ opts = { :QueryString => identifier }
64
+ opts[:QueryClass] = options[:queryclass]
65
+ opts[:MaxHits] = options[:maxhits]
66
+ # QueryString=string&QueryClass=string&MaxHits=string
67
+
68
+ results = []
69
+ response = send_request(api_url, :post, opts)
70
+ response['Result'].each do |result|
71
+ results.push DBpediaLookup::Results.new(result)
72
+ end
73
+ return results
74
+ end
75
+
76
+ def find_by_keyword(keyword, options = {})
77
+ find(keyword, :keyword, options)
78
+ end
79
+
80
+ def find_by_prefix(prefix, options = {})
81
+ find(prefix, :prefix, options)
82
+ end
83
+
84
+ protected
85
+
86
+ # For easier testing. You can mock this method with a XML file you re expecting to receive
87
+ def request_over_http(api_url, http_method, options)
88
+
89
+ req = nil
90
+ http_opts = { "User-Agent" => "dbpedialookup" }
91
+ url = URI.parse(api_url)
92
+
93
+ case http_method
94
+ when :get
95
+ u = url.query.nil? ? url.path : url.path+"?"+url.query
96
+ req = Net::HTTP::Get.new(u, http_opts)
97
+ when :post
98
+ req = Net::HTTP::Post.new(url.path, http_opts)
99
+ when :put
100
+ req = Net::HTTP::Put.new(url.path, http_opts)
101
+ when :delete
102
+ req = Net::HTTP::Delete.new(url.path, http_opts)
103
+ else
104
+ raise 'invalid http method specified'
105
+ end
106
+
107
+ req.set_form_data(options) unless options.keys.empty?
108
+ #req.basic_auth @username, @password
109
+
110
+ http = Net::HTTP.new(url.host, url.port)
111
+ http.open_timeout = @@http_open_timeout
112
+ http.read_timeout = @@http_read_timeout
113
+ #http.set_debug_output $stderr
114
+ http.start do |http|
115
+ res = http.request(req)
116
+ case res
117
+ when Net::HTTPSuccess
118
+ return res
119
+ else
120
+ raise DBpediaLookup::Errors.error_for(res.code, 'HTTP Error')
121
+ end
122
+ end
123
+
124
+ end
125
+
126
+
127
+ end
128
+ end
@@ -0,0 +1,19 @@
1
+ module DBpediaLookup
2
+
3
+ class Error < RuntimeError
4
+ attr_accessor :code
5
+ end
6
+
7
+ class Errors
8
+ # Method used for raising the appropriate error class for a given error code.
9
+ # Currently raises only DBpediaLookup::Error
10
+ def self.error_for(code, message)
11
+ raise RuntimeError.new("Internal error. API error not identified or unknown error.") if (code.nil? || message.nil? || message.empty?)
12
+ raise RuntimeError.new("Internal error. Unknown error.") if code.to_i == 0 # We assume that error code 0 is never returned
13
+ e = DBpediaLookup::Error.new("#{code}: #{message}")
14
+ e.code = code
15
+ raise e
16
+ end
17
+ end
18
+
19
+ end
@@ -0,0 +1,21 @@
1
+ class DBpediaLookup::Results < DBpediaLookup::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)
11
+ raise "No method named #{method.to_s}"
12
+ end
13
+ if args.nil?
14
+ @attributes[method]
15
+ else
16
+ @attributes[method] = args
17
+ return true
18
+ end
19
+ end
20
+
21
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class DbpedialookupTest < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'dbpedialookup'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: monkeyhelper-dbpedialookup
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - robl
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-07 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: robl[at]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
+ - dbpedialookup.gemspec
33
+ - examples/lookup-keyword.rb
34
+ - examples/lookup-prefix.rb
35
+ - lib/dbpedialookup.rb
36
+ - lib/dbpedialookup/base.rb
37
+ - lib/dbpedialookup/errors.rb
38
+ - lib/dbpedialookup/results.rb
39
+ - test/dbpedialookup_test.rb
40
+ - test/test_helper.rb
41
+ has_rdoc: true
42
+ homepage: http://github.com/monkeyhelper/dbpedialookup
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --charset=UTF-8
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.2.0
64
+ signing_key:
65
+ specification_version: 2
66
+ summary: A ruby interface to lookup.dbpedia.org
67
+ test_files:
68
+ - test/dbpedialookup_test.rb
69
+ - test/test_helper.rb
70
+ - examples/lookup-keyword.rb
71
+ - examples/lookup-prefix.rb