lib/inquisitio/searcher.rb in inquisitio-0.1.7 vs lib/inquisitio/searcher.rb in inquisitio-0.2.0

- old
+ new

@@ -16,10 +16,11 @@ per: 10, page: 1, returns: [], with: {} } + @failed_attempts = 0 yield(self) if block_given? end def search @@ -31,11 +32,11 @@ end def records @records ||= begin klasses = {} - map do |result| + results.map do |result| klass = result['data']['med_type'].first klasses[klass] ||= [] klasses[klass] << result['data']['med_id'].first end @@ -100,23 +101,29 @@ end private def results - if @results.nil? - Inquisitio.config.logger.info("Performing search: #{search_url}") - response = Excon.get(search_url) - raise InquisitioError.new("Search failed with status code: #{response.status} Message #{response.body}") unless response.status == 200 - body = JSON.parse(response.body) - @results = Results.new(body["hits"]["hit"], - params[:page], - params[:per], - body["hits"]["found"]) - end - @results + return @results unless @results.nil? + + Inquisitio.config.logger.info("Performing search: #{search_url}") + response = Excon.get(search_url) + raise InquisitioError.new("Search failed with status code: #{response.status} Message #{response.body}") unless response.status == 200 + body = JSON.parse(response.body) + @results = Results.new(body["hits"]["hit"], + params[:page], + params[:per], + body["hits"]["found"]) rescue => e - Inquisitio.config.logger.error("Exception Performing search: #{search_url} #{e}") + @failed_attempts += 1 + Inquisitio.config.logger.error("Exception Performing search: #{search_url} #{e}") + + if @failed_attempts < Inquisitio.config.max_attempts + Inquisitio.config.logger.error("Retrying search #{@failed_attempts}/#{Inquisitio.config.max_attempts}") + results + else raise InquisitioError.new("Exception performing search") + end end def search_url @search_url ||= begin return_fields = params[:returns].empty?? [:med_type, :med_id] : params[:returns]