idol-search 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
data/idol-search.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "idol-search"
8
- s.version = "0.3.0"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Shane Sherman"]
12
- s.date = "2013-01-18"
12
+ s.date = "2013-01-23"
13
13
  s.description = "ruby client for autonomy idol search"
14
14
  s.email = "shane.sherman@gmail.com"
15
15
  s.extra_rdoc_files = [
data/lib/idol-search.rb CHANGED
@@ -41,16 +41,23 @@ module Idol
41
41
  module OptionalParams
42
42
  def has_optional_params(*params)
43
43
  params.flatten.each do |opt_param|
44
+
45
+ # reader
46
+ define_method("#{opt_param}") { @parameters[opt_param] }
47
+
48
+ # write the value to params
44
49
  define_method :"#{opt_param}" do |value|
45
50
  instance_variable_set("@#{opt_param}", value)
46
51
  @parameters[opt_param.gsub("_", "")] = value
47
52
  return self
48
53
  end
54
+
49
55
  end
50
56
  end
51
-
52
57
  end
53
58
 
59
+
60
+
54
61
  module BasicIdolFunctionality
55
62
  attr_accessor :url, :filters, :parameters, :raw_results
56
63
  attr_reader :action
@@ -81,9 +88,11 @@ module Idol
81
88
  end
82
89
 
83
90
  request.http_post(*post_fields)
84
- results = @raw_results ? body : Hash.from_xml(body)
85
- if !@raw_results && results[:autnresponse][:responsedata][:abridged]
86
- # TODO: parse the abridged results
91
+ return body if @raw_results
92
+ results = if abridged
93
+ AbridgedResultsParser.new(body).parse
94
+ else
95
+ Hash.from_xml(body)
87
96
  end
88
97
  results
89
98
  end
@@ -95,7 +104,6 @@ module Idol
95
104
  post_fields << Curl::PostField.content("fieldtext", @filters.to_idol_syntax)
96
105
  end
97
106
 
98
- puts "@params: #{@parameters.inspect}"
99
107
  @parameters.each do |name, values|
100
108
  post_fields << Curl::PostField.content(name, values)
101
109
  end
@@ -103,6 +111,80 @@ module Idol
103
111
  end
104
112
  end
105
113
 
114
+ class AbridgedResultsParser
115
+ attr_accessor :result, :parsed_result
116
+ def initialize(result)
117
+ @result = result
118
+ end
119
+
120
+ def parse
121
+ fields = []
122
+ autoparsed_result = Hash.from_xml(@result)
123
+ xml = Nokogiri::XML(@result)
124
+
125
+ autn_ids = xml.xpath('//autn:abrid').first.content.split(",")
126
+ weights = xml.xpath('//autn:abrweight')[0].content.split(',').map{|w|w.to_i}
127
+
128
+ # parse out the field meta data
129
+ xml.xpath('//autn:abrfield').each do |field_data|
130
+ name = field_data.xpath('autn:abrname')[0].content
131
+ lengths = field_data.xpath('autn:abrlength')[0].content.split(',').map{|l| l.to_i }
132
+ values = field_data.xpath('autn:abrvalue')[0].content.strip
133
+ array_size_of_values = field_data.xpath('autn:abrnumber')[0].content.split(',').map{|n| n.to_i }
134
+
135
+ current_values_position = 0
136
+ parsed_values = []
137
+
138
+ # the abridged results store everything in a big long string with the lengths to
139
+ # split on in a different comma seperated string. so it's parse out the values based
140
+ # on the value length
141
+ lengths.each do |length|
142
+ parsed_values << values[current_values_position, length]
143
+ current_values_position += length
144
+ end
145
+
146
+ # if the value is an array there's some additional magic that needs to be done to get the right array value
147
+ final_values = []
148
+ index = 0
149
+ array_size_of_values.each do |array_size|
150
+ if array_size == 1
151
+ final_values << parsed_values[index]
152
+ elsif array_size > 1
153
+ final_values << parsed_values[index, array_size]
154
+ end
155
+ index += 1
156
+ end
157
+
158
+ fields << {:name => name, :values => final_values}
159
+ end
160
+
161
+ normalized_results = []
162
+ index = 0
163
+
164
+ # build a hash that mimics the hash from the unabridged normal results
165
+ autn_ids.each do |id|
166
+ result = {:id => id, :weight => weights[index], :content => {}}
167
+ fields.each do |field|
168
+ section, name = field[:name].split("/")
169
+ unless result[:content].has_key?(section)
170
+ result[:content][section] = {}
171
+ end
172
+ result[:content][section][name] = field[:values][index]
173
+ end
174
+ normalized_results << result
175
+ index += 1
176
+ end
177
+
178
+ response_data = autoparsed_result[:autnresponse][:responsedata]
179
+ response_data[:hit] = normalized_results
180
+ response_data.delete(:abrid)
181
+ response_data.delete(:abrweight)
182
+ response_data.delete(:abrfield)
183
+ @parsed_result = autoparsed_result
184
+ @parsed_result
185
+ end
186
+ end
187
+
106
188
  class Query
107
189
  extend OptionalParams
108
190
  include BasicIdolFunctionality
@@ -87,4 +87,46 @@ class TestIdolSearch < Test::Unit::TestCase
87
87
 
88
88
  end
89
89
 
90
+ ABRIDGED_RESULTS_DATA = <<END_DATA
91
+ <autnresponse xmlns:autn="http://schemas.autonomy.com/aci/">
92
+ <action>QUERY</action>
93
+ <response>SUCCESS</response>
94
+ <responsedata>
95
+ <autn:numhits>6</autn:numhits>
96
+ <autn:abridged>1</autn:abridged>
97
+ <autn:abrid>6747,6731,6702,6676,6515,6511</autn:abrid>
98
+ <autn:abrweight>9683,9683,9683,9683,9683,9683</autn:abrweight>
99
+ <autn:abrfield>
100
+ <autn:abrname>DOCUMENT/NGEN_TYPE</autn:abrname>
101
+ <autn:abrnumber>1,1,1,1,2,2</autn:abrnumber>
102
+ <autn:abrlength>8,8,8,8,7,7,7,7</autn:abrlength>
103
+ <autn:abrvalue>
104
+ BlogPostBlogPostBlogPostBlogPostArticleArticleArticleArticle
105
+ </autn:abrvalue>
106
+ </autn:abrfield>
107
+ <autn:abrfield>
108
+ <autn:abrname>DOCUMENT/ID</autn:abrname>
109
+ <autn:abrnumber>1,1,1,1,0,0</autn:abrnumber>
110
+ <autn:abrlength>24,24,24,24</autn:abrlength>
111
+ <autn:abrvalue>
112
+ 50fdf2401772be26af00010f50fdc80f1772be269c00010a50fd82631772be269c00008d50fd82601772be269c000056
113
+ </autn:abrvalue>
114
+ </autn:abrfield>
115
+ </responsedata>
116
+ </autnresponse>
117
+ END_DATA
118
+
119
+ context "Abridged results parser" do
120
+ setup do
121
+ @parser = Idol::AbridgedResultsParser.new(ABRIDGED_RESULTS_DATA)
122
+ @parsed_result = @parser.parse
123
+ end
124
+
125
+ should "parse the data correctly" do
126
+ assert_equal 6, @parsed_result[:autnresponse][:responsedata][:hit].count
127
+ end
128
+
129
+
130
+ end
131
+
90
132
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: idol-search
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-18 00:00:00.000000000 Z
12
+ date: 2013-01-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: curb
@@ -159,7 +159,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
159
159
  version: '0'
160
160
  segments:
161
161
  - 0
162
- hash: -96738212002076492
162
+ hash: -3348675117184048522
163
163
  required_rubygems_version: !ruby/object:Gem::Requirement
164
164
  none: false
165
165
  requirements: