Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: move Helpers into Algolia namespace #492

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
224 changes: 113 additions & 111 deletions lib/algolia/helpers.rb
Original file line number Diff line number Diff line change
@@ -1,134 +1,136 @@
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
module Algolia
module Helpers
# Convert an Hash to json
#
def to_json(body)
body.is_a?(String) ? body : MultiJson.dump(body)
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
# 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 a json object to an hash
#
def json_to_hash(json, symbolize_keys)
MultiJson.load(json, symbolize_keys: symbolize_keys)
end
# Convert params to a full query string
#
def handle_params(params)
params.nil? || params.empty? ? '' : "?#{to_query_string(params)}"
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
# 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

# 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)))
# Convert a json object to an hash
#
def json_to_hash(json, symbolize_keys)
MultiJson.load(json, symbolize_keys: symbolize_keys)
end

format(path, *arguments)
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

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

keys.each do |deprecated_key, current_key|
deprecated_key = symbolize_keys ? deprecated_key : deprecated_key.to_s
if settings.has_key?(deprecated_key)
key = symbolize_keys ? current_key.to_sym : current_key.to_s
settings[key] = settings.delete(deprecated_key)
# 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

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

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

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

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

# Check the passed object to determine if it's an array
#
# @param object [Object]
#
def check_array(object)
raise Algolia::AlgoliaError, 'argument must be an array of objects' unless object.is_a?(Array)
end
def hash_includes_subset?(hash, subset)
res = true
subset.each do |k, v|
res &&= hash[k] == v
end
res
end

# Check the passed object
#
# @param object [Object]
# @param in_array [Boolean] whether the object is an array or not
#
def check_object(object, in_array = false)
case object
when Array
raise Algolia::AlgoliaError, in_array ? 'argument must be an array of objects' : 'argument must not be an array'
when String, Integer, Float, TrueClass, FalseClass, NilClass
raise Algolia::AlgoliaError, "argument must be an #{'array of' if in_array} object, got: #{object.inspect}"
# Check the passed object to determine if it's an array
#
# @param object [Object]
#
def check_array(object)
raise Algolia::AlgoliaError, 'argument must be an array of objects' unless object.is_a?(Array)
end
end

# Check if passed object has a objectID
#
# @param object [Object]
# @param object_id [String]
#
def get_object_id(object, object_id = nil)
check_object(object)
object_id ||= object[:objectID] || object['objectID']
raise Algolia::AlgoliaError, "Missing 'objectID'" if object_id.nil?
object_id
end
# Check the passed object
#
# @param object [Object]
# @param in_array [Boolean] whether the object is an array or not
#
def check_object(object, in_array = false)
case object
when Array
raise Algolia::AlgoliaError, in_array ? 'argument must be an array of objects' : 'argument must not be an array'
when String, Integer, Float, TrueClass, FalseClass, NilClass
raise Algolia::AlgoliaError, "argument must be an #{'array of' if in_array} object, got: #{object.inspect}"
end
end

# Build a batch request
#
# @param action [String] action to perform on the engine
# @param objects [Array] objects on which build the action
# @param with_object_id [Boolean] if set to true, check if each object has an objectID set
#
def chunk(action, objects, with_object_id = false)
objects.map do |object|
check_object(object, true)
request = { action: action, body: object }
request[:objectID] = get_object_id(object).to_s if with_object_id
request
# Check if passed object has a objectID
#
# @param object [Object]
# @param object_id [String]
#
def get_object_id(object, object_id = nil)
check_object(object)
object_id ||= object[:objectID] || object['objectID']
raise Algolia::AlgoliaError, "Missing 'objectID'" if object_id.nil?
object_id
end

# Build a batch request
#
# @param action [String] action to perform on the engine
# @param objects [Array] objects on which build the action
# @param with_object_id [Boolean] if set to true, check if each object has an objectID set
#
def chunk(action, objects, with_object_id = false)
objects.map do |object|
check_object(object, true)
request = { action: action, body: object }
request[:objectID] = get_object_id(object).to_s if with_object_id
request
end
end
end
end
22 changes: 12 additions & 10 deletions sig/helpers.rbs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
module Helpers
module Algolia
module Helpers

def to_json: (String|Hash[Symbol, String] body) -> String
def symbolize_hash: (Hash[Symbol|String, untyped] hash) -> Hash[Symbol, untyped]
def handle_params: (Hash[Symbol, String] params) -> String
def to_query_string: (Hash[Symbol, String] params) -> String
def json_to_hash: (String json, bool symbolize_keys) -> Hash[Symbol, String]
def get_option: ([Symbol|String, String] hash, String key) -> (String | [String])
def path_encode: (String path, *String args) -> String
def deserialize_settings: (Hash[Symbol|String, String] data) -> Hash[Symbol, String]
def self.included: (self base) -> self
def to_json: (String|Hash[Symbol, String] body) -> String
def symbolize_hash: (Hash[Symbol|String, untyped] hash) -> Hash[Symbol, untyped]
def handle_params: (Hash[Symbol, String] params) -> String
def to_query_string: (Hash[Symbol, String] params) -> String
def json_to_hash: (String json, bool symbolize_keys) -> Hash[Symbol, String]
def get_option: ([Symbol|String, String] hash, String key) -> (String | [String])
def path_encode: (String path, *String args) -> String
def deserialize_settings: (Hash[Symbol|String, String] data) -> Hash[Symbol, String]
def self.included: (self base) -> self
end
end
2 changes: 1 addition & 1 deletion test/algolia/unit/helpers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'test_helper'

class HelpersTest
include Helpers
include Algolia::Helpers

describe 'test helpers' do
def test_deserialize_settings
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Minitest::Test
attr_reader :search_client

include Minitest::Hooks
include Helpers
include Algolia::Helpers
@@search_config = Algolia::Search::Config.new(application_id: APPLICATION_ID_1, api_key: ADMIN_KEY_1, user_agent: USER_AGENT)
@@search_client = Algolia::Search::Client.new(@@search_config)
end
Expand Down