Skip to content

Instantly share code, notes, and snippets.

@makuk66
Created November 22, 2011 20:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makuk66/1386855 to your computer and use it in GitHub Desktop.
Save makuk66/1386855 to your computer and use it in GitHub Desktop.
a simple Rsolr 1.0.3 example for basic auth
#!/usr/bin/env ruby
#
# A simple Rsolr 1.0.3 example for basic auth
#
# rvm gemset create rsolr-example
# rvm use rsolr-example
# gem install rsolr
require 'rsolr'
# patch basic auth support into RSolr, until it is supported upstream
require 'rsolr/connection'
class RSolr::Connection
protected
def setup_raw_request_with_basic_auth request_context
raw_request = setup_raw_request_without_basic_auth(request_context)
raw_request.basic_auth(request_context[:uri].user, request_context[:uri].password)
raw_request
end
alias_method :setup_raw_request_without_basic_auth, :setup_raw_request
alias_method :setup_raw_request, :setup_raw_request_with_basic_auth
end
rsolr = RSolr.connect :url => 'http://USERNAME:PASSWORD@HOSTNAME/ROUTEID/solr/collection1'
rsolr.add(:id => 1, :name => "some title", :body => "some body")
rsolr.commit
result = rsolr.select :params => { :q => "some" }
p result
@hammady
Copy link

hammady commented Dec 14, 2016

Thanks for the gist. For anyone coming here through search engines, RSolr now has basic auth built-in. Just specify username and password in the url as shown here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment