solr4r 0.0.2 → 0.0.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 586864696853144c04da2e5cea908d23f165ac93
4
- data.tar.gz: a8b43ec8857d0a4d78ad29d659599e3460cac848
3
+ metadata.gz: 4fc0f7535c323bbbd5658b37a8347ffb480352d8
4
+ data.tar.gz: 8adf499638d2dce1b6f145714f19897e35234679
5
5
  SHA512:
6
- metadata.gz: b5cf30799e4a101d6eaddbdc7e80b9df1fc77c05f9c19a8c83e99d8880c229d76b0ac26f60ea7cc0229cdb970014b4a37d00dbd65507a9615dc179adb9172b4d
7
- data.tar.gz: 5c821ebf42f7be62f81229adbb328f43e179a198aba53c91fa818f023e85532f8e970c8444608ad18a677ef17b156fc437c38007a16e3fac5ecf03752252af59
6
+ metadata.gz: a6c0549a56c70c7b9e7a644a2d1c563bf42dc5ad172db183f01b4966a411b379db21ca751a7295fbfa12f1ccfa84bc3564a9f5a7477ba631599e25643f2c9569
7
+ data.tar.gz: 038f091785cb04024a139c4de2f7da174b359496c82dc3f2881e95000e8ba8c3dbf38123023291bd76c845d2b88a2a13808a4bc30590450fbf2b656a395a4b81
data/ChangeLog CHANGED
@@ -2,6 +2,15 @@
2
2
 
3
3
  = Revision history for solr4r
4
4
 
5
+ == 0.0.3 [2014-09-19]
6
+
7
+ * Added Solr4R::Client#json.
8
+ * Added Solr4R::Client#json_query.
9
+ * Added Solr4R::Client#solr_version.
10
+ * Updated Solr4R::Request with some fixes.
11
+ * Refactored Solr4R::Response to use wrapper object.
12
+ * Refactored Solr4R::Builder to use custom document class.
13
+
5
14
  == 0.0.2 [2014-04-17]
6
15
 
7
16
  * Refactored Solr4R::Request away from curb.
data/README CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  == VERSION
4
4
 
5
- This documentation refers to solr4r version 0.0.2
5
+ This documentation refers to solr4r version 0.0.3
6
6
 
7
7
 
8
8
  == DESCRIPTION
@@ -12,7 +12,7 @@ TODO
12
12
 
13
13
  == LINKS
14
14
 
15
- Documentation:: https://blackwinter.github.io/solr4r/
15
+ Documentation:: https://blackwinter.github.com/solr4r
16
16
  Source code:: https://github.com/blackwinter/solr4r
17
17
  RubyGem:: https://rubygems.org/gems/solr4r
18
18
  Travis CI:: https://travis-ci.org/blackwinter/solr4r
data/Rakefile CHANGED
@@ -13,7 +13,7 @@ begin
13
13
  email: %q{jens.wille@gmail.com},
14
14
  license: %q{AGPL-3.0},
15
15
  homepage: :blackwinter,
16
- dependencies: { nokogiri: '~> 1.6' },
16
+ dependencies: { nokogiri: '~> 1.6', 'nuggets' => '~> 1.0' },
17
17
 
18
18
  required_ruby_version: '>= 1.9.3'
19
19
  }
@@ -27,10 +27,16 @@ require 'nokogiri'
27
27
 
28
28
  module Solr4R
29
29
 
30
- Document = Nokogiri::XML::Document
31
-
32
30
  class Builder < Nokogiri::XML::Builder
33
31
 
32
+ class Document < Nokogiri::XML::Document
33
+
34
+ def document
35
+ self
36
+ end
37
+
38
+ end
39
+
34
40
  DEFAULT_OPTIONS = {
35
41
  encoding: 'UTF-8'
36
42
  }
@@ -41,9 +47,10 @@ module Solr4R
41
47
  'block argument not supported, use options hash instead'
42
48
  end
43
49
 
44
- super(@options = DEFAULT_OPTIONS.merge(options))
45
-
46
- @_solr_doc = @doc
50
+ super(
51
+ @_solr_opt = DEFAULT_OPTIONS.merge(options),
52
+ @_solr_doc = Document.new
53
+ )
47
54
  end
48
55
 
49
56
  # See Schema[http://wiki.apache.org/solr/UpdateXmlMessages#add.2Freplace_documents].
@@ -122,21 +129,14 @@ module Solr4R
122
129
  # </doc>
123
130
  # </add>
124
131
  def add(doc, attributes = {})
