Class: Refract

Inherits:
Object
  • Object
show all
Defined in:
lib/refract.rb

Class Method Summary collapse

Class Method Details

.search(json, query) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/refract.rb', line 2

def self.search(json, query)
  fail(ArgumentError, 'Expected hash as the first argument') unless json.is_a?(Hash)
  fail(ArgumentError, 'Expected hash as the second argument') unless query.is_a?(Hash)

  return [] unless json['content']
  return [] unless json['content'].is_a?(Array)

  result = json['content'].select do |element|
    query.map do |k, v|
      # TODO: deep search
      element[k] == v
    end.all?
  end

  json['content'].map do |element|
    search(element, query)
  end.flatten.concat(result)
end