solr4r 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d3dc3a37e421ae648cb13923219a9f386a50eb1d
4
- data.tar.gz: 8ddb4ffb6df9364345554ca7b41ec32cad8f346e
3
+ metadata.gz: 586864696853144c04da2e5cea908d23f165ac93
4
+ data.tar.gz: a8b43ec8857d0a4d78ad29d659599e3460cac848
5
5
  SHA512:
6
- metadata.gz: e617a5540b822c6c635d809aa6e3575f32b6d2a7dcd56a04bcdfd36d4014da5a239811ca254397babd285804010b98d4ef78978f293c8ae54c5c0184e066d765
7
- data.tar.gz: 28a4a5c40629e990368ab284646ad53137e2a95d7991ab35b6049cd34109f9f14848165d012bf3320339ae71f6babef2954facb79f04b5824eb1226355b0856c
6
+ metadata.gz: b5cf30799e4a101d6eaddbdc7e80b9df1fc77c05f9c19a8c83e99d8880c229d76b0ac26f60ea7cc0229cdb970014b4a37d00dbd65507a9615dc179adb9172b4d
7
+ data.tar.gz: 5c821ebf42f7be62f81229adbb328f43e179a198aba53c91fa818f023e85532f8e970c8444608ad18a677ef17b156fc437c38007a16e3fac5ecf03752252af59
data/ChangeLog CHANGED
@@ -2,6 +2,11 @@
2
2
 
3
3
  = Revision history for solr4r
4
4
 
5
+ == 0.0.2 [2014-04-17]
6
+
7
+ * Refactored Solr4R::Request away from curb.
8
+ * Refactored Solr4R::Response away from ostruct.
9
+
5
10
  == 0.0.1 [2014-03-28]
6
11
 
7
12
  * Birthday :-)
data/README CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  == VERSION
4
4
 
5
- This documentation refers to solr4r version 0.0.1
5
+ This documentation refers to solr4r version 0.0.2
6
6
 
7
7
 
8
8
  == DESCRIPTION
@@ -12,9 +12,10 @@ TODO
12
12
 
13
13
  == LINKS
14
14
 
15
- Documentation:: http://blackwinter.github.com/solr4r
16
- Source code:: http://github.com/blackwinter/solr4r
17
- RubyGem:: http://rubygems.org/gems/solr4r
15
+ Documentation:: https://blackwinter.github.io/solr4r/
16
+ Source code:: https://github.com/blackwinter/solr4r
17
+ RubyGem:: https://rubygems.org/gems/solr4r
18
+ Travis CI:: https://travis-ci.org/blackwinter/solr4r
18
19
 
19
20
 
20
21
  == AUTHORS
data/Rakefile CHANGED
@@ -7,13 +7,13 @@ begin
7
7
  gem: {
8
8
  name: %q{solr4r},
9
9
  version: Solr4R::VERSION,
10
- summary: %q{A Ruby client for Apache Solr},
11
- description: %q{Access the Apache Solr search server from Ruby},
10
+ summary: %q{A Ruby client for Apache Solr.},
11
+ description: %q{Access the Apache Solr search server from Ruby.},
12
12
  author: %q{Jens Wille},
13
13
  email: %q{jens.wille@gmail.com},
14
14
  license: %q{AGPL-3.0},
15
15
  homepage: :blackwinter,
16
- dependencies: { curb: ['~> 0.8', '> 0.8.5'], nokogiri: '~> 1.6' },
16
+ dependencies: { nokogiri: '~> 1.6' },
17
17
 
18
18
  required_ruby_version: '>= 1.9.3'
19
19
  }
data/TODO CHANGED
@@ -1,3 +1,2 @@
1
- * request: https://github.com/taf2/curb/pull/196
2
1
  * documentation
3
2
  * specs
data/lib/solr4r/client.rb CHANGED
@@ -23,14 +23,12 @@
23
23
  ###############################################################################
