Class: Solr::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/solr/connection.rb

Overview

low-level connection that can do network requests to Solr

Constant Summary collapse

INSTRUMENT_KEY =
'request.solrb'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, faraday_options: Solr.configuration.faraday_options) ⇒ Connection

Returns a new instance of Connection.



6
7
8
9
10
# File 'lib/solr/connection.rb', line 6

def initialize(url, faraday_options: Solr.configuration.faraday_options)
  # Allow mock the connection for testing
  @raw_connection = Solr.configuration.test_connection || build_faraday_connection(url, faraday_options)
  freeze
end

Class Method Details

.call(url:, method:, body:) ⇒ Object



12
13
14
15
# File 'lib/solr/connection.rb', line 12

def self.call(url:, method:, body:)
  raise "HTTP method not supported: #{method}" unless [:get, :post].include?(method.to_sym)
  new(url).public_send(method, body)
end

Instance Method Details

#get(_) ⇒ Object



17
18
19
# File 'lib/solr/connection.rb', line 17

def get(_)
  Solr.instrument(name: INSTRUMENT_KEY) { @raw_connection.get }
end

#post(data) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/solr/connection.rb', line 21

def post(data)
  Solr.instrument(name: INSTRUMENT_KEY, data: data) do
    @raw_connection.post do |req|
      req.headers['Content-Type'] = 'application/json'.freeze
      req.body = JSON.generate(data)
    end
  end
end