Sha256: d13475646a69feba9ad6d44b7b5fd7139db1153f7037b58eead0bbdbc242b613

Contents?: true

Size: 1.8 KB

Versions: 5

Compression:

Stored size: 1.8 KB

Contents

require 'multi_json'

module Helpers
  # Convert an Hash to json
  #
  def to_json(body)
    body.is_a?(String) ? body : MultiJson.dump(body)
  end

  # Converts each key of a hash to symbols
  #
  def symbolize_hash(hash)
    hash.each_with_object({}) { |(k, v), h| h[k.to_sym] = v }
  end

  # Convert params to a full query string
  #
  def handle_params(params)
    params.nil? || params.empty? ? '' : "?#{to_query_string(params)}"
  end

  # Create a query string from params
  #
  def to_query_string(params)
    params.map do |key, value|
      "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}"
    end.join('&')
  end

  # Convert a json object to an hash
  #
  def json_to_hash(json, symbolize_keys)
    MultiJson.load(json, symbolize_keys: symbolize_keys)
  end

  # Retrieve the given value associated with a key, in string or symbol format
  #
  def get_option(hash, key)
    hash[key.to_sym] || hash[key] || nil
  end

  # Build a path with the given arguments
  #
  def path_encode(path, *args)
    arguments = []
    args.each do |arg|
      arguments.push(CGI.escape(CGI.unescape(arg.to_s)))
    end

    format(path, *arguments)
  end

  # Support to convert old settings to their new names
  #
  def deserialize_settings(data)
    settings = symbolize_hash(data)
    keys     = {
      attributesToIndex: 'searchableAttributes',
      numericAttributesToIndex: 'numericAttributesForFiltering',
      slaves: 'replicas'
    }

    keys.each do |deprecated_key, current_key|
      if settings.has_key?(deprecated_key)
        settings[current_key.to_sym] = settings.delete(deprecated_key)
      end
    end

    settings
  end

  def self.included(base)
    base.extend(Helpers)
  end

  def hash_includes_subset?(hash, subset)
    res = true
    subset.each do |k, v|
      res &&= hash[k] == v
    end
    res
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
algolia-2.0.0.pre.beta.2 lib/algolia/helpers.rb
algolia-2.0.0.pre.beta.1 lib/algolia/helpers.rb
algolia-2.0.0.pre.alpha.4 lib/algolia/helpers.rb
algolia-2.0.0.pre.alpha.3 lib/algolia/helpers.rb
algolia-2.0.0.pre.alpha.2 lib/algolia/helpers.rb