Skip to content

Commit f7009fc

Browse files
authored
chore: move methods to helpers (#446)
1 parent 94d1d90 commit f7009fc

File tree

2 files changed

+49
-49
lines changed

2 files changed

+49
-49
lines changed

lib/algolia/helpers.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,53 @@ def hash_includes_subset?(hash, subset)
8282
end
8383
res
8484
end
85+
86+
# Check the passed object to determine if it's an array
87+
#
88+
# @param object [Object]
89+
#
90+
def check_array(object)
91+
raise Algolia::AlgoliaError, 'argument must be an array of objects' unless object.is_a?(Array)
92+
end
93+
94+
# Check the passed object
95+
#
96+
# @param object [Object]
97+
# @param in_array [Boolean] whether the object is an array or not
98+
#
99+
def check_object(object, in_array = false)
100+
case object
101+
when Array
102+
raise Algolia::AlgoliaError, in_array ? 'argument must be an array of objects' : 'argument must not be an array'
103+
when String, Integer, Float, TrueClass, FalseClass, NilClass
104+
raise Algolia::AlgoliaError, "argument must be an #{'array of' if in_array} object, got: #{object.inspect}"
105+
end
106+
end
107+
108+
# Check if passed object has a objectID
109+
#
110+
# @param object [Object]
111+
# @param object_id [String]
112+
#
113+
def get_object_id(object, object_id = nil)
114+
check_object(object)
115+
object_id ||= object[:objectID] || object['objectID']
116+
raise Algolia::AlgoliaError, "Missing 'objectID'" if object_id.nil?
117+
object_id
118+
end
119+
120+
# Build a batch request
121+
#
122+
# @param action [String] action to perform on the engine
123+
# @param objects [Array] objects on which build the action
124+
# @param with_object_id [Boolean] if set to true, check if each object has an objectID set
125+
#
126+
def chunk(action, objects, with_object_id = false)
127+
objects.map do |object|
128+
check_object(object, true)
129+
request = { action: action, body: object }
130+
request[:objectID] = get_object_id(object).to_s if with_object_id
131+
request
132+
end
133+
end
85134
end

lib/algolia/search_index.rb

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,55 +1037,6 @@ def exists
10371037

10381038
private
10391039

1040-
# Check the passed object to determine if it's an array
1041-
#
1042-
# @param object [Object]
1043-
#
1044-
def check_array(object)
1045-
raise AlgoliaError, 'argument must be an array of objects' unless object.is_a?(Array)
1046-
end
1047-
1048-
# Check the passed object
1049-
#
1050-
# @param object [Object]
1051-
# @param in_array [Boolean] whether the object is an array or not
1052-
#
1053-
def check_object(object, in_array = false)
1054-
case object
1055-
when Array
1056-
raise AlgoliaError, in_array ? 'argument must be an array of objects' : 'argument must not be an array'
1057-
when String, Integer, Float, TrueClass, FalseClass, NilClass
1058-
raise AlgoliaError, "argument must be an #{'array of' if in_array} object, got: #{object.inspect}"
1059-
end
1060-
end
1061-
1062-
# Check if passed object has a objectID
1063-
#
1064-
# @param object [Object]
1065-
# @param object_id [String]
1066-
#
1067-
def get_object_id(object, object_id = nil)
1068-
check_object(object)
1069-
object_id ||= object[:objectID] || object['objectID']
1070-
raise AlgoliaError, "Missing 'objectID'" if object_id.nil?
1071-
object_id
1072-
end
1073-
1074-
# Build a batch request
1075-
#
1076-
# @param action [String] action to perform on the engine
1077-
# @param objects [Array] objects on which build the action
1078-
# @param with_object_id [Boolean] if set to true, check if each object has an objectID set
1079-
#
1080-
def chunk(action, objects, with_object_id = false)
1081-
objects.map do |object|
1082-
check_object(object, true)
1083-
request = { action: action, body: object }
1084-
request[:objectID] = get_object_id(object).to_s if with_object_id
1085-
request
1086-
end
1087-
end
1088-
10891040
def raw_batch(requests, opts)
10901041
@transporter.write(:POST, path_encode('/1/indexes/%s/batch', @name), { requests: requests }, opts)
10911042
end

0 commit comments

Comments
 (0)