Module: Sctechnicalchallenge

Defined in:
lib/sctechnicalchallenge.rb,
lib/sctechnicalchallenge/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.1.0"
@@clients_cache =
{}

Class Method Summary collapse

Class Method Details

.find_duplicate_clients(file: "clients.json") ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/sctechnicalchallenge.rb', line 26

def self.find_duplicate_clients(file: "clients.json")
  clients = load_clients(file)

  grouped_by_email = clients.group_by { |client| client["email"] }
  duplicates = grouped_by_email.select { |email, clients| clients.size > 1 }

  duplicates.values.flatten
end

.load_clients(path_to_file) ⇒ Object



35
36
37
# File 'lib/sctechnicalchallenge.rb', line 35

def self.load_clients(path_to_file)
  @@clients_cache[path_to_file] ||= JSON.parse(File.read(File.expand_path(path_to_file || "clients.json")))
end

.search(query:, field: "full_name", file: "clients.json") ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/sctechnicalchallenge.rb', line 11

def self.search(query:, field: "full_name", file: "clients.json")
  field ||= "full_name" # in case field value is nil

  clients = load_clients(file)

  search_strategies = {
    "id" => ->(client, q) { client["id"] == q.to_i },
    "default" => ->(client, q) { client[field].downcase.include?(query.downcase) }
  }

  search_strategy = search_strategies[field] || search_strategies["default"]

  clients.select { |client| search_strategy.call(client, query) }
end