Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to support Sunspot 1.3.0 #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*.gem
.bundle
Gemfile.lock
pkg/*
vendor
40 changes: 40 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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!
3 changes: 2 additions & 1 deletion lib/sunspot-rails-http-basic-auth.rb
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
16 changes: 16 additions & 0 deletions lib/sunspot-rails-http-basic-auth/rsolr/client.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions lib/sunspot-rails-http-basic-auth/rsolr/connection.rb
Original file line number Diff line number Diff line change
@@ -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
84 changes: 42 additions & 42 deletions lib/sunspot-rails-http-basic-auth/rsolr/connection/net_http.rb
Original file line number Diff line number Diff line change
@@ -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
14 changes: 9 additions & 5 deletions lib/sunspot-rails-http-basic-auth/sunspot/rails/configuration.rb
Original file line number Diff line number Diff line change
@@ -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
Loading