dnz-client 0.1.2 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,26 @@
1
+ === 0.1.4 2010-05-26
2
+
3
+ * Record objects now expose their ID:
4
+
5
+ Record.find(123).id => 123
6
+
7
+ === 0.1.3 2010-05-26
8
+
9
+ * Fix for filtering bug where filters would combine incorrectly with search terms. For example:
10
+
11
+ :search_text => 'id:123 OR id:321', :filter => {:content_partner => 'National Library'}
12
+
13
+ before this fix this would result in the following search being run:
14
+
15
+ id:123 OR id:321 AND content_partner:"National Library"
16
+
17
+ clearly this does not follow the intent of the filter option, as it would only return id:321 if it is from
18
+ the National Library partner, but would return id:123 regardless.
19
+
20
+ After the change:
21
+
22
+ (id:123 OR id:321) AND content_partner:"National Library"
23
+
1
24
  === 0.1.2 2010-05-24
2
25
 
3
26
  * Fix for bug where facet values would not be returned in order.
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.1.2'
7
+ VERSION = '0.1.4'
8
8
  end
@@ -9,14 +9,21 @@ module DNZ
9
9
  self.new(Client.connection, id)
10
10
  end
11
11
 
12
+ attr_reader :id
13
+
12
14
  def initialize(client, id)
13
15
  @client = client
14
- options = {:id => id}
16
+ @id = id
15
17
 
18
+ options = {:id => @id}
16
19
  @xml = @client.fetch(:record, options)
17
20
  parse_record
18
21
  end
19
22
 
23
+ def type
24
+ @data['type']
25
+ end
26
+
20
27
  def method_missing(method, * args, & block)
21
28
  if attribute = document.root.attributes[method.to_s.upcase]
22
29
  attribute.to_s
@@ -17,7 +17,7 @@ module DNZ
17
17
  # === Example
18
18
  # search = client.search('text')
19
19
  # puts "%d results found on %d pages" % [search.result_count, search.pages]
20
- class Search
20
+ class Search
21
21
  extend DNZ::Memoizable
22
22
 
23
23
  # Constructor for Search class. Do not call this directly, instead use the <tt>Client.search</tt> method.
@@ -51,75 +51,96 @@ module DNZ
51
51
 
52
52
  def to_s
53
53
  {
54
- :results => self.result_count,
55
- :facets => self.facets.length,
56
- :page => self.page,
57
- :pages => self.pages,
58
- :per_page => self.num_results_requested
54
+ :results => self.result_count,
55
+ :facets => self.facets.length,
56
+ :page => self.page,
57
+ :pages => self.pages,
58
+ :per_page => self.num_results_requested
59
59
  }.inspect
60
60
  end
61
-
61
+
62
62
  # Return true if this search is using a custom search engine
63
63
  def custom_search?
64
64
  !@search_options.has_key?(:custom_search)
65
65
  end
66
-
67
- def method_missing(method, *args, &block)
66
+
67
+ def method_missing(method, * args, & block)
68
68
  if @results
69
- @results.send(method, *args, &block)
69
+ @results.send(method, * args, & block)
70
70
  else
71
71
  super
72
72
  end
73
73
  end
74
74
 
75
75
  private
76
-
76
+
77
+ # Combine an array of search terms into a term separated by AND
78
+ #
79
+ # If the last argument is true then the search terms will be wrapped
80
+ # in brackes. For example,
81
+ #
82
+ # combine_search_terms('term 1', 'term 2', true) => '(term 1) AND (term 2)'.
83
+ #
84
+ # This is useful for wrapping pre-combined terms:
85
+ #
86
+ # combine_search_terms('id:123 OR id:321', 'content_partner:"penfold"') => '(id:123 OR id:321) AND (content_partner:"penfold")'
87
+ #
88
+ def combine_search_terms(* args)
89
+ args.flatten!
90
+ separate = args.last == true || args.last == false ? args.pop : false
91
+ args = args.map { |term| term.to_s.strip }.reject(& :blank?).compact
92
+ args.map! { |term| '(%s)' % term } if separate
93
+ args.join(' AND ')
94
+ end
95
+
77
96
  # Turn the filter hash into an array of strings
