Class: Dina::SearchConnection
- Inherits:
-
Object
- Object
- Dina::SearchConnection
- Defined in:
- lib/dina/search/search_connection.rb
Instance Attribute Summary collapse
-
#faraday ⇒ Object
readonly
Returns the value of attribute faraday.
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.
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/dina/search/search_connection.rb', line 8 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 Attribute Details
#faraday ⇒ Object (readonly)
Returns the value of attribute faraday.
6 7 8 |
# File 'lib/dina/search/search_connection.rb', line 6 def faraday @faraday end |
Instance Method Details
#custom_headers ⇒ Object
31 32 33 |
# File 'lib/dina/search/search_connection.rb', line 31 def custom_headers { content_type: "application/json", authorization: Dina.header } end |
#index_name(index:) ⇒ String
Sets the search index name
26 27 28 29 |
# File 'lib/dina/search/search_connection.rb', line 26 def index_name(index:) return nil if !index "dina_#{index}_index" end |
#run(request_method, path, params: nil, headers: {}, body: nil) ⇒ Object
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 63 64 |
# File 'lib/dina/search/search_connection.rb', line 35 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
66 67 68 |
# File 'lib/dina/search/search_connection.rb', line 66 def use(middleware, *args, &block) return if faraday.builder.locked? end |