Sha256: b0149b147ab705069db2435bca483212bd2f0a500966fa42f3dc0ed515657940

Contents?: true

Size: 1.27 KB

Versions: 6

Compression:

Stored size: 1.27 KB

Contents

require 'active_support/core_ext/hash/indifferent_access'

module Waistband
  class Query

    attr_accessor :page, :page_size

    def initialize(index, options = {})
      @index      = index
      @page       = (options[:page] || 1).to_i
      @page_size  = (options[:page_size] || 20).to_i
      prepare
    end

    def prepare(hash = {})
      @hash = hash.with_indifferent_access
      self
    end

    def paginated_results
      return Kaminari.paginate_array(results, total_count: total_results).page(@page).per(@page_size) if defined?(Kaminari)
      raise "Please include the `kaminari` gem to use this method!"
    end

    def results
      hits.map do |hit|
        Waistband::QueryResult.new(hit)
      end
    end

    def hits
      execute!['hits']['hits'] rescue []
    end

    def total_results
      execute!['hits']['total'] rescue 0
    end

    private

      def to_hash
        @hash[:from] = from       unless @hash[:from]
        @hash[:size] = @page_size unless @hash[:size]

        @hash
      end

      def url
        @index.search_url
      end

      def execute!
        JSON.parse(RestClient::Request.execute(method: :get, url: url, payload: to_hash.to_json))
      end

      def from
        @page_size * (@page - 1)
      end

    # /private

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
waistband-0.8.5 lib/waistband/query.rb
waistband-0.8.4 lib/waistband/query.rb
waistband-0.8.3 lib/waistband/query.rb
waistband-0.8.2 lib/waistband/query.rb
waistband-0.8.1 lib/waistband/query.rb
waistband-0.8.0 lib/waistband/query.rb