24
24
  #++
25
25
 
26
- require 'uri'
27
-
28
26
  module Solr4R
29
27
 
30
28
  class Client
31
29
 
32
30
  DEFAULT_HOST = 'localhost'
33
- DEFAULT_PATH = 'solr/'
31
+ DEFAULT_PATH = 'solr'
34
32
  DEFAULT_PORT = 8983
35
33
 
36
34
  DEFAULT_PARAMS = {
@@ -49,22 +47,22 @@ module Solr4R
49
47
 
50
48
  def initialize(options = {})
51
49
  if options.is_a?(String)
52
- url, options = options, {}
50
+ uri, options = options, {}
53
51
  else
54
- url = options.fetch(:url, default_url(options))
52
+ uri = options.fetch(:uri, default_uri(options))
55
53
  end
56
54
 
57
- self.url, self.options = URI.parse(url), options
55
+ self.options = options
58
56
 
59
- self.request = options.fetch(:request, Request.new)
60
57
  self.builder = options.fetch(:builder, Builder.new)
58
+ self.request = options.fetch(:request, Request.new(uri))
61
59
 
62
60
  self.default_params = options.fetch(:default_params, DEFAULT_PARAMS)
63
61
 
64
62
  register_endpoints(options.fetch(:endpoints, DEFAULT_ENDPOINTS))
65
63
  end
66
64
 
67
- attr_accessor :url, :options, :request, :builder, :default_params
65
+ attr_accessor :options, :builder, :request, :default_params
68
66
 
69
67
  def register_endpoints(endpoints)
70
68
  endpoints.each { |args| register_endpoint(*args) } if endpoints
@@ -150,14 +148,14 @@ module Solr4R
150
148
  end
151
149
 
152
150
  def inspect
153
- '#<%s:0x%x @url=%p, @default_params=%p>' % [
154
- self.class, object_id, url, default_params
151
+ '#<%s:0x%x @default_params=%p %s>' % [
152
+ self.class, object_id, default_params, request.request_line
155
153
  ]
156
154
  end
157
155
 
158
156
  private
159
157
 
160
- def default_url(options)
158
+ def default_uri(options)
161
159
  'http://%s:%d/%s' % [
162
160
  options.fetch(:host, DEFAULT_HOST),
163
161
  options.fetch(:port, DEFAULT_PORT),
@@ -170,8 +168,8 @@ module Solr4R
170
168
  end
171
169
 
172
170
  def send_request(path, options, &block)
173
- options = amend_options(options, :params, default_params)
174
- request.execute(URI.join(url, path), options, &block)
171
+ request.execute(path,
172
+ amend_options(options, :params, default_params), &block)
175
173
  end
176
174
 
177
175
  def invalid_endpoint?(name)
@@ -23,98 +23,117 @@
23
23
  ###############################################################################
24
24
  #++
25
25
 
26
- require 'curl'
26
+ require 'net/https'
27
+
28
+ require_relative 'request_extension'
29
+ require_relative 'uri_extension'
27
30
 
28
31
  module Solr4R
29
32
 
30
- class Request < Curl::Easy
33
+ class Request
31
34
 
32
- DEFAULT_VERB = :get
35
+ DEFAULT_METHOD = :get
33
36
 
34
37
  DEFAULT_USER_AGENT = "Solr4R/#{VERSION}"
35
38
 
36
- RESPONSE_ATTRIBUTES = {
37
- # request
38
- headers: :request_headers,
39
- last_effective_url: :request_url,
40
- post_body: :post_body,
41
- params: :request_params,
42
- verb: :request_verb,
43
-
44
- # response
45
- body_str: :response_body,
46
- content_type: :content_type,
47
- header_str: :response_header,
48
- response_code: :response_code
49
- }
50
-
51
- def initialize(options = {})
52
- if block_given?
53
- raise ArgumentError,
54
- 'block argument not supported, use options hash instead'
55
- end
39
+ def initialize(base_uri, http_options = {})
40
+ self.base_uri, self.http_options =
41
+ URI(base_uri).extend(BaseUriExtension), http_options
42
+ end
43
+
44
+ def start
45
+ self.http = Net::HTTP.start(base_uri.hostname, base_uri.port,
46
+ { use_ssl: base_uri.scheme == 'https' }.merge(http_options))
47
+
48
+ self
49
+ end
56
50
 
57
- super()
51
+ def finish
52
+ http.finish if started?
53
+ self
54
+ end
58
55
 
59
- self.options = options.merge(params: nil, verb: nil)
60
- set_options
56
+ def started?
57
+ http && http.started?
61
58
  end
62
59
 
63
- attr_accessor :options, :params, :verb
60
+ def execute(path, options = {}, &block)
61
+ start unless started?
64
62
 
65
- def execute(request_url, options = {}, &block)
66
- prepare_request(request_url, options, &block)
63
+ self.last_response = nil
67
64
 
68
- send("http_#{verb}")
65
+ req = prepare_request(path, options, &block)
66
+ res = http.request(req)
69
67
 
70
- Response.new { |response|
71
- RESPONSE_ATTRIBUTES.each { |attribute, key|
72
- response.send("#{key}=", send(attribute))
73
- }
74
- }
68
+ self.last_response = Response.new(
69
+ request_body: req.body,
70
+ request_headers: req.to_hash,
71
+ request_method: req.method.downcase.to_sym,
72
+ request_params: req.params,
73
+ request_url: req.uri.to_s,
74
+
75
+ response_body: res.body,
76
+ response_charset: res.type_params['charset'],
77
+ response_code: res.code.to_i,
78
+ response_headers: res.to_hash
79
+ )
75
80
  end
76
81
 
77
- def reset
78
- super.tap {
79
- set_options
80
- headers['User-Agent'] ||= DEFAULT_USER_AGENT
81
- }
82
+ def request_line
83
+ last_response ? last_response.request_line : "[#{base_uri}]"
82
84
  end
83
85
 
84
86
  def inspect
85
- '#<%s:0x%x @options=%p, @verb=%p, @url=%p, @response_code=%p>' % [
86
- self.class, object_id, options, verb, url, response_code
87
- ]
87
+ '#<%s:0x%x %s>' % [self.class, object_id, request_line]
88
88
  end
89
89
 
90
90
  private
91
91
 
92
- def set_options
93
- options.each { |key, value| send("#{key}=", value) }
94
- end
92
+ attr_accessor :base_uri, :http_options, :http, :last_response
93
+
94
+ def prepare_request(path, options)
95
+ uri = make_uri(path, options.fetch(:params, {}))
96
+
97
+ req = make_request(uri,
98
+ options.fetch(:method, DEFAULT_METHOD),
99
+ options.fetch(:data, {}))
95
100
 
96
- def prepare_request(request_url, options)
97
- reset
101
+ set_headers(req, options.fetch(:headers, {}))
102
+
103
+ yield req if block_given?
104
+
105
+ req
106
+ end
98
107
 
99
- self.url = Curl.urlalize(request_url.to_s,
100
- self.params = options.fetch(:params, {}))
108
+ def make_uri(path, params)
109
+ base_uri.merge(path).extend(RequestUriExtension).with_params(params)
110
+ end
101
111
 
102
- case self.verb = options.fetch(:method, DEFAULT_VERB)
112
+ def make_request(uri, method, data)
113
+ case method
103
114
  when :get, :head
104
- # ok
105
- when :post, :delete, :patch
106
- self.post_body = Curl.postalize(options[:data])
107
- when :put
108
- self.put_data = Curl.postalize(options[:data])
115
+ req = request_for(method, uri)
116
+ when :post
117
+ req = request_for(method, uri)
118
+ req.set_form_data(data)
109
119
  else
110
- raise ArgumentError, "verb not supported: #{verb}"
120
+ raise ArgumentError, "method not supported: #{method}"
111
121
  end
112
122
 
113
- headers.update(options[:headers]) if options[:headers]
123
+ req
124
+ end
125
+
126
+ def request_for(method, uri)
127
+ Net::HTTP.const_get(method.to_s.capitalize)
128
+ .extend(HTTPRequestExtension).from_uri(uri)
129
+ end
114
130
 
115
- yield self if block_given?
131
+ def set_headers(req, headers)
132
+ req['User-Agent'] = DEFAULT_USER_AGENT
116
133
 
117
- self
134
+ headers.each { |key, value|
135
+ Array(value).each { |val| req.add_field(key, val) }
136
+ }
118
137
  end
119
138
 
120
139
  end
@@ -0,0 +1,44 @@
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 'cgi'
27
+
28
+ module Solr4R
29
+
30
+ module HTTPRequestExtension
31
+
32
+ def self.extended(base)
33
+ base.send(:attr_accessor, :params)
34
+ end
35
+
36
+ def from_uri(uri)
37
+ req = new(uri)
38
+ req.params = uri.params
39
+ req
40
+ end
41
+
42
+ end
43
+
44
+ end
@@ -24,42 +24,39 @@
24
24
  #++
25
25
 
26
26
  require 'json'
27
- require 'ostruct'
28
- require 'webrick'
29
27
 
30
28
  module Solr4R
31
29
 
32
- class Response < OpenStruct
33
-
34
- REQUEST_LINE = %r{\AHTTP/.*\r?\n?}
35
-
36
- CHARSET_RE = %r{;\s*charset=([\w-]+)}
30
+ class Response
37
31
 
38
32
  DEFAULT_CHARSET = 'UTF-8'
39
33
 
40
- def initialize
41
- super
42
- yield self if block_given?
34
+ EVALUATE_METHODS = [:get, :post]
35
+
36
+ def initialize(options)
37
+ options.each { |key, value| send("#{key}=", value) }
38
+ @evaluate = EVALUATE_METHODS.include?(request_method)
43
39
  end
44
40
 
45
- def result(options = {})
46
- @result ||= evaluate_result(options)
41
+ attr_accessor :response_body, :response_charset, :response_code, :response_headers,
42
+ :request_body, :request_method, :request_params, :request_url, :request_headers
43
+
44
+ def request_line
45
+ '"%s %s" %d' % [request_method.upcase, request_url, response_code]
47
46
  end
48
47
 
49
- def response_headers
50
- @response_headers ||= evaluate_header
48
+ def result(options = {})
49
+ @result ||= evaluate_result(options) if @evaluate
51
50
  end
52
51
 
53
52
  def num_found
54
- @num_found ||= evaluate_count('numFound')
53
+ @num_found ||= evaluate_count('numFound') if @evaluate
55
54
  end
56
55
 
57
- def charset
58
- @charset ||= response_headers.fetch('content-type')[0][CHARSET_RE, 1]
59
- end
56
+ alias_method :to_i, :num_found
60
57
 
61
58
  def to_s
62
- @to_s ||= response_body.to_s.force_encoding(charset || DEFAULT_CHARSET)
59
+ @to_s ||= response_body.to_s.force_encoding(response_charset || DEFAULT_CHARSET)
63
60
  end
64
61
 
65
62
  def inspect
@@ -79,10 +76,6 @@ module Solr4R
79
76
  end
80
77
  end
81
78
 
82
- def evaluate_header
83
- WEBrick::HTTPUtils.parse_header(response_header.sub(REQUEST_LINE, ''))
84
- end
85
-
86
79
  def evaluate_count(name)
87
80
  case wt = request_params[:wt]
88
81
  # numFound="35"
@@ -0,0 +1,57 @@
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 'cgi'
27
+
28
+ module Solr4R
29
+
30
+ module BaseUriExtension
31
+
32
+ def self.extended(uri)
33
+ uri.path << '/' unless uri.path.end_with?('/')
34
+ end
35
+
36
+ end
37
+
38
+ module RequestUriExtension
39
+
40
+ attr_accessor :params
41
+
42
+ def with_params(params)
43
+ self.params, query = params, [self.query].compact
44
+
45
+ params.each { |key, value|
46
+ key = URI.escape(key.to_s)
47
+ Array(value).each { |val| query << "#{key}=#{CGI.escape(val.to_s)}" }
48
+ }
49
+
50
+ self.query = query.join('&') unless query.empty?
51
+
52
+ self
53
+ end
54
+
55
+ end
56
+
57
+ end
@@ -4,7 +4,7 @@ module Solr4R
4
4
 
5
5
  MAJOR = 0
6
6
  MINOR = 0
7
- TINY = 1
7
+ TINY = 2
8
8
 
9
9
  class << self
10
10
 
metadata CHANGED
@@ -1,50 +1,72 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solr4r
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
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-02 00:00:00.000000000 Z
11
+ date: 2014-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: curb
14
+ name: nokogiri
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.8'
20
- - - ">"
21
- - !ruby/object:Gem::Version
22
- version: 0.8.5
19
+ version: '1.6'
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
24
  - - "~>"
28
25
  - !ruby/object:Gem::Version
29
- version: '0.8'
30
- - - ">"
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: hen
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
31
32
  - !ruby/object:Gem::Version
32
- version: 0.8.5
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
33
41
  - !ruby/object:Gem::Dependency
34
- name: nokogiri
42
+ name: rake
35
43
  requirement: !ruby/object:Gem::Requirement
36
44
  requirements:
37
- - - "~>"
45
+ - - ">="
38
46
  - !ruby/object:Gem::Version
39
- version: '1.6'
40
- type: :runtime
47
+ version: '0'
48
+ type: :development
41
49
  prerelease: false
42
50
  version_requirements: !ruby/object:Gem::Requirement
43
51
  requirements:
44
- - - "~>"
52
+ - - ">="
45
53
  - !ruby/object:Gem::Version
46
- version: '1.6'
47
- description: Access the Apache Solr search server from Ruby
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Access the Apache Solr search server from Ruby.
48
70
  email: jens.wille@gmail.com
49
71
  executables: []
50
72
  extensions: []
@@ -62,7 +84,9 @@ files:
62
84
  - lib/solr4r/builder.rb
63
85
  - lib/solr4r/client.rb
64
86
  - lib/solr4r/request.rb
87
+ - lib/solr4r/request_extension.rb
65
88
  - lib/solr4r/response.rb
89
+ - lib/solr4r/uri_extension.rb
66
90
  - lib/solr4r/version.rb
67
91
  - spec/solr4r/builder_spec.rb
68
92
  - spec/spec_helper.rb
@@ -72,13 +96,14 @@ licenses:
72
96
  metadata: {}
73
97
  post_install_message: |2+
74
98
 
75
- solr4r-0.0.1 [2014-03-28]:
99
+ solr4r-0.0.2 [2014-04-17]:
76
100
 
77
- * Birthday :-)
101
+ * Refactored Solr4R::Request away from curb.
102
+ * Refactored Solr4R::Response away from ostruct.
78
103
 
79
104
  rdoc_options:
80
105
  - "--title"
81
- - solr4r Application documentation (v0.0.1)
106
+ - solr4r Application documentation (v0.0.2)
82
107
  - "--charset"
83
108
  - UTF-8
84
109
  - "--line-numbers"
@@ -102,5 +127,5 @@ rubyforge_project:
102
127
  rubygems_version: 2.2.2
103
128
  signing_key:
104
129
  specification_version: 4
105
- summary: A Ruby client for Apache Solr
130
+ summary: A Ruby client for Apache Solr.
106
131
  test_files: []