dnz-client 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +4 -0
- data/License.txt +22 -0
- data/Manifest.txt +21 -0
- data/PostInstall.txt +3 -0
- data/README.rdoc +46 -0
- data/Rakefile +26 -0
- data/lib/dnz/attributes.rb +9 -0
- data/lib/dnz/client.rb +131 -0
- data/lib/dnz/facet.rb +48 -0
- data/lib/dnz/result.rb +16 -0
- data/lib/dnz/search.rb +143 -0
- data/lib/dnz.rb +8 -0
- data/script/console +11 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/spec/dnz/client_spec.rb +36 -0
- data/spec/dnz/result_spec.rb +8 -0
- data/spec/dnz/search_spec.rb +55 -0
- data/spec/spec.opts +4 -0
- data/spec/spec_helper.rb +9 -0
- data/tasks/rspec.rake +21 -0
- metadata +109 -0
data/History.txt
ADDED
data/License.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2009 Boost New Media (http://www.boost.co.nz)
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
4
|
+
obtaining a copy of this software and associated documentation
|
5
|
+
files (the "Software"), to deal in the Software without
|
6
|
+
restriction, including without limitation the rights to use,
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the
|
9
|
+
Software is furnished to do so, subject to the following
|
10
|
+
conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
History.txt
|
2
|
+
Manifest.txt
|
3
|
+
PostInstall.txt
|
4
|
+
README.rdoc
|
5
|
+
License.txt
|
6
|
+
Rakefile
|
7
|
+
lib/dnz.rb
|
8
|
+
lib/dnz/attributes.rb
|
9
|
+
lib/dnz/client.rb
|
10
|
+
lib/dnz/facet.rb
|
11
|
+
lib/dnz/result.rb
|
12
|
+
lib/dnz/search.rb
|
13
|
+
script/console
|
14
|
+
script/destroy
|
15
|
+
script/generate
|
16
|
+
spec/dnz/client_spec.rb
|
17
|
+
spec/dnz/result_spec.rb
|
18
|
+
spec/dnz/search_spec.rb
|
19
|
+
spec/spec.opts
|
20
|
+
spec/spec_helper.rb
|
21
|
+
tasks/rspec.rake
|
data/PostInstall.txt
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
= DigitalNZ Client Library
|
2
|
+
|
3
|
+
* http://github.com/boost/dnz-client
|
4
|
+
|
5
|
+
== SYNOPSIS:
|
6
|
+
|
7
|
+
require 'dnz/client'
|
8
|
+
client = DNZ::Client.new('abcdefg') # API KEY
|
9
|
+
search = client.search('otago')
|
10
|
+
search.results.each do |result|
|
11
|
+
puts result.title
|
12
|
+
end
|
13
|
+
|
14
|
+
== REQUIREMENTS:
|
15
|
+
|
16
|
+
* Nokogiri
|
17
|
+
* ActiveSupport
|
18
|
+
|
19
|
+
== INSTALL:
|
20
|
+
|
21
|
+
* sudo gem install dnz-client / sudo gem install boost-dnz-client
|
22
|
+
|
23
|
+
== LICENSE:
|
24
|
+
|
25
|
+
(The MIT License)
|
26
|
+
|
27
|
+
Copyright (c) 2009 Boost New Media (http:/www.boost.co.nz)
|
28
|
+
|
29
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
30
|
+
a copy of this software and associated documentation files (the
|
31
|
+
'Software'), to deal in the Software without restriction, including
|
32
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
33
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
34
|
+
permit persons to whom the Software is furnished to do so, subject to
|
35
|
+
the following conditions:
|
36
|
+
|
37
|
+
The above copyright notice and this permission notice shall be
|
38
|
+
included in all copies or substantial portions of the Software.
|
39
|
+
|
40
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
41
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
42
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
43
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
44
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
45
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
46
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
gem 'hoe', '>= 2.1.0'
|
3
|
+
require 'hoe'
|
4
|
+
require 'fileutils'
|
5
|
+
require './lib/dnz'
|
6
|
+
|
7
|
+
Hoe.plugin :newgem
|
8
|
+
# Hoe.plugin :website
|
9
|
+
# Hoe.plugin :cucumberfeatures
|
10
|
+
|
11
|
+
# Generate all the Rake tasks
|
12
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
13
|
+
$hoe = Hoe.spec 'dnz-client' do
|
14
|
+
self.developer 'Jeremy Wells', 'jeremy@boost.co.nz'
|
15
|
+
self.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
|
16
|
+
self.description = "Ruby library for accessing Digital New Zealand's search API (digitalnz.org)"
|
17
|
+
#self.rubyforge_name = self.name # TODO this is default value
|
18
|
+
self.extra_deps = [['activesupport','>= 2.0.2'], ['nokogiri', '>= 1.2.3']]
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'newgem/tasks'
|
22
|
+
Dir['tasks/**/*.rake'].each { |t| load t }
|
23
|
+
|
24
|
+
# TODO - want other tests/tasks run by default? Add them to the list
|
25
|
+
# remove_task :default
|
26
|
+
# task :default => [:spec, :features]
|
data/lib/dnz/client.rb
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'open-uri'
|
3
|
+
require 'set'
|
4
|
+
require 'active_support'
|
5
|
+
require 'dnz/search'
|
6
|
+
|
7
|
+
module DNZ
|
8
|
+
# This is a simple client for accessing the digitalnz.org API
|
9
|
+
# for searching New Zealand's digital content. It provides
|
10
|
+
# access to search results and facet information.
|
11
|
+
#
|
12
|
+
# Author:: Jeremy Wells, Boost New Media (http://www.boost.co.nz)
|
13
|
+
# Copyright:: Copyright (c) 2009 Boost New Media
|
14
|
+
# License:: MIT
|
15
|
+
class Client
|
16
|
+
# The dnz API key
|
17
|
+
attr_reader :api_key
|
18
|
+
# The base URL (defaults to http://api.digitalnz.org)
|
19
|
+
attr_reader :base_url
|
20
|
+
# The version of the API to use (defaults to v1)
|
21
|
+
attr_reader :version
|
22
|
+
|
23
|
+
APIS = {
|
24
|
+
:search => 'records/${version}.xml/',
|
25
|
+
:custom_search => 'custom_searches/${version}/${title}.xml'
|
26
|
+
}
|
27
|
+
|
28
|
+
ARGS = {
|
29
|
+
:search => Set.new([
|
30
|
+
:search_text,
|
31
|
+
:api_key,
|
32
|
+
:num_results,
|
33
|
+
:start,
|
34
|
+
:sort,
|
35
|
+
:direction,
|
36
|
+
:facets,
|
37
|
+
:facet_num_results,
|
38
|
+
:facet_start,
|
39
|
+
])
|
40
|
+
}
|
41
|
+
|
42
|
+
# List of available facets that can be passed to search
|
43
|
+
FACETS = [
|
44
|
+
:category, :content_partner, :creator, :language, :rights, :century, :decade, :year
|
45
|
+
]
|
46
|
+
|
47
|
+
# Constructor method for the Client class. An API key must be provided.
|
48
|
+
# The base url and version default to "http://api.digitalnz.org" and "v1".
|
49
|
+
#
|
50
|
+
# ==== Example
|
51
|
+
# client = DNZ::Client.new('abcdefghijklmnoq')
|
52
|
+
# search = client.search('some text')
|
53
|
+
# search.results.each do |result|
|
54
|
+
# puts result.title
|
55
|
+
# end
|
56
|
+
def initialize(api_key, base_url = 'http://api.digitalnz.org', version = 'v1')
|
57
|
+
@api_key = api_key
|
58
|
+
@base_url = base_url
|
59
|
+
@version = version
|
60
|
+
end
|
61
|
+
|
62
|
+
# Get a list of all categories using the 'category' facet.
|
63
|
+
#
|
64
|
+
# ==== Example
|
65
|
+
# categories = client.categories
|
66
|
+
# categories.each do |category|
|
67
|
+
# puts category.name
|
68
|
+
# end
|
69
|
+
def categories
|
70
|
+
search('*:*', :facets => 'category', :facet_num_results => 100).facets['category'].values
|
71
|
+
end
|
72
|
+
|
73
|
+
# Run a search using the digitalnz.org API.
|
74
|
+
#
|
75
|
+
# ==== Options
|
76
|
+
#
|
77
|
+
# * <tt>:num_results</tt> - The number of results to return in this call. Defaults to 20.
|
78
|
+
# * <tt>:start</tt> - The starting offset of the results.
|
79
|
+
# * <tt>:facets</tt> - The facets to return for this search.
|
80
|
+
#
|
81
|
+
# ==== Example
|
82
|
+
# search = client.search('rubgy', :num_results => 50)
|
83
|
+
# search.results.each_with_index do |result, index|
|
84
|
+
# puts "#{index+1}: #{result.title}"
|
85
|
+
# end
|
86
|
+
def search(text, options = {})
|
87
|
+
options.reverse_merge!(
|
88
|
+
:search_text => text,
|
89
|
+
:num_results => 20,
|
90
|
+
:start => 0
|
91
|
+
)
|
92
|
+
|
93
|
+
# Select the correct page
|
94
|
+
page = options.delete(:page)
|
95
|
+
options[:start] = (page-1) * options[:num_results] if page
|
96
|
+
|
97
|
+
DNZ::Search.new(self, options)
|
98
|
+
end
|
99
|
+
|
100
|
+
# Make a direct call to the digitalnz.org API.
|
101
|
+
#
|
102
|
+
# * <tt>api</tt> - The api call to make. This must be listed in the APIS constant.
|
103
|
+
# * <tt>options</tt> - A hash of options to pass to the API call. These options must be defined in the ARGS constant.
|
104
|
+
def fetch(api, options = {})
|
105
|
+
validate_options(api, options)
|
106
|
+
|
107
|
+
options = options.reverse_merge(:api_key => self.api_key)
|
108
|
+
|
109
|
+
#api_url = APIS[url]
|
110
|
+
#matches = (/\$\{(.*?)\}/)
|
111
|
+
#
|
112
|
+
#
|
113
|
+
#
|
114
|
+
|
115
|
+
|
116
|
+
qs = options.map{|k,v| '%s=%s' % [k,v] }.join('&')
|
117
|
+
url = self.base_url + '/' + APIS[api].gsub('${version}', self.version) + '?' + qs
|
118
|
+
open(url)
|
119
|
+
end
|
120
|
+
|
121
|
+
private
|
122
|
+
|
123
|
+
def validate_options(path, options = {})
|
124
|
+
options.symbolize_keys!
|
125
|
+
|
126
|
+
if ARGS.has_key?(path) && !Set.new(options.keys).subset?(ARGS[path])
|
127
|
+
raise ArgumentError.new("Valid options for #{path} are: #{ARGS[path].to_a.join(', ')}, provided: #{options.keys.join(', ')}")
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
data/lib/dnz/facet.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'dnz/attributes'
|
2
|
+
|
3
|
+
module DNZ
|
4
|
+
class Facet
|
5
|
+
attr_reader :name
|
6
|
+
attr_reader :values
|
7
|
+
attr_reader :search
|
8
|
+
|
9
|
+
def initialize(client, search, doc)
|
10
|
+
@name = doc.xpath('facet-field').text
|
11
|
+
@values = []
|
12
|
+
@search = search
|
13
|
+
|
14
|
+
doc.xpath('values').first.children.each do |value_doc|
|
15
|
+
value = DNZ::FacetValue.new(client, self, value_doc)
|
16
|
+
@values << value if value.valid?
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class FacetValue
|
22
|
+
attr_reader :name, :count
|
23
|
+
|
24
|
+
def initialize(client, facet, doc)
|
25
|
+
@client = client
|
26
|
+
@facet = facet
|
27
|
+
@search_text = facet.search.text
|
28
|
+
@name = doc.xpath('name').text
|
29
|
+
@count = doc.xpath('num-results').text.to_i
|
30
|
+
end
|
31
|
+
|
32
|
+
def valid?
|
33
|
+
!self.name.blank?
|
34
|
+
end
|
35
|
+
|
36
|
+
def search(text = @search_text, options = {})
|
37
|
+
@client.search('%s:%s %s' % [@facet.name, self.name, text], options)
|
38
|
+
end
|
39
|
+
|
40
|
+
def inspect
|
41
|
+
{:name => self.name, :count => self.count}.inspect
|
42
|
+
end
|
43
|
+
|
44
|
+
def to_s
|
45
|
+
self.name
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/dnz/result.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'dnz/attributes'
|
2
|
+
|
3
|
+
module DNZ
|
4
|
+
# A DNZ::Search result record
|
5
|
+
class Result
|
6
|
+
include DNZ::Attributes
|
7
|
+
|
8
|
+
def initialize(doc)
|
9
|
+
@attributes = {}
|
10
|
+
|
11
|
+
doc.children.each do |child|
|
12
|
+
@attributes[child.name.downcase.underscore] = child.text.to_s
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/dnz/search.rb
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
require 'nokogiri'
|
2
|
+
require 'dnz/result'
|
3
|
+
require 'dnz/facet_array'
|
4
|
+
require 'dnz/facet'
|
5
|
+
|
6
|
+
begin
|
7
|
+
gem 'mislav-will_paginate' rescue nil
|
8
|
+
require 'will_paginate/collection' rescue nil
|
9
|
+
rescue LoadError => e
|
10
|
+
end
|
11
|
+
|
12
|
+
module DNZ
|
13
|
+
# This class represents a digitalnz search API call. It should not be created directly. Instead
|
14
|
+
# use the <tt>Client.search</tt> method.
|
15
|
+
#
|
16
|
+
# === Example
|
17
|
+
# search = client.search('text')
|
18
|
+
# puts "%d results found on %d pages" % [search.result_count, search.pages]
|
19
|
+
class Search
|
20
|
+
attr_reader :result_count
|
21
|
+
|
22
|
+
# Constructor for Search class. Do not call this directly, instead use the <tt>Client.search</tt> method.
|
23
|
+
def initialize(client, search_options)
|
24
|
+
@client = client
|
25
|
+
@search_options = search_options
|
26
|
+
|
27
|
+
execute
|
28
|
+
end
|
29
|
+
|
30
|
+
# The text used for searching
|
31
|
+
def text
|
32
|
+
@search_options[:search_text]
|
33
|
+
end
|
34
|
+
|
35
|
+
# The search options passed to the digitalnz API
|
36
|
+
def options
|
37
|
+
@search_options
|
38
|
+
end
|
39
|
+
|
40
|
+
# An array of results. If the mislav-will_paginate gem is installed this will return a paginated array.
|
41
|
+
def results
|
42
|
+
if @results.nil?
|
43
|
+
parse_results
|
44
|
+
paginate_results if defined? WillPaginate::Collection
|
45
|
+
end
|
46
|
+
|
47
|
+
@results
|
48
|
+
end
|
49
|
+
|
50
|
+
# An array of facets.
|
51
|
+
#
|
52
|
+
# === Example
|
53
|
+
# search = client.search('text', :facets => 'category')
|
54
|
+
# categories = search.facets['category']
|
55
|
+
# categories.each do |category|
|
56
|
+
# puts '%d results in category %s' % [category.count, category.name]
|
57
|
+
# end
|
58
|
+
def facets
|
59
|
+
parse_facets if @facets.nil?
|
60
|
+
@facets
|
61
|
+
end
|
62
|
+
|
63
|
+
# The current page of results, based on the number of requested results and the start value
|
64
|
+
# (see <tt>Client.search</tt>).
|
65
|
+
def page
|
66
|
+
(((@start || 0) / num_results_requested) + 1) rescue 1
|
67
|
+
end
|
68
|
+
|
69
|
+
# Set the page. This will update the search :start option and call the API again. The results array
|
70
|
+
# will be replaced with the new page of results.
|
71
|
+
def page=(new_page)
|
72
|
+
@search_options['start'] = (new_page-1) * num_results_requested
|
73
|
+
execute
|
74
|
+
end
|
75
|
+
|
76
|
+
# The number of pages available for the current search.
|
77
|
+
def pages
|
78
|
+
num_results_requested < result_count ? (result_count.to_f / num_results_requested).ceil : 0
|
79
|
+
end
|
80
|
+
|
81
|
+
# The number of results requested via the :num_results option (see <tt>Client.search</tt>).
|
82
|
+
def num_results_requested
|
83
|
+
@num_results_requested || 20
|
84
|
+
end
|
85
|
+
|
86
|
+
def to_s
|
87
|
+
{
|
88
|
+
:results => self.results.length,
|
89
|
+
:facets => self.facets.length,
|
90
|
+
:page => self.page,
|
91
|
+
:pages => self.pages
|
92
|
+
}.inspect
|
93
|
+
end
|
94
|
+
|
95
|
+
private
|
96
|
+
|
97
|
+
def doc
|
98
|
+
@doc ||= Nokogiri::XML(@xml)
|
99
|
+
end
|
100
|
+
|
101
|
+
def execute
|
102
|
+
@doc = nil
|
103
|
+
@results = nil
|
104
|
+
@facets = nil
|
105
|
+
@xml = @client.send(:fetch, :search, @search_options)
|
106
|
+
|
107
|
+
parse_attributes
|
108
|
+
|
109
|
+
self
|
110
|
+
end
|
111
|
+
|
112
|
+
def paginate_results
|
113
|
+
@results = WillPaginate::Collection.create(self.page, num_results_requested, self.result_count) do |pager|
|
114
|
+
pager.replace @results
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def parse_attributes
|
119
|
+
%w(num-results-requested result-count start).each do |node|
|
120
|
+
if child = doc.root.xpath(node).first
|
121
|
+
name = child.name.downcase.underscore
|
122
|
+
value = child['type'] == 'integer' ? child.text.to_i : child.text
|
123
|
+
instance_variable_set('@%s' % name, value)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def parse_results
|
129
|
+
@results = []
|
130
|
+
doc.xpath('//results/result').each do |result_xml|
|
131
|
+
@results << DNZ::Result.new(result_xml)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
def parse_facets
|
136
|
+
@facets = FacetArray.new
|
137
|
+
|
138
|
+
doc.xpath('//facets/facet').each do |facet_xml|
|
139
|
+
@facets << DNZ::Facet.new(@client, self, facet_xml)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
data/lib/dnz.rb
ADDED
data/script/console
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# File: script/console
|
3
|
+
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
|
4
|
+
|
5
|
+
libs = " -r irb/completion"
|
6
|
+
# Perhaps use a console_lib to store any extra methods I may want available in the cosole
|
7
|
+
# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
|
8
|
+
libs << " -r #{File.dirname(__FILE__) + '/../lib/dnz.rb'}"
|
9
|
+
|
10
|
+
puts "Loading dnz gem"
|
11
|
+
exec "#{irb} #{libs} --simple-prompt"
|
data/script/destroy
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/destroy'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Destroy.new.run(ARGV)
|
data/script/generate
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/generate'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Generate.new.run(ARGV)
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
require 'dnz/client'
|
3
|
+
|
4
|
+
include DNZ
|
5
|
+
|
6
|
+
describe Client do
|
7
|
+
before(:each) do
|
8
|
+
@client = Client.new('abc')
|
9
|
+
@client.stub!(:open) # make sure open is never called
|
10
|
+
@search = mock(:search)
|
11
|
+
DNZ::Search.stub!(:new).and_return(@search)
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '#search' do
|
15
|
+
it 'should create a new search object and return it' do
|
16
|
+
@client.search('*:*').should be(@search)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#fetch' do
|
21
|
+
it 'should raise an error an invalid option is set' do
|
22
|
+
lambda do
|
23
|
+
@client.fetch(:search, :blahblah => 'dlfkgj')
|
24
|
+
end.should raise_error(ArgumentError)
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should call open with query string arguments' do
|
28
|
+
@client.should_receive(:open).with do |url|
|
29
|
+
url.should include('http://api.digitalnz.org/records/v1.xml/?')
|
30
|
+
url.should include('api_key=abc')
|
31
|
+
url.should include('search_text=*:*')
|
32
|
+
end
|
33
|
+
@client.fetch(:search, :search_text => '*:*')
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
require 'dnz/search'
|
3
|
+
|
4
|
+
include DNZ
|
5
|
+
|
6
|
+
describe Search do
|
7
|
+
before(:each) do
|
8
|
+
@xml = %Q{<?xml version="1.0"?>
|
9
|
+
<response>
|
10
|
+
<results>
|
11
|
+
<result>
|
12
|
+
<attribute>test</attribute>
|
13
|
+
</result>
|
14
|
+
</results>
|
15
|
+
<facets>
|
16
|
+
<facet>
|
17
|
+
<value>test</value>
|
18
|
+
</facet>
|
19
|
+
</facets>
|
20
|
+
</response>
|
21
|
+
}
|
22
|
+
|
23
|
+
@doc = Nokogiri::XML(@xml)
|
24
|
+
@client = mock(:client)
|
25
|
+
@result = mock(:result)
|
26
|
+
@facet = mock(:facet)
|
27
|
+
@options = {:search_text => 'test'}
|
28
|
+
|
29
|
+
Result.stub!(:new).and_return(@result)
|
30
|
+
Facet.stub!(:new).and_return(@facet)
|
31
|
+
|
32
|
+
@client.stub!(:fetch).and_return(@xml)
|
33
|
+
end
|
34
|
+
|
35
|
+
describe 'Search.new' do
|
36
|
+
it 'should call @client.fetch' do
|
37
|
+
@client.should_receive(:fetch).with(:search, :search_text => 'test').and_return(@xml)
|
38
|
+
Search.new(@client, @options)
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should create one result' do
|
42
|
+
Result.should_receive(:new).and_return(@result)
|
43
|
+
Search.new(@client, @options).results.should == [@result]
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should create one facet' do
|
47
|
+
Facet.should_receive(:new).and_return(@facet)
|
48
|
+
Search.new(@client, @options).facets.should == [@facet]
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'should return facets as a FacetArray' do
|
52
|
+
Search.new(@client, @options).facets.should be_a(FacetArray)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
data/tasks/rspec.rake
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
begin
|
2
|
+
require 'spec'
|
3
|
+
rescue LoadError
|
4
|
+
require 'rubygems'
|
5
|
+
require 'spec'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'spec/rake/spectask'
|
9
|
+
rescue LoadError
|
10
|
+
puts <<-EOS
|
11
|
+
To use rspec for testing you must install rspec gem:
|
12
|
+
gem install rspec
|
13
|
+
EOS
|
14
|
+
exit(0)
|
15
|
+
end
|
16
|
+
|
17
|
+
desc "Run the specs under spec"
|
18
|
+
Spec::Rake::SpecTask.new do |t|
|
19
|
+
t.spec_opts = ['--options', "spec/spec.opts"]
|
20
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dnz-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jeremy Wells
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-12 00:00:00 +13:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activesupport
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.0.2
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: nokogiri
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.2.3
|
34
|
+
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: hoe
|
37
|
+
type: :development
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 2.3.3
|
44
|
+
version:
|
45
|
+
description: Ruby library for accessing Digital New Zealand's search API (digitalnz.org)
|
46
|
+
email:
|
47
|
+
- jeremy@boost.co.nz
|
48
|
+
executables: []
|
49
|
+
|
50
|
+
extensions: []
|
51
|
+
|
52
|
+
extra_rdoc_files:
|
53
|
+
- History.txt
|
54
|
+
- Manifest.txt
|
55
|
+
- PostInstall.txt
|
56
|
+
- License.txt
|
57
|
+
files:
|
58
|
+
- History.txt
|
59
|
+
- Manifest.txt
|
60
|
+
- PostInstall.txt
|
61
|
+
- README.rdoc
|
62
|
+
- License.txt
|
63
|
+
- Rakefile
|
64
|
+
- lib/dnz.rb
|
65
|
+
- lib/dnz/attributes.rb
|
66
|
+
- lib/dnz/client.rb
|
67
|
+
- lib/dnz/facet.rb
|
68
|
+
- lib/dnz/result.rb
|
69
|
+
- lib/dnz/search.rb
|
70
|
+
- script/console
|
71
|
+
- script/destroy
|
72
|
+
- script/generate
|
73
|
+
- spec/dnz/client_spec.rb
|
74
|
+
- spec/dnz/result_spec.rb
|
75
|
+
- spec/dnz/search_spec.rb
|
76
|
+
- spec/spec.opts
|
77
|
+
- spec/spec_helper.rb
|
78
|
+
- tasks/rspec.rake
|
79
|
+
has_rdoc: true
|
80
|
+
homepage: http://github.com/boost/dnz-client
|
81
|
+
licenses: []
|
82
|
+
|
83
|
+
post_install_message: PostInstall.txt
|
84
|
+
rdoc_options:
|
85
|
+
- --main
|
86
|
+
- README.rdoc
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: "0"
|
94
|
+
version:
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: "0"
|
100
|
+
version:
|
101
|
+
requirements: []
|
102
|
+
|
103
|
+
rubyforge_project: dnz-client
|
104
|
+
rubygems_version: 1.3.3
|
105
|
+
signing_key:
|
106
|
+
specification_version: 3
|
107
|
+
summary: ""
|
108
|
+
test_files: []
|
109
|
+
|