used to - add - delete - re-map - search email mappings
Signed-off-by: Luan Shengde shdluan@163.com --- lib/email_mapping_client.rb | 45 +++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100755 lib/email_mapping_client.rb
diff --git a/lib/email_mapping_client.rb b/lib/email_mapping_client.rb new file mode 100755 index 0000000..ec82d93 --- /dev/null +++ b/lib/email_mapping_client.rb @@ -0,0 +1,45 @@ +#!/usr/bin/env ruby +# SPDX-License-Identifier: MulanPSL-2.0+ +# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved. +# frozen_string_literal: true + +require 'redis' +require 'json' + +# used to add/delete/re-map/search email mappings +class AddEmailMapping + REDIS_HOST = ENV['REDIS_HOST'] || 'localhost' + REDIS_PORT = ENV['REDIS_PORT'] || '6379' + + def initialize(r_name, d_email) + @r_name = r_name + @d_email = d_email + @redis = Redis.new('host' => REDIS_HOST, 'port' => REDIS_PORT) + end + + def add_email_mapping + if @redis.hexists 'email_mapping', @r_name + puts "#{@r_name} has already mapped an email." + else + @redis.hset 'email_mapping', @r_name, @d_email + puts "Successfully mapped email for #{@r_name}." + end + end + + def delete_email_mapping + @redis.hdel 'email_mapping', @r_name + end + + def re_map_email + @redis.hset 'email_mapping', @r_name, @d_email + puts "Successfully re-mapped email for #{@r_name}." + end + + def search_map + d_email = @redis.hget 'email_mapping', @r_name + + return d_email if d_email + + return "#{@r_name} has not mapped an email yet." + end +end