78
97
  # in the format key:"value"
79
98
  def parsed_search_filter
80
99
  filter = @search_options[:filter]
81
100
  filter = {} unless filter.is_a?(Hash)
82
101
  filter.symbolize_keys!
83
- filter.map do |k,v|
102
+ filter.map do |k, v|
84
103
  if v.is_a?(Array)
85
104
  # OR together multiple values for the same filter
86
- '(' + v.map{|i| '%s:"%s"' % [k,i]}.join(' OR ') + ')'
105
+ '(' + v.map { |i| '%s:"%s"' % [k, i] }.join(' OR ') + ')'
87
106
  else
88
- '%s:"%s"' % [k,v]
107
+ '%s:"%s"' % [k, v]
89
108
  end
90
109
  end
91
110
  end
111
+
92
112
  memoize :parsed_search_filter
93
-
113
+
94
114
  # Join the search text with any filters with " AND "
95
115
  def parsed_search_text
96
116
  if parsed_search_filter.any?
97
- ([text] + parsed_search_filter).join(' AND ')
117
+ combine_search_terms(text, parsed_search_filter, true)
98
118
  else
99
119
  text
100
120
  end
101
121
  end
102
-
122
+
103
123
  # The facets option gets turned into a comma separated string
104
124
  def parsed_search_facets
105
125
  search_facets = @search_options[:facets] || []
106
126
  search_facets = search_facets.join(',') if search_facets.is_a?(Array)
107
127
  search_facets
108
128
  end
109
-
129
+
110
130
  # Turn the options into options acceptable for an API call.
111
131
  # Removes the filter option and parses the other options.
112
132
  def parsed_search_options
113
133
  parsed_options = @search_options.dup
114
134
  parsed_options.delete(:filter)
115
-
135
+
116
136
  parsed_options[:search_text] = parsed_search_text
117
137
  parsed_options[:facets] = parsed_search_facets
118
-
138
+
119
139
  parsed_options
120
140
  end
141
+
121
142
  memoize :parsed_search_options
122
-
143
+
123
144
  # Choose which API call to make, either search or
124
145
  # custom_search if a custom search engine is specified.
125
146
  def execute_action
@@ -131,7 +152,7 @@ module DNZ
131
152
  end
132
153
 
133
154
  # Execute the search by making the API call
134
- def execute
155
+ def execute
135
156
  @xml = @client.send(:fetch, execute_action, parsed_search_options)
136
157
  @results = DNZ::Results.new(@xml, self)
137
158
 
@@ -58,7 +58,7 @@ describe Search do
58
58
  @options = {:search_text => 'test', :filter => {:category => 'Images'}}
59
59
  @client.should_receive(:fetch).with(
60
60
  :search,
61
- :search_text => 'test AND category:"Images"',
61
+ :search_text => '(test) AND (category:"Images")',
62
62
  :facets => ""
63
63
  ).and_return(@xml)
64
64
  Search.new(@client, @options)
@@ -68,7 +68,7 @@ describe Search do
68
68
  @options = {:search_text => 'test', :filter => {:category => ['Images', 'Videos']}}
69
69
  @client.should_receive(:fetch).with(
70
70
  :search,
71
- :search_text => 'test AND (category:"Images" OR category:"Videos")',
71
+ :search_text => '(test) AND ((category:"Images" OR category:"Videos"))',
72
72
  :facets => ""
73
73
  ).and_return(@xml)
74
74
  Search.new(@client, @options)
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 2
9
- version: 0.1.2
8
+ - 4
9
+ version: 0.1.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jeremy Wells
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-24 00:00:00 +12:00
17
+ date: 2010-05-28 00:00:00 +12:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency