dnz-client 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ === 0.0.9 2010-04-23
2
+
3
+ * Support for previewing custom searches
4
+
1
5
  === 0.0.8 2009-11-19
2
6
 
3
7
  * Support for custom_search API with categories call.
data/Manifest.txt CHANGED
@@ -10,6 +10,8 @@ lib/dnz/client.rb
10
10
  lib/dnz/facet.rb
11
11
  lib/dnz/facet_array.rb
12
12
  lib/dnz/result.rb
13
+ lib/dnz/results.rb
14
+ lib/dnz/custom_search.rb
13
15
  lib/dnz/search.rb
14
16
  lib/dnz/memoizable.rb
15
17
  lib/dnz/error/invalid_api_key.rb
data/lib/dnz/client.rb CHANGED
@@ -2,6 +2,7 @@ require 'rubygems'
2
2
  require 'open-uri'
3
3
  require 'set'
4
4
  require 'active_support'
5
+ require 'dnz/custom_search'
5
6
  require 'dnz/search'
6
7
  require 'dnz/error/invalid_api_key'
7
8
 
@@ -24,7 +25,8 @@ module DNZ
24
25
  # API URLS
25
26
  APIS = {
26
27
  :search => 'records/${version}.xml/',
27
- :custom_search => 'custom_searches/${version}/${custom_search}.xml'
28
+ :custom_search => 'custom_searches/${version}/${custom_search}.xml',
29
+ :custom_search_preview => 'custom_searches/${version}/test.xml'
28
30
  }
29
31
 
30
32
  # API Arguments
@@ -74,6 +76,9 @@ module DNZ
74
76
  :facets,
75
77
  :facet_num_results,
76
78
  :facet_start
79
+ ]),
80
+ :custom_search_preview => Set.new([
81
+ :api_key
77
82
  ])
78
83
  }
79
84
  }
@@ -149,14 +154,18 @@ module DNZ
149
154
  options[:start] = (page-1) * options[:num_results] if page
150
155
 
151
156
  DNZ::Search.new(self, options)
152
- end
157
+ end
153
158
 
154
159
  # Make a direct call to the digitalnz.org API.
155
160
  #
156
161
  # * <tt>api</tt> - The api call to make. This must be listed in the APIS constant.
157
162
  # * <tt>options</tt> - A hash of options to pass to the API call. These options must be defined in the ARGS constant.
158
163
  def fetch(api, options = {})
159
- validate_options(api, options)
164
+ options = options.dup
165
+
166
+ unless options.delete(:validate) === false
167
+ validate_options(api, options)
168
+ end
160
169
 
