Sha256: 64da82195cdae19c287c9ed6ea4315e52fecaf829ac500c5af4a150d5a7e28f2

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

require "httparty"
require "active_support/core_ext/object/to_json"

module ActiveSearch
  module Algolia
    class Client
      include HTTParty

      def self.configure(api_key, app_id, index = "activesearch")
        base_uri "https://#{app_id}.algolia.io/1/indexes/#{index}"
        headers({
          "X-Algolia-API-Key" => api_key,
          "X-Algolia-Application-Id" => app_id,
          "Content-Type" => "application/json; charset=utf-8"
        })
      end

      def delete_index
        self.class.delete("")
      end

      def delete(id)
        return false if id.nil?
        self.class.delete("/#{id}")
      end

      def save(id, object)
        self.class.put("/#{id}", body: object.to_json)
      end

      def query(text, extras = {}, options = {})
        page, per_page = options[:page] || 0, options[:per_page] || 20

        self.class.get('', query: extras.merge!(
          query:        text,
          page:         page,
          hitsPerPage:  per_page
        ))
      end

      def get(id)
        self.class.get("/#{id}")
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activesearch-0.3.1 lib/activesearch/algolia/client.rb