solr4r 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,103 @@
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 'json'
27
+ require 'ostruct'
28
+ require 'webrick'
29
+
30
+ module Solr4R
31
+
32
+ class Response < OpenStruct
33
+
34
+ REQUEST_LINE = %r{\AHTTP/.*\r?\n?}
35
+
36
+ CHARSET_RE = %r{;\s*charset=([\w-]+)}
37
+
38
+ DEFAULT_CHARSET = 'UTF-8'
39
+
40
+ def initialize
41
+ super
42
+ yield self if block_given?
43
+ end
44
+
45
+ def result(options = {})
46
+ @result ||= evaluate_result(options)
47
+ end
48
+
49
+ def response_headers
50
+ @response_headers ||= evaluate_header
51
+ end
52
+
53
+ def num_found
54
+ @num_found ||= evaluate_count('numFound')
55
+ end
56
+
57
+ def charset
58
+ @charset ||= response_headers.fetch('content-type')[0][CHARSET_RE, 1]
59
+ end
60
+
61
+ def to_s
62
+ @to_s ||= response_body.to_s.force_encoding(charset || DEFAULT_CHARSET)
63
+ end
64
+
65
+ def inspect
66
+ '#<%s:0x%x @request_url=%p, @request_headers=%p, @response_headers=%p, @response_code=%p>' % [
67
+ self.class, object_id, request_url, request_headers, response_headers, response_code
68
+ ]
69
+ end
70
+
71
+ private
72
+
73
+ def evaluate_result(options)
74
+ case wt = request_params[:wt]
75
+ when String then to_s
76
+ when :ruby then eval(to_s)
77
+ when :json then JSON.parse(to_s, options)
78
+ else raise 'The response cannot be evaluated: wt=%p not supported.' % wt
79
+ end
80
+ end
81
+
82
+ def evaluate_header
83
+ WEBrick::HTTPUtils.parse_header(response_header.sub(REQUEST_LINE, ''))
84
+ end
85
+
86
+ def evaluate_count(name)
87
+ case wt = request_params[:wt]
88
+ # numFound="35"
89
+ when 'xml' then Integer(result[/\s#{name}="(\d+)"/, 1])
90
+ # 'numFound'=>35
91
+ when 'ruby' then Integer(result[/'#{name}'=>(\d+)/, 1])
92
+ # "numFound":35
93
+ when 'json' then Integer(result[/"#{name}":(\d+)/, 1])
94
+ # { 'response' => 'numFound' } OR { :response => :numFound }
95
+ when :ruby, :json then result.fetch(key = 'response') {
96
+ name = name.to_sym; result.fetch(key.to_sym) }.fetch(name)
97
+ else raise 'The count cannot be extracted: wt=%p not supported.' % wt
98
+ end
99
+ end
100
+
101
+ end
102
+
103
+ end
@@ -0,0 +1,27 @@
1
+ module Solr4R
2
+
3
+ module Version
4
+
5
+ MAJOR = 0
6
+ MINOR = 0
7
+ TINY = 1
8
+
9
+ class << self
10
+
11
+ # Returns array representation.
12
+ def to_a
13
+ [MAJOR, MINOR, TINY]
14
+ end
15
+
16
+ # Short-cut for version string.
17
+ def to_s
18
+ to_a.join('.')
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+
25
+ VERSION = Version.to_s
26
+
27
+ end
data/lib/solr4r.rb ADDED
@@ -0,0 +1,42 @@
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
+ module Solr4R
27
+
28
+ class << self
29
+
30
+ def connect(*args)
31
+ Client.new(*args)
32
+ end
33
+
34
+ end
35
+
36
+ end
37
+
38
+ require_relative 'solr4r/version'
39
+ require_relative 'solr4r/builder'
40
+ require_relative 'solr4r/request'
41
+ require_relative 'solr4r/response'
42
+ require_relative 'solr4r/client'
@@ -0,0 +1,210 @@
1
+ describe Solr4R::Builder do
2
+
3
+ before :all do
4
+ @builder = Solr4R::Builder.new
5
+ end
6
+
7
+ describe '#add' do
8
+
9
+ example do
10
+ @builder.add(employeeId: '05991', office: 'Bridgewater', skills: %w[Perl Java]).should == <<-EOT
11
+ <?xml version="1.0" encoding="UTF-8"?>
12
+ <add>
13
+ <doc>
14
+ <field name="employeeId">05991</field>
15
+ <field name="office">Bridgewater</field>
16
+ <field name="skills">Perl</field>
17
+ <field name="skills">Java</field>
18
+ </doc>
19
+ </add>
20
+ EOT
21
+ end
22
+
23
+ example do
24
+ @builder.add([{ employeeId: '05992', office: 'Blackwater' }, { employeeId: '05993', skills: 'Ruby' }]).should == <<-EOT
25
+ <?xml version="1.0" encoding="UTF-8"?>
26
+ <add>
27
+ <doc>
28
+ <field name="employeeId">05992</field>
29
+ <field name="office">Blackwater</field>
30
+ </doc>
31
+ <doc>
32
+ <field name="employeeId">05993</field>
33
+ <field name="skills">Ruby</field>
34
+ </doc>
35
+ </add>
36
+ EOT
37
+ end
38
+
39
+ example do
40
+ @builder.add([id: 42, text: 'blah'], commitWithin: 23).should == <<-EOT
41
+ <?xml version="1.0" encoding="UTF-8"?>
42
+ <add commitWithin="23">
43
+ <doc>
44
+ <field name="id">42</field>
45
+ <field name="text">blah</field>
46
+ </doc>
47
+ </add>
48
+ EOT
49
+ end
50
+
51
+ example do
52
+ @builder.add([[{ id: 42, text: 'blah' }, { boost: 10.0 }]]).should == <<-EOT
53
+ <?xml version="1.0" encoding="UTF-8"?>
54
+ <add>
55
+ <doc boost="10.0">
56
+ <field name="id">42</field>
57
+ <field name="text">blah</field>
58
+ </doc>
59
+ </add>
60
+ EOT
61
+ end
62
+
63
+ example do
64
+ @builder.add(id: 42, text: ['blah', boost: 2.0]).should == <<-EOT
65
+ <?xml version="1.0" encoding="UTF-8"?>
66
+ <add>
67
+ <doc>
68
+ <field name="id">42</field>
69
+ <field boost="2.0" name="text">blah</field>
70
+ </doc>
71
+ </add>
72
+ EOT
73
+ end
74
+
75
+ example do
76
+ @builder.add([[{ id: 42, text: ['blah', boost: 2.0] }, { boost: 10.0 }]], commitWithin: 23).should == <<-EOT
77
+ <?xml version="1.0" encoding="UTF-8"?>
78
+ <add commitWithin="23">
79
+ <doc boost="10.0">
80
+ <field name="id">42</field>
81
+ <field boost="2.0" name="text">blah</field>
82
+ </doc>
83
+ </add>
84
+ EOT
85
+ end
86
+
87
+ end
88
+
89
+ describe '#commit' do
90
+
91
+ example do
92
+ @builder.commit.should == <<-EOT
93
+ <?xml version="1.0" encoding="UTF-8"?>
94
+ <commit/>
95
+ EOT
96
+ end
97
+
98
+ example do
99
+ @builder.commit(softCommit: true).should == <<-EOT
100
+ <?xml version="1.0" encoding="UTF-8"?>
101
+ <commit softCommit="true"/>
102
+ EOT
103
+ end
104
+
105
+ end
106
+
107
+ describe '#optimize' do
108
+
109
+ example do
110
+ @builder.optimize.should == <<-EOT
111
+ <?xml version="1.0" encoding="UTF-8"?>
112
+ <optimize/>
113
+ EOT
114
+ end
115
+
116
+ example do
117
+ @builder.optimize(maxSegments: 42).should == <<-EOT
118
+ <?xml version="1.0" encoding="UTF-8"?>
119
+ <optimize maxSegments="42"/>
120
+ EOT
121
+ end
122
+
123
+ end
124
+
125
+ describe '#rollback' do
126
+
127
+ example do
128
+ @builder.rollback.should == <<-EOT
129
+ <?xml version="1.0" encoding="UTF-8"?>
130
+ <rollback/>
131
+ EOT
132
+ end
133
+
134
+ end
135
+
136
+ describe '#delete' do
137
+
138
+ example do
139
+ @builder.delete(id: '05991').should == <<-EOT
140
+ <?xml version="1.0" encoding="UTF-8"?>
141
+ <delete>
142
+ <id>05991</id>
143
+ </delete>
144
+ EOT
145
+ end
146
+
147
+ example do
148
+ @builder.delete(id: %w[05991 06000]).should == <<-EOT
149
+ <?xml version="1.0" encoding="UTF-8"?>
150
+ <delete>
151
+ <id>05991</id>
152
+ <id>06000</id>
153
+ </delete>
154
+ EOT
155
+ end
156
+
157
+ example do
158
+ @builder.delete(query: 'office:Bridgewater').should == <<-EOT
159
+ <?xml version="1.0" encoding="UTF-8"?>
160
+ <delete>
161
+ <query>office:Bridgewater</query>
162
+ </delete>
163
+ EOT
164
+ end
165
+
166
+ example do
167
+ @builder.delete(query: %w[office:Bridgewater office:Osaka]).should == <<-EOT
168
+ <?xml version="1.0" encoding="UTF-8"?>
169
+ <delete>
170
+ <query>office:Bridgewater</query>
171
+ <query>office:Osaka</query>
172
+ </delete>
173
+ EOT
174
+ end
175
+
176
+ example do
177
+ @builder.delete(query: { office: 'Bridgewater', skills: 'Perl' }).should == <<-EOT
178
+ <?xml version="1.0" encoding="UTF-8"?>
179
+ <delete>
180
+ <query>office:Bridgewater</query>
181
+ <query>skills:Perl</query>
182
+ </delete>
183
+ EOT
184
+ end
185
+
186
+ example do
187
+ @builder.delete(query: { office: %w[Bridgewater Osaka] }).should == <<-EOT
188
+ <?xml version="1.0" encoding="UTF-8"?>
189
+ <delete>
190
+ <query>office:Bridgewater</query>
191
+ <query>office:Osaka</query>
192
+ </delete>
193
+ EOT
194
+ end
195
+
196
+ example do
197
+ @builder.delete(id: %w[05991 06000], query: { office: %w[Bridgewater Osaka] }).should == <<-EOT
198
+ <?xml version="1.0" encoding="UTF-8"?>
199
+ <delete>
200
+ <id>05991</id>
201
+ <id>06000</id>
202
+ <query>office:Bridgewater</query>
203
+ <query>office:Osaka</query>
204
+ </delete>
205
+ EOT
206
+ end
207
+
208
+ end
209
+
210
+ end
@@ -0,0 +1,3 @@
1
+ $:.unshift('lib') unless $:.first == 'lib'
2
+
3
+ require 'solr4r'
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: solr4r
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jens Wille
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: curb
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.8'
20
+ - - ">"
21
+ - !ruby/object:Gem::Version
22
+ version: 0.8.5
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '0.8'
30
+ - - ">"
31
+ - !ruby/object:Gem::Version
32
+ version: 0.8.5
33
+ - !ruby/object:Gem::Dependency
34
+ name: nokogiri
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.6'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.6'
47
+ description: Access the Apache Solr search server from Ruby
48
+ email: jens.wille@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files:
52
+ - README
53
+ - COPYING
54
+ - ChangeLog
55
+ files:
56
+ - COPYING
57
+ - ChangeLog
58
+ - README
59
+ - Rakefile
60
+ - TODO
61
+ - lib/solr4r.rb
62
+ - lib/solr4r/builder.rb
63
+ - lib/solr4r/client.rb
64
+ - lib/solr4r/request.rb
65
+ - lib/solr4r/response.rb
66
+ - lib/solr4r/version.rb
67
+ - spec/solr4r/builder_spec.rb
68
+ - spec/spec_helper.rb
69
+ homepage: http://github.com/blackwinter/solr4r
70
+ licenses:
71
+ - AGPL-3.0
72
+ metadata: {}
73
+ post_install_message: |2+
74
+
75
+ solr4r-0.0.1 [2014-03-28]:
76
+
77
+ * Birthday :-)
78
+
79
+ rdoc_options:
80
+ - "--title"
81
+ - solr4r Application documentation (v0.0.1)
82
+ - "--charset"
83
+ - UTF-8
84
+ - "--line-numbers"
85
+ - "--all"
86
+ - "--main"
87
+ - README
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: 1.9.3
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.2.2
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: A Ruby client for Apache Solr
106
+ test_files: []