Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Update to support Sunspot 1.3.0 #2

Open
wants to merge 4 commits into from

3 participants

@woodhull

The latest version of Sunspot is required to use Sunspot with Rails 3.2.

Nathan

@justinko
Owner

Thanks for the pull!

I am not a fan of alias_method in this context. Can you switch it to using modules? Something like this:

module RSolrClientExtension
  def adapt_response(request, response)
    if response[:status] == 401
      result = response[:body]
      result.extend Context
      result.request = request
      result.response = response
      return result
    else
      super
    end
  end
end

RSolr::Client.send(:include, RSolrClientExtension)

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 11, 2011
  1. @andhapp
  2. @andhapp

    Fixed the specs.

    andhapp authored
Commits on Jan 22, 2012
  1. @woodhull

    update to latest sunspot

    woodhull authored
  2. @woodhull
This page is out of date. Refresh to see the latest.
View
2  .gitignore
@@ -1,4 +1,4 @@
*.gem
.bundle
-Gemfile.lock
pkg/*
+vendor
View
40 Gemfile.lock
@@ -0,0 +1,40 @@
+PATH
+ remote: .
+ specs:
+ sunspot-rails-http-basic-auth (0.0.2)
+ sunspot_rails (~> 1.3.0.rc4)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ builder (3.0.0)
+ diff-lcs (1.1.3)
+ escape (0.0.4)
+ fakeweb (1.3.0)
+ nokogiri (1.5.0)
+ pr_geohash (1.0.0)
+ rsolr (1.0.2)
+ builder (>= 2.1.2)
+ rspec (2.6.0)
+ rspec-core (~> 2.6.0)
+ rspec-expectations (~> 2.6.0)
+ rspec-mocks (~> 2.6.0)
+ rspec-core (2.6.4)
+ rspec-expectations (2.6.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.6.0)
+ sunspot (1.3.0.rc4)
+ escape (= 0.0.4)
+ pr_geohash (~> 1.0)
+ rsolr (= 1.0.2)
+ sunspot_rails (1.3.0.rc4)
+ nokogiri
+ sunspot (= 1.3.0.rc4)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ fakeweb
+ rspec (~> 2.5)
+ sunspot-rails-http-basic-auth!
View
3  lib/sunspot-rails-http-basic-auth.rb
@@ -1,7 +1,8 @@
require 'rsolr'
require 'sunspot/rails'
-require 'sunspot-rails-http-basic-auth/rsolr/connection/net_http'
+require 'sunspot-rails-http-basic-auth/rsolr/connection'
+require 'sunspot-rails-http-basic-auth/rsolr/client'
require 'sunspot-rails-http-basic-auth/sunspot/rails'
require 'sunspot-rails-http-basic-auth/sunspot/rails/configuration'
View
16 lib/sunspot-rails-http-basic-auth/rsolr/client.rb
@@ -0,0 +1,16 @@
+class RSolr::Client
+ alias_method :adapt_response_original, :adapt_response
+
+ def adapt_response request, response
+ if response[:status] == 401
+ result = response[:body]
+ result.extend Context
+ result.request = request
+ result.response = response
+ return result
+ else
+ adapt_response_original(request, response)
+ end
+ end
+
+end
View
12 lib/sunspot-rails-http-basic-auth/rsolr/connection.rb
@@ -0,0 +1,12 @@
+class RSolr::Connection
+ alias_method :setup_raw_request_original, :setup_raw_request
+
+ def setup_raw_request request_context
+ raw_request = setup_raw_request_original(request_context)
+ if request_context[:uri].user and request_context[:uri].password
+ raw_request.basic_auth(request_context[:uri].user, request_context[:uri].password)
+ end
+ raw_request
+ end
+
+end
View
84 lib/sunspot-rails-http-basic-auth/rsolr/connection/net_http.rb
@@ -1,42 +1,42 @@
-module RSolr
- module Connection
- class NetHttp
-
- def get(path, params={})
- url = build_url(path, params)
- net_http_response = basic_auth_get_request(url)
- create_http_context(net_http_response, url, path, params)
- end
-
- def post(path, data, params={}, headers={})
- url = build_url(path, params)
- net_http_response = basic_auth_post_request(url, data, headers)
- create_http_context(net_http_response, url, path, params, data, headers)
- end
-
- private
-
- def basic_auth_get_request(url)
- rails_logger "SOLR GET: #{url}"
- req = add_basic_auth(Net::HTTP::Get.new(url))
- connection.request(req)
- end
-
- def basic_auth_post_request(url, data, headers)
- rails_logger "SOLR POST: #{url}"
- req = add_basic_auth(Net::HTTP::Post.new(url, headers))
- connection.request(req, data)
- end
-
- def add_basic_auth(req)
- req.basic_auth(uri.user, uri.password) if uri.user and uri.password
- req
- end
-
- def rails_logger(text)
- Rails.logger.debug(text) if defined?(Rails)
- end
-
- end
- end
-end
+# module RSolr
+# module Connection
+# class NetHttp
+#
+# def get(path, params={})
+# url = build_url(path, params)
+# net_http_response = basic_auth_get_request(url)
+# create_http_context(net_http_response, url, path, params)
+# end
+#
+# def post(path, data, params={}, headers={})
+# url = build_url(path, params)
+# net_http_response = basic_auth_post_request(url, data, headers)
+# create_http_context(net_http_response, url, path, params, data, headers)
+# end
+#
+# private
+#
+# def basic_auth_get_request(url)
+# rails_logger "SOLR GET: #{url}"
+# req = add_basic_auth(Net::HTTP::Get.new(url))
+# connection.request(req)
+# end
+#
+# def basic_auth_post_request(url, data, headers)
+# rails_logger "SOLR POST: #{url}"
+# req = add_basic_auth(Net::HTTP::Post.new(url, headers))
+# connection.request(req, data)
+# end
+#
+# def add_basic_auth(req)
+# req.basic_auth(uri.user, uri.password) if uri.user and uri.password
+# req
+# end
+#
+# def rails_logger(text)
+# Rails.logger.debug(text) if defined?(Rails)
+# end
+#
+# end
+# end
+# end
View
14 lib/sunspot-rails-http-basic-auth/sunspot/rails/configuration.rb
@@ -1,23 +1,27 @@
module Sunspot
module Rails
class Configuration
-
+
+ # User used for basic http auth
def user
@user ||= user_configuration_from_key('solr', 'user')
end
-
+
+ # Master user used for basic http auth, in case, you have separate solr instances you read and write (master_solr) to.
def master_user
@master_user ||= user_configuration_from_key('master_solr', 'user')
end
-
+
+ # Password for basic http auth
def password
@password ||= user_configuration_from_key('solr', 'password')
end
-
+
+ # Master password for basic http auth
def master_password
@master_password ||= user_configuration_from_key('master_solr', 'password')
end
-
+
end
end
end
View
200 spec/rsolr/connection/net_http_spec.rb
@@ -1,100 +1,100 @@
-require 'spec_helper'
-require 'fakeweb'
-
-module RSolr
- module Connection
- describe NetHttp do
-
- describe '#get' do
- context 'with valid basic auth credentials' do
- before do
- FakeWeb.register_uri :get,
- 'http://user:pass@127.0.0.1:8983/solr/select?q=a',
- :body => 'Authorized'
- end
-
- let(:net_http) { described_class.new(:url => 'http://user:pass@127.0.0.1:8983/solr') }
-
- let(:result) { net_http.get('/select', :q => 'a') }
-
- specify { result[:data].should be_nil }
- specify { result[:body].should eq('Authorized') }
- specify { result[:status_code].should eq(200) }
- specify { result[:path].should eq('/select') }
- specify { result[:url].should eq('http://127.0.0.1:8983/solr/select?q=a') }
- specify { result[:headers].should be_empty }
- specify { result[:params].should eq({:q => 'a'}) }
- specify { result[:message].should eq('OK') }
- end
-
- context 'with invalid basic auth credentials' do
- before do
- FakeWeb.register_uri :get,
- 'http://user:pass@127.0.0.1:8983/solr/select?q=a',
- :body => 'Unauthorized',
- :status => [401, 'Unauthorized']
- end
-
- let(:net_http) { described_class.new(:url => 'http://user:pass@127.0.0.1:8983/solr') }
-
- let(:result) { net_http.get('/select', :q => 'a') }
-
- specify { result[:data].should be_nil }
- specify { result[:body].should eq('Unauthorized') }
- specify { result[:status_code].should eq(401) }
- specify { result[:path].should eq('/select') }
- specify { result[:url].should eq('http://127.0.0.1:8983/solr/select?q=a') }
- specify { result[:headers].should be_empty }
- specify { result[:params].should eq({:q => 'a'}) }
- specify { result[:message].should eq('Unauthorized') }
- end
- end
-
- describe '#post' do
- context 'with valid basic auth credentials' do
- before do
- FakeWeb.register_uri :post,
- 'http://user:pass@127.0.0.1:8983/solr/update',
- :body => 'Authorized'
- end
-
- let(:net_http) { described_class.new(:url => 'http://user:pass@127.0.0.1:8983/solr') }
-
- let(:result) { net_http.post('/update', '<rollback/>') }
-
- specify { result[:data].should eq('<rollback/>') }
- specify { result[:body].should eq('Authorized') }
- specify { result[:status_code].should eq(200) }
- specify { result[:path].should eq('/update') }
- specify { result[:url].should eq('http://127.0.0.1:8983/solr/update') }
- specify { result[:headers].should be_empty }
- specify { result[:params].should be_empty }
- specify { result[:message].should eq('OK') }
- end
-
- context 'with invalid basic auth credentials' do
- before do
- FakeWeb.register_uri :post,
- 'http://user:pass@127.0.0.1:8983/solr/update',
- :body => 'Unauthorized',
- :status => [401, 'Unauthorized']
- end
-
- let(:net_http) { described_class.new(:url => 'http://user:pass@127.0.0.1:8983/solr') }
-
- let(:result) { net_http.post('/update', '<rollback/>') }
-
- specify { result[:data].should eq('<rollback/>') }
- specify { result[:body].should eq('Unauthorized') }
- specify { result[:status_code].should eq(401) }
- specify { result[:path].should eq('/update') }
- specify { result[:url].should eq('http://127.0.0.1:8983/solr/update') }
- specify { result[:headers].should be_empty }
- specify { result[:params].should be_empty }
- specify { result[:message].should eq('Unauthorized') }
- end
- end
-
- end
- end
-end
+# require 'spec_helper'
+# require 'fakeweb'
+#
+# module RSolr
+# module Connection
+# describe NetHttp do
+#
+# describe '#get' do
+# context 'with valid basic auth credentials' do
+# before do
+# FakeWeb.register_uri :get,
+# 'http://user:pass@127.0.0.1:8983/solr/select?q=a',
+# :body => 'Authorized'
+# end
+#
+# let(:net_http) { described_class.new(:url => 'http://user:pass@127.0.0.1:8983/solr') }
+#
+# let(:result) { net_http.get('/select', :q => 'a') }
+#
+# specify { result[:data].should be_nil }
+# specify { result[:body].should eq('Authorized') }
+# specify { result[:status_code].should eq(200) }
+# specify { result[:path].should eq('/select') }
+# specify { result[:url].should eq('http://127.0.0.1:8983/solr/select?q=a') }
+# specify { result[:headers].should be_empty }
+# specify { result[:params].should eq({:q => 'a'}) }
+# specify { result[:message].should eq('OK') }
+# end
+#
+# context 'with invalid basic auth credentials' do
+# before do
+# FakeWeb.register_uri :get,
+# 'http://user:pass@127.0.0.1:8983/solr/select?q=a',
+# :body => 'Unauthorized',
+# :status => [401, 'Unauthorized']
+# end
+#
+# let(:net_http) { described_class.new(:url => 'http://user:pass@127.0.0.1:8983/solr') }
+#
+# let(:result) { net_http.get('/select', :q => 'a') }
+#
+# specify { result[:data].should be_nil }
+# specify { result[:body].should eq('Unauthorized') }
+# specify { result[:status_code].should eq(401) }
+# specify { result[:path].should eq('/select') }
+# specify { result[:url].should eq('http://127.0.0.1:8983/solr/select?q=a') }
+# specify { result[:headers].should be_empty }
+# specify { result[:params].should eq({:q => 'a'}) }
+# specify { result[:message].should eq('Unauthorized') }
+# end
+# end
+#
+# describe '#post' do
+# context 'with valid basic auth credentials' do
+# before do
+# FakeWeb.register_uri :post,
+# 'http://user:pass@127.0.0.1:8983/solr/update',
+# :body => 'Authorized'
+# end
+#
+# let(:net_http) { described_class.new(:url => 'http://user:pass@127.0.0.1:8983/solr') }
+#
+# let(:result) { net_http.post('/update', '<rollback/>') }
+#
+# specify { result[:data].should eq('<rollback/>') }
+# specify { result[:body].should eq('Authorized') }
+# specify { result[:status_code].should eq(200) }
+# specify { result[:path].should eq('/update') }
+# specify { result[:url].should eq('http://127.0.0.1:8983/solr/update') }
+# specify { result[:headers].should be_empty }
+# specify { result[:params].should be_empty }
+# specify { result[:message].should eq('OK') }
+# end
+#
+# context 'with invalid basic auth credentials' do
+# before do
+# FakeWeb.register_uri :post,
+# 'http://user:pass@127.0.0.1:8983/solr/update',
+# :body => 'Unauthorized',
+# :status => [401, 'Unauthorized']
+# end
+#
+# let(:net_http) { described_class.new(:url => 'http://user:pass@127.0.0.1:8983/solr') }
+#
+# let(:result) { net_http.post('/update', '<rollback/>') }
+#
+# specify { result[:data].should eq('<rollback/>') }
+# specify { result[:body].should eq('Unauthorized') }
+# specify { result[:status_code].should eq(401) }
+# specify { result[:path].should eq('/update') }
+# specify { result[:url].should eq('http://127.0.0.1:8983/solr/update') }
+# specify { result[:headers].should be_empty }
+# specify { result[:params].should be_empty }
+# specify { result[:message].should eq('Unauthorized') }
+# end
+# end
+#
+# end
+# end
+# end
View
86 spec/rsolr/connection_spec.rb
@@ -0,0 +1,86 @@
+require 'spec_helper'
+require 'fakeweb'
+
+describe RSolr::Connection do
+ describe '#get' do
+ context 'with valid basic auth credentials' do
+ before do
+ FakeWeb.register_uri :get,
+ 'http://user:pass@127.0.0.1:8983/solr/select?q=a&wt=ruby',
+ :body => "{'result' => 'Authorized'}"
+ end
+
+ let(:client) { RSolr::Client.new(RSolr::Connection.new, :url => 'http://user:pass@127.0.0.1:8983/solr') }
+ let(:result) { client.get('/select', :params => {:q => 'a'}) }
+
+ specify { result.response[:data].should be_nil }
+ specify { result.response[:body].should eq("{'result' => 'Authorized'}") }
+ specify { result.response[:status].should eq(200) }
+ specify { result.request[:path].should eq('/select') }
+ specify { result.request[:uri].to_s.should eq('http://user:pass@127.0.0.1:8983/solr/select?q=a&wt=ruby') }
+ specify { result.response[:headers].should be_empty }
+ specify { result.request[:params].should eq({:q => 'a', :wt => :ruby}) }
+ end
+
+ context 'with invalid basic auth credentials' do
+ before do
+ FakeWeb.register_uri :get,
+ 'http://user:pass@127.0.0.1:8983/solr/select?q=a&wt=ruby',
+ :body => 'Unauthorized',
+ :status => [401, 'Unauthorized']
+ end
+
+ let(:client) { RSolr::Client.new(RSolr::Connection.new, :url => 'http://user:pass@127.0.0.1:8983/solr') }
+ let(:result) { client.get('/select', :params => {:q => 'a'}) }
+
+ specify { result.response[:data].should be_nil }
+ specify { result.response[:body].should eq('Unauthorized') }
+ specify { result.response[:status].should eq(401) }
+ specify { result.request[:path].should eq('/select') }
+ specify { result.request[:uri].to_s.should eq('http://user:pass@127.0.0.1:8983/solr/select?q=a&wt=ruby') }
+ specify { result.response[:headers].should be_empty }
+ specify { result.request[:params].should eq({:q => 'a', :wt => :ruby}) }
+ end
+ end
+
+ describe '#post' do
+ context 'with valid basic auth credentials' do
+ before do
+ FakeWeb.register_uri :post,
+ 'http://user:pass@127.0.0.1:8983/solr/update?wt=ruby',
+ :body => "{'result' => 'Authorized'}"
+ end
+
+ let(:client) { RSolr::Client.new(RSolr::Connection.new, :url => 'http://user:pass@127.0.0.1:8983/solr') }
+ let(:result) { client.post('/update', :data => '<rollback/>') }
+
+ specify { result.request[:data].should eq('<rollback/>') }
+ specify { result.response[:body].should eq("{'result' => 'Authorized'}") }
+ specify { result.response[:status].should eq(200) }
+ specify { result.request[:path].should eq('/update') }
+ specify { result.request[:uri].to_s.should eq('http://user:pass@127.0.0.1:8983/solr/update?wt=ruby') }
+ specify { result.response[:headers].should be_empty }
+ specify { result.request[:params].should eq({:wt => :ruby}) }
+ end
+
+ context 'with invalid basic auth credentials' do
+ before do
+ FakeWeb.register_uri :post,
+ 'http://user:pass@127.0.0.1:8983/solr/update?wt=ruby',
+ :body => 'Unauthorized',
+ :status => [401, 'Unauthorized']
+ end
+
+ let(:client) { RSolr::Client.new(RSolr::Connection.new, :url => 'http://user:pass@127.0.0.1:8983/solr') }
+ let(:result) { client.post('/update', :data => '<rollback/>') }
+
+ specify { result.request[:data].should eq('<rollback/>') }
+ specify { result.response[:body].should eq('Unauthorized') }
+ specify { result.response[:status].should eq(401) }
+ specify { result.request[:path].should eq('/update') }
+ specify { result.request[:uri].to_s.should eq('http://user:pass@127.0.0.1:8983/solr/update?wt=ruby') }
+ specify { result.response[:headers].should be_empty }
+ specify { result.request[:params].should eq({:wt => :ruby}) }
+ end
+ end
+end
View
2  sunspot-rails-http-basic-auth.gemspec
@@ -16,7 +16,7 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_path = 'lib'
- s.add_dependency 'sunspot_rails', '~> 1.2.1'
+ s.add_dependency 'sunspot_rails', '~> 1.3.0'
s.add_development_dependency 'rspec', '~> 2.5'
s.add_development_dependency 'fakeweb'
Something went wrong with that request. Please try again.