161
170
  options = options.reverse_merge(
162
171
  :api_key => self.api_key,
@@ -0,0 +1,55 @@
1
+ require 'nokogiri'
2
+ require 'dnz/result'
3
+ require 'dnz/facet_array'
4
+ require 'dnz/facet'
5
+ require 'dnz/memoizable'
6
+
7
+ # Load will_paginate if it's available
8
+ # to provide pagination
9
+ begin
10
+ gem 'mislav-will_paginate' rescue nil
11
+ require 'will_paginate/collection' rescue nil
12
+ rescue LoadError => e
13
+ end
14
+
15
+ module DNZ
16
+ class CustomSearch
17
+ attr_reader :attributes
18
+
19
+ def initialize(client, attributes = {})
20
+ @client = client
21
+ @attributes = attributes
22
+ end
23
+
24
+ def [](key)
25
+ @attributes[key]
26
+ end
27
+
28
+ def []=(key, value)
29
+ @attributes[key] = value
30
+ end
31
+
32
+ def method_missing(method, *args, &block)
33
+ if @attributes.has_key?(method.to_sym)
34
+ @attributes[method.to_sym]
35
+ else
36
+ super
37
+ end
38
+ end
39
+
40
+ def preview
41
+ options = {:validate => false, :search_text => '*:*'}
42
+ attributes.each do |key, value|
43
+ options['custom_search[%s]' % key] = value
44
+ end
45
+
46
+ xml = @client.fetch(:custom_search_preview, options)
47
+ DNZ::Results.new(xml, self)
48
+ end
49
+
50
+ def text
51
+ attributes[:search_term] || ''
52
+ end
53
+ end
54
+ end
55
+
@@ -0,0 +1,112 @@
1
+ require 'nokogiri'
2
+ require 'dnz/result'
3
+ require 'dnz/facet_array'
4
+ require 'dnz/facet'
5
+ require 'dnz/memoizable'
6
+
7
+ # Load will_paginate if it's available
8
+ # to provide pagination
9
+ begin
10
+ gem 'mislav-will_paginate' rescue nil
11
+ require 'will_paginate/collection' rescue nil
12
+ rescue LoadError => e
13
+ end
14
+
15
+ module DNZ
16
+ class Results
17
+ extend DNZ::Memoizable
18
+
19
+ attr_reader :result_count
20
+
21
+ # An array of facets.
22
+ #
23
+ # === Example
24
+ # search = client.search('text', :facets => 'category')
25
+ # categories = search.facets['category']
26
+ # categories.each do |category|
27
+ # puts '%d results in category %s' % [category.count, category.name]
28
+ # end
29
+ attr_reader :facets
30
+
31
+ # An array of results. If the mislav-will_paginate gem is installed this will return a paginated array.
32
+ attr_reader :results
33
+
34
+ def initialize(xml, search)
35
+ reset
36
+
37
+ @xml = xml
38
+ @search = search
39
+
40
+ # Parse the results
41
+ parse_attributes
42
+ parse_facets
43
+ parse_results
44
+ paginate_results if defined? WillPaginate::Collection
45
+ end
46
+
47
+ # The current page of results, based on the number of requested results and the start value
48
+ # (see <tt>Client.search</tt>).
49
+ def page
50
+ (((@start || 0) / num_results_requested) + 1) rescue 1
51
+ end
52
+
53
+ # The number of pages available for the current search.
54
+ def pages
55
+ num_results_requested < result_count ? (result_count.to_f / num_results_requested).ceil : 1
56
+ end
57
+
58
+ # The number of results requested via the :num_results option (see <tt>Client.search</tt>).
59
+ def num_results_requested
60
+ @num_results_requested || 20
61
+ end
62
+
63
+ private
64
+
65
+ # Return a Nokogiri document for the XML
66
+ def doc
67
+ @doc ||= Nokogiri::XML(@xml)
68
+ end
69
+
70
+ # Reset important instance variables
71
+ def reset
72
+ @doc = nil
73
+ @results = nil
74
+ @facets = nil
75
+ end
76
+
77
+ # Replace the results array with a paginated array
78
+ def paginate_results
79
+ @results = WillPaginate::Collection.create(self.page, num_results_requested, self.result_count) do |pager|
80
+ pager.replace @results
81
+ end
82
+ end
83
+
84
+ # Parse important global attributes into instance variables
85
+ def parse_attributes
86
+ %w(num-results-requested result-count start).each do |node|
87
+ if child = doc.root.xpath(node).first
88
+ name = child.name.downcase.underscore
89
+ value = child['type'] == 'integer' ? child.text.to_i : child.text
90
+ instance_variable_set('@%s' % name, value)
91
+ end
92
+ end
93
+ end
94
+
95
+ # Parse the results into an array of DNZ::Result
96
+ def parse_results
97
+ @results = []
98
+ doc.xpath('//results/result').each do |result_xml|
99
+ @results << DNZ::Result.new(result_xml)
100
+ end
101
+ end
102
+
103
+ # Parse the facets into an array of DNZ::FacetArray
104
+ def parse_facets
105
+ @facets = FacetArray.new
106
+
107
+ doc.xpath('//facets/facet').each do |facet_xml|
108
+ @facets << DNZ::Facet.new(@client, @search, facet_xml)
109
+ end
110
+ end
111
+ end
112
+ end
data/lib/dnz/search.rb CHANGED
@@ -1,7 +1,5 @@
1
1
  require 'nokogiri'
2
- require 'dnz/result'
3
- require 'dnz/facet_array'
4
- require 'dnz/facet'
2
+ require 'dnz/results'
5
3
  require 'dnz/memoizable'
6
4
 
7
5
  # Load will_paginate if it's available
@@ -19,10 +17,7 @@ module DNZ
19
17
  # === Example
20
18
  # search = client.search('text')
21
19
  # puts "%d results found on %d pages" % [search.result_count, search.pages]
22
- class Search
23
- # The total number of results returned by the search
24
- attr_reader :result_count
25
-
20
+ class Search
26
21
  extend DNZ::Memoizable
27
22
 
28
23
  # Constructor for Search class. Do not call this directly, instead use the <tt>Client.search</tt> method.
@@ -43,29 +38,6 @@ module DNZ
43
38
  @search_options
44
39
  end
45
40
 
46
- # An array of results. If the mislav-will_paginate gem is installed this will return a paginated array.
47
- def results
48
- @results
49
- end
50
-
51
- # An array of facets.
52
- #
53
- # === Example
54
- # search = client.search('text', :facets => 'category')
55
- # categories = search.facets['category']
56
- # categories.each do |category|
57
- # puts '%d results in category %s' % [category.count, category.name]
58
- # end
59
- def facets
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
41
  # Set the page. This will update the search :start option and call the API again. The results array
70
42
  # will be replaced with the new page of results.
71
43
  def page=(new_page)
@@ -73,16 +45,6 @@ module DNZ
73
45
  execute
74
46
  end
75
47
 
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 : 1
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
48
  def inspect
87
49
  self.to_s
88
50
  end
@@ -101,6 +63,14 @@ module DNZ
101
63
  def custom_search?
102
64
  !@search_options.has_key?(:custom_search)
103
65
  end
66
+
67
+ def method_missing(method, *args, &block)
68
+ if @results
69
+ @results.send(method, *args, &block)
70
+ else
71
+ super
72
+ end
73
+ end
104
74
 
105
75
  private
106
76
 
@@ -149,11 +119,6 @@ module DNZ
149
119
  parsed_options
150
120
  end
151
121
  memoize :parsed_search_options
152
-
153
- # Return a Nokogiri document for the XML
154
- def doc
155
- @doc ||= Nokogiri::XML(@xml)
156
- end
157
122
 
158
123
  # Choose which API call to make, either search or
159
124
  # custom_search if a custom search engine is specified.
@@ -166,60 +131,11 @@ module DNZ
166
131
  end
167
132
 
168
133
  # Execute the search by making the API call
169
- def execute
170
- reset
171
-
134
+ def execute
172
135
  @xml = @client.send(:fetch, execute_action, parsed_search_options)
173
-
174
- # Parse the results
175
- parse_attributes
176
- parse_facets
177
- parse_results
178
- paginate_results if defined? WillPaginate::Collection
136
+ @results = DNZ::Results.new(@xml, self)
179
137
 
180
138
  self
181
139
  end
182
-
183
- # Reset important instance variables
184
- def reset
185
- @doc = nil
186
- @results = nil
187
- @facets = nil
188
- end
189
-
190
- # Replace the results array with a paginated array
191
- def paginate_results
192
- @results = WillPaginate::Collection.create(self.page, num_results_requested, self.result_count) do |pager|
193
- pager.replace @results
194
- end
195
- end
196
-
197
- # Parse important global attributes into instance variables
198
- def parse_attributes
199
- %w(num-results-requested result-count start).each do |node|
200
- if child = doc.root.xpath(node).first
201
- name = child.name.downcase.underscore
202
- value = child['type'] == 'integer' ? child.text.to_i : child.text
203
- instance_variable_set('@%s' % name, value)
204
- end
205
- end
206
- end
207
-
208
- # Parse the results into an array of DNZ::Result
209
- def parse_results
210
- @results = []
211
- doc.xpath('//results/result').each do |result_xml|
212
- @results << DNZ::Result.new(result_xml)
213
- end
214
- end
215
-
216
- # Parse the facets into an array of DNZ::FacetArray
217
- def parse_facets
218
- @facets = FacetArray.new
219
-
220
- doc.xpath('//facets/facet').each do |facet_xml|
221
- @facets << DNZ::Facet.new(@client, self, facet_xml)
222
- end
223
- end
224
140
  end
225
141
  end
data/lib/dnz.rb CHANGED
@@ -4,5 +4,5 @@ $:.unshift(File.dirname(__FILE__)) unless
4
4
  require 'dnz/client'
5
5
 
6
6
  module Dnz
7
- VERSION = '0.0.8'
7
+ VERSION = '0.0.9'
8
8
  end
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dnz-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 9
9
+ version: 0.0.9
5
10
  platform: ruby
6
11
  authors:
7
12
  - Jeremy Wells
@@ -9,39 +14,65 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2009-11-19 00:00:00 +13:00
17
+ date: 2010-04-21 00:00:00 +12:00
13
18
  default_executable:
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: activesupport
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
20
24
  requirements:
21
25
  - - ">="
22
26
  - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 0
30
+ - 2
23
31
  version: 2.0.2
24
- version:
32
+ type: :runtime
33
+ version_requirements: *id001
25
34
  - !ruby/object:Gem::Dependency
26
35
  name: nokogiri
27
- type: :runtime
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
30
38
  requirements:
31
39
  - - ">="
32
40
  - !ruby/object:Gem::Version
41
+ segments:
42
+ - 1
43
+ - 2
44
+ - 3
33
45
  version: 1.2.3
34
- version:
46
+ type: :runtime
47
+ version_requirements: *id002
35
48
  - !ruby/object:Gem::Dependency
36
- name: hoe
49
+ name: rubyforge
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 2
57
+ - 0
58
+ - 4
59
+ version: 2.0.4
37
60
  type: :development
38
- version_requirement:
39
- version_requirements: !ruby/object:Gem::Requirement
61
+ version_requirements: *id003
62
+ - !ruby/object:Gem::Dependency
63
+ name: hoe
64
+ prerelease: false
65
+ requirement: &id004 !ruby/object:Gem::Requirement
40
66
  requirements:
41
67
  - - ">="
42
68
  - !ruby/object:Gem::Version
43
- version: 2.3.3
44
- version:
69
+ segments:
70
+ - 2
71
+ - 6
72
+ - 0
73
+ version: 2.6.0
74
+ type: :development
75
+ version_requirements: *id004
45
76
  description: Ruby library for accessing Digital New Zealand's search API (digitalnz.org)
46
77
  email:
47
78
  - jeremy@boost.co.nz
@@ -68,6 +99,8 @@ files:
68
99
  - lib/dnz/facet.rb
69
100
  - lib/dnz/facet_array.rb
70
101
  - lib/dnz/result.rb
102
+ - lib/dnz/results.rb
103
+ - lib/dnz/custom_search.rb
71
104
  - lib/dnz/search.rb
72
105
  - lib/dnz/memoizable.rb
73
106
  - lib/dnz/error/invalid_api_key.rb
@@ -94,18 +127,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
94
127
  requirements:
95
128
  - - ">="
96
129
  - !ruby/object:Gem::Version
130
+ segments:
131
+ - 0
97
132
  version: "0"
98
- version:
99
133
  required_rubygems_version: !ruby/object:Gem::Requirement
100
134
  requirements:
101
135
  - - ">="
102
136
  - !ruby/object:Gem::Version
137
+ segments:
138
+ - 0
103
139
  version: "0"
104
- version:
105
140
  requirements: []
106
141
 
107
142
  rubyforge_project: dnz-client
108
- rubygems_version: 1.3.3
143
+ rubygems_version: 1.3.6
109
144
  signing_key:
110
145
  specification_version: 3
111
146
  summary: ""