125
- to_xml(:add, attributes) { |add_node|
126
- doc = [doc] unless doc.is_a?(Array)
127
- doc.each { |hash, doc_attributes|
128
- doc_(doc_attributes) { |doc_node|
129
- hash.each { |key, values|
130
- values = values.is_a?(Array) ? values.dup : [values]
132
+ to_xml(:add, attributes) { |add_node| _each(doc) { |hash, doc_attributes|
133
+ add_node.doc_(doc_attributes) { |doc_node| hash.each { |key, values|
134
+ field_attributes = (values.is_a?(Array) && values.last.is_a?(Hash) ?
135
+ (values = values.dup).pop : {}).merge(name: key)
131
136
 
132
- field_attributes = values.last.is_a?(Hash) ? values.pop : {}
133
- field_attributes = field_attributes.merge(name: key)
134
-
135
- values.each { |value| doc_node.field_(value, field_attributes) }
136
- }
137
- }
138
- }
139
- }
137
+ _each(values) { |value| doc_node.field_(value, field_attributes) }
138
+ } }
139
+ } }
140
140
  end
141
141
 
142
142
  # See Schema[http://wiki.apache.org/solr/UpdateXmlMessages#A.22commit.22_and_.22optimize.22].
@@ -252,40 +252,30 @@ module Solr4R
252
252
  # <query>office:Osaka</query>
253
253
  # </delete>
254
254
  def delete(hash)
255
- to_xml(:delete) { |delete_node|
256
- hash.each { |key, values|
257
- values = [values] unless values.is_a?(Array)
258
- values.each { |value|
259
- ary = []
260
-
261
- if value.is_a?(Hash)
262
- value.each { |k, v| Array(v).each { |w| ary << "#{k}:#{w}" } }
263
- else
264
- ary << value
265
- end
255
+ to_xml(:delete) { |delete_node| hash.each { |key, values|
256
+ delete = lambda { |value| delete_node.method_missing(key, value) }
266
257
 
267
- ary.each { |v| delete_node.method_missing(key, v) }
268
- }
269
- }
270
- }
258
+ _each(values) { |value| !value.is_a?(Hash) ? delete[value] :
259
+ value.each { |k, v| Array(v).each { |w| delete["#{k}:#{w}"] } } }
260
+ } }
271
261
  end
272
262
 
273
263
  def inspect
274
- '#<%s:0x%x @options=%p>' % [
275
- self.class, object_id, @options
276
- ]
264
+ '#<%s:0x%x %p>' % [self.class, object_id, @_solr_opt]
277
265
  end
278
266
 
279
267
  private
280
268
 
281
269
  def to_xml(name, attributes = {}, &block)
282
270
  self.parent = self.doc = @_solr_doc.dup
283
-
284
271
  method_missing(name, attributes, &block)
285
-
286
272
  super(&nil)
287
273
  end
288
274
 
275
+ def _each(values, &block)
276
+ (values.is_a?(Array) ? values : [values]).each(&block)
277
+ end
278
+
289
279
  end
290
280
 
291
281
  end
@@ -43,6 +43,8 @@ module Solr4R
43
43
 
44
44
  DEFAULT_UPDATE_PATH = 'update'
45
45
 
46
+ SYSTEM_INFO_PATH = 'admin/info/system'
47
+
46
48
  MATCH_ALL_QUERY = '*:*'
47
49
 
48
50
  def initialize(options = {})
@@ -84,6 +86,10 @@ module Solr4R
84
86
  self
85
87
  end
86
88
 
89
+ def json(path, params = {}, options = {}, &block)
90
+ get(path, params.merge(wt: :json), options, &block).result
91
+ end
92
+
87
93
  def get(path, params = {}, options = {}, &block)
88
94
  send_request(path, options.merge(method: :get, params: params), &block)
89
95
  end
@@ -147,6 +153,14 @@ module Solr4R
147
153
  get(path, params, options, &block)
148
154
  end
149
155
 
156
+ def json_query(params = {}, options = {}, path = DEFAULT_SELECT_PATH, &block)
157
+ json(path, params, options, &block)
158
+ end
159
+
160
+ def solr_version(type = :spec)
161
+ json(SYSTEM_INFO_PATH) % "lucene/solr-#{type}-version"
162
+ end
163
+
150
164
  def inspect
