Class: Dina::SearchConnection
- Inherits:
-
Object
- Object
- Dina::SearchConnection
- Defined in:
- lib/dina/search/search_connection.rb
Instance Method Summary collapse
- #custom_headers ⇒ Object
-
#index_name(index:) ⇒ String
Sets the search index name.
-
#initialize(options = {}) {|_self| ... } ⇒ SearchConnection
constructor
A new instance of SearchConnection.
- #run(request_method, path, params: nil, headers: {}, body: nil) ⇒ Object
- #use(middleware, *args, &block) ⇒ Object
Constructor Details
#initialize(options = {}) {|_self| ... } ⇒ SearchConnection
Returns a new instance of SearchConnection.
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/dina/search/search_connection.rb', line 6 def initialize( = {}) site = .fetch(:site) = .slice(:proxy, :ssl, :request, :headers, :params) = Array(.fetch(:adapter, Faraday.default_adapter)) @faraday = Faraday.new(site, ) do |builder| builder.request :json builder.response :json builder.adapter(*) end yield(self) if block_given? end |
Instance Method Details
#custom_headers ⇒ Object
29 30 31 |
# File 'lib/dina/search/search_connection.rb', line 29 def custom_headers { content_type: "application/json", authorization: Dina.header } end |
#index_name(index:) ⇒ String
Sets the search index name
24 25 26 27 |
# File 'lib/dina/search/search_connection.rb', line 24 def index_name(index:) return nil if !index "dina_#{index}_index" end |
#run(request_method, path, params: nil, headers: {}, body: nil) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/dina/search/search_connection.rb', line 33 def run(request_method, path, params: nil, headers: {}, body: nil) path.slice!("/execute") if params && params.has_key?(:index) params.merge!(indexName: index_name(index: params[:index])) elsif body && body.has_key?(:index) params = { indexName: index_name(index: body[:index]) } end payload = JSON.generate(body[:payload]) rescue "" response = @faraday.run_request(request_method, path, body, headers) do |request| request.params.update(params) if params request.headers = custom_headers request.body = payload end attributes = response.body.dup = {} if attributes.has_key?("hits") #TODO: does not work with SearchAutoComplete because response is different from Search ["count"] = attributes["hits"]["total"]["value"] rescue 0 elsif attributes.has_key?("count") ["count"] = attributes["count"] elsif attributes.has_key?("attributes") = attributes end response.body["meta"] = response.body["errors"] = [] response.body["data"] = attributes["hits"]["hits"].map{|d| d["_source"]["data"]} rescue [] response end |
#use(middleware, *args, &block) ⇒ Object
64 65 66 |
# File 'lib/dina/search/search_connection.rb', line 64 def use(middleware, *args, &block) return if @faraday.builder.locked? end |