Module: SageWorld::Api::FindHelper::InstanceMethods

Defined in:
lib/sage_world/api/concerns/find_helper.rb

Instance Method Summary collapse

Instance Method Details

#find_in_hash(lookup_key, haystack = body) ⇒ Object

SageWorld Api provides helper method on SageWorld::ResponseHandler which can return the key in nested hash or array and return its value.

e.g response = SageWorld::Api::Product.search(“mugs”) Find in hash provides an easy way to do response.body[:search_results][:item] => simplifies to response.find_in_hash(“Item”) => returns array of items.



14
15
16
17
18
19
20
21
22
# File 'lib/sage_world/api/concerns/find_helper.rb', line 14

def find_in_hash(lookup_key, haystack = body)
  if haystack.respond_to?(:key?) && haystack.key?(lookup_key)
    haystack[lookup_key]
  elsif haystack.respond_to?(:each)
    data = nil
    haystack.find{ |*nested_haystack| data = find_in_hash(lookup_key, nested_haystack.last) }
    data
  end
end