Sha256: b8d3060d7b8a50358f00addee9963be96e2a09a7344e7343ce4eea30c2f6f4d0

Contents?: true

Size: 1.07 KB

Versions: 3

Compression:

Stored size: 1.07 KB

Contents

require 'excon'

module Inquisitio
  class Indexer

    def self.index(documents)
      new(documents).index
    end

    def initialize(documents)
      raise InquisitioError.new("Document(s) is null") if documents.nil?
      raise InquisitioError.new("Document(s) is empty") unless documents.any?

      @documents = documents
    end

    def index
      Inquisitio.config.logger.info "Indexer posting to #{batch_index_url} body: #{body}"
      response = Excon.post(batch_index_url,
                           :body => body,
                           :headers => {"Content-Type" =>"application/json"})
      Inquisitio.config.logger.info "Response - status: #{response.status} body: #{response.body}"
      raise InquisitioError.new("Index failed with status code: #{response.status} Message: #{response.body}") unless response.status == 200
      response.body
    end

    private

    def body
      body = @documents.map(&:to_SDF).join(", ")
      "[#{body}]"
    end

    def batch_index_url
      "#{Inquisitio.config.document_endpoint}/2011-02-01/documents/batch"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
inquisitio-0.1.6 lib/inquisitio/indexer.rb
inquisitio-0.1.5 lib/inquisitio/indexer.rb
inquisitio-0.1.4 lib/inquisitio/indexer.rb