151
165
  '#<%s:0x%x @default_params=%p %s>' % [
152
166
  self.class, object_id, default_params, request.request_line
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+
3
+ #--
4
+ ###############################################################################
5
+ # #
6
+ # solr4r -- A Ruby client for Apache Solr #
7
+ # #
8
+ # Copyright (C) 2014 Jens Wille #
9
+ # #
10
+ # Mir is free software: you can redistribute it and/or modify it under the #
11
+ # terms of the GNU Affero General Public License as published by the Free #
12
+ # Software Foundation, either version 3 of the License, or (at your option) #
13
+ # any later version. #
14
+ # #
15
+ # solr4r is distributed in the hope that it will be useful, but WITHOUT ANY #
16
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
17
+ # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
18
+ # more details. #
19
+ # #
20
+ # You should have received a copy of the GNU Affero General Public License #
21
+ # along with solr4r. If not, see <http://www.gnu.org/licenses/>. #
22
+ # #
23
+ ###############################################################################
24
+ #++
25
+
26
+ require 'forwardable'
27
+
28
+ module Solr4R
29
+
30
+ class Document
31
+
32
+ extend Forwardable
33
+ def_delegators :to_hash, :[], :each
34
+
35
+ def initialize(result, hash)
36
+ @result, @hash = result, hash
37
+ end
38
+
39
+ attr_reader :result
40
+
41
+ def to_hash
42
+ @hash
43
+ end
44
+
45
+ end
46
+
47
+ end
@@ -115,7 +115,7 @@ module Solr4R
115
115
  req = request_for(method, uri)
116
116
  when :post
117
117
  req = request_for(method, uri)
118
- req.set_form_data(data)
118
+ req.set_form_data(Array(data))
119
119
  else
120
120
  raise ArgumentError, "method not supported: #{method}"
121
121
  end
@@ -24,6 +24,7 @@
24
24
  #++
25
25
 
26
26
  require 'json'
27
+ require_relative 'result'
27
28
 
28
29
  module Solr4R
29
30
 
@@ -45,8 +46,8 @@ module Solr4R
45
46
  '"%s %s" %d' % [request_method.upcase, request_url, response_code]
46
47
  end
47
48
 
48
- def result(options = {})
49
- @result ||= evaluate_result(options) if @evaluate
49
+ def result
50
+ @result ||= evaluate_result if @evaluate
50
51
  end
51
52
 
52
53
  def num_found
@@ -67,30 +68,33 @@ module Solr4R
67
68
 
68
69
  private
69
70
 
70
- def evaluate_result(options)
71
+ def evaluate_result
71
72
  case wt = request_params[:wt]
72
73
  when String then to_s
73
- when :ruby then eval(to_s)
74
- when :json then JSON.parse(to_s, options)
74
+ when :ruby then extend_hash(eval(to_s))
75
+ when :json then extend_hash(JSON.parse(to_s))
75
76
  else raise 'The response cannot be evaluated: wt=%p not supported.' % wt
76
77
  end
77
78
  end
78
79
 
79
80
  def evaluate_count(name)
80
81
  case wt = request_params[:wt]
81
- # numFound="35"
82
- when 'xml' then Integer(result[/\s#{name}="(\d+)"/, 1])
83
- # 'numFound'=>35
84
- when 'ruby' then Integer(result[/'#{name}'=>(\d+)/, 1])
85
- # "numFound":35
86
- when 'json' then Integer(result[/"#{name}":(\d+)/, 1])
87
- # { 'response' => 'numFound' } OR { :response => :numFound }
88
- when :ruby, :json then result.fetch(key = 'response') {
89
- name = name.to_sym; result.fetch(key.to_sym) }.fetch(name)
82
+ when 'xml' then extract_int(name, '\s%s="%s"') # numFound="35"
83
+ when 'ruby' then extract_int(name, "'%s'=>%s") # 'numFound'=>35
84
+ when 'json' then extract_int(name, '"%s":%s') # "numFound":35
85
+ when Symbol then result % ['response', name] # {"response"=>{"numFound"=>35}}
90
86
  else raise 'The count cannot be extracted: wt=%p not supported.' % wt
91
87
  end
92
88
  end
93
89
 
90
+ def extract_int(name, pattern)
91
+ Integer(result[Regexp.new(pattern % [name, '(\d+)']), 1])
92
+ end
93
+
94
+ def extend_hash(object)
95
+ object.is_a?(Hash) ? Result.new(self, object) : object
96
+ end
97
+
94
98
  end
95
99
 
96
100
  end
@@ -0,0 +1,79 @@
1
+ # encoding: utf-8
2
+
3
+ #--
4
+ ###############################################################################
5
+ # #
6
+ # solr4r -- A Ruby client for Apache Solr #
7
+ # #
8
+ # Copyright (C) 2014 Jens Wille #
9
+ # #
10
+ # Mir is free software: you can redistribute it and/or modify it under the #
11
+ # terms of the GNU Affero General Public License as published by the Free #
12
+ # Software Foundation, either version 3 of the License, or (at your option) #
13
+ # any later version. #
14
+ # #
15
+ # solr4r is distributed in the hope that it will be useful, but WITHOUT ANY #
16
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
17
+ # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
18
+ # more details. #
19
+ # #
20
+ # You should have received a copy of the GNU Affero General Public License #
21
+ # along with solr4r. If not, see <http://www.gnu.org/licenses/>. #
22
+ # #
23
+ ###############################################################################
24
+ #++
25
+
26
+ require 'forwardable'
27
+ require 'nuggets/hash/deep_fetch_mixin'
28
+
29
+ require_relative 'document'
30
+
31
+ module Solr4R
32
+
33
+ class Result
34
+
35
+ include Enumerable
36
+
37
+ extend Forwardable
38
+
39
+ def_delegators :response, :to_i
40
+ def_delegators :to_hash, :%, :deep_fetch
41
+
42
+ def initialize(response, hash)
43
+ @response, @hash = response, hash.extend(Nuggets::Hash::DeepFetchMixin)
44
+ extend(Error) if hash.key?('error')
45
+ end
46
+
47
+ attr_reader :response
48
+
49
+ def to_hash
50
+ @hash
51
+ end
52
+
53
+ def each
54
+ return enum_for(:each) unless block_given?
55
+
56
+ deep_fetch('response/docs').each { |hash|
57
+ yield Document.new(self, hash)
58
+ }
59
+ end
60
+
61
+ def error?
62
+ is_a?(Error)
63
+ end
64
+
65
+ module Error
66
+
67
+ def to_i
68
+ 0
69
+ end
70
+
71
+ def each
72
+ []
73
+ end
74
+
75
+ end
76
+
77
+ end
78
+
79
+ end
@@ -43,7 +43,7 @@ module Solr4R
43
43
  self.params, query = params, [self.query].compact
44
44
 
45
45
  params.each { |key, value|
46
- key = URI.escape(key.to_s)
46
+ key = CGI.escape(key.to_s)
47
47
  Array(value).each { |val| query << "#{key}=#{CGI.escape(val.to_s)}" }
48
48
  }
49
49
 
@@ -4,7 +4,7 @@ module Solr4R
4
4
 
5
5
  MAJOR = 0
6
6
  MINOR = 0
7
- TINY = 2
7
+ TINY = 3
8
8
 
9
9
  class << self
10
10
 
@@ -1,3 +1,7 @@
1
1
  $:.unshift('lib') unless $:.first == 'lib'
2
2
 
3
3
  require 'solr4r'
4
+
5
+ RSpec.configure { |config|
6
+ config.expect_with(:rspec) { |c| c.syntax = [:should, :expect] }
7
+ }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solr4r
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jens Wille
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-17 00:00:00.000000000 Z
11
+ date: 2014-09-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: nuggets
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: hen
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -83,9 +97,11 @@ files:
83
97
  - lib/solr4r.rb
84
98
  - lib/solr4r/builder.rb
85
99
  - lib/solr4r/client.rb
100
+ - lib/solr4r/document.rb
86
101
  - lib/solr4r/request.rb
87
102
  - lib/solr4r/request_extension.rb
88
103
  - lib/solr4r/response.rb
104
+ - lib/solr4r/result.rb
89
105
  - lib/solr4r/uri_extension.rb
90
106
  - lib/solr4r/version.rb
91
107
  - spec/solr4r/builder_spec.rb
@@ -96,14 +112,18 @@ licenses:
96
112
  metadata: {}
97
113
  post_install_message: |2+
98
114
 
99
- solr4r-0.0.2 [2014-04-17]:
115
+ solr4r-0.0.3 [2014-09-19]:
100
116
 
101
- * Refactored Solr4R::Request away from curb.
102
- * Refactored Solr4R::Response away from ostruct.
117
+ * Added Solr4R::Client#json.
118
+ * Added Solr4R::Client#json_query.
119
+ * Added Solr4R::Client#solr_version.
120
+ * Updated Solr4R::Request with some fixes.
121
+ * Refactored Solr4R::Response to use wrapper object.
122
+ * Refactored Solr4R::Builder to use custom document class.
103
123
 
104
124
  rdoc_options:
105
125
  - "--title"
106
- - solr4r Application documentation (v0.0.2)
126
+ - solr4r Application documentation (v0.0.3)
107
127
  - "--charset"
108
128
  - UTF-8
109
129
  - "--line-numbers"
@@ -124,7 +144,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
144
  version: '0'
125
145
  requirements: []
126
146
  rubyforge_project:
127
- rubygems_version: 2.2.2
147
+ rubygems_version: 2.4.1
128
148
  signing_key:
129
149
  specification_version: 4
130
150
  summary: A Ruby client for Apache Solr.