Module: Five9Tools::Helpers

Defined in:
lib/five9tools/helpers.rb

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Class Method Details

.append_always(obj, elem, appendation) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/five9tools/helpers.rb', line 7

def append_always(obj, elem, appendation)
  if obj.has_key? elem
    obj[elem] << appendation
  else
    obj[elem] = [appendation]
  end
end

.arr_of_hashes_to_csv(arr_of_hashes) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/five9tools/helpers.rb', line 93

def arr_of_hashes_to_csv(arr_of_hashes)
  CSV.open("test.csv", "w") do |csv|
    csv << ["username", "skills"]
    arr_of_hashes.each do |ob|
      csv << [ob.keys, ob.values].flatten
    end
  end
end

.csv_to_hash_arr(csv_file_name) ⇒ Object



41
42
43
# File 'lib/five9tools/helpers.rb', line 41

def csv_to_hash_arr(csv_file_name)
  CSV.open(csv_file_name, headers: :first_row, header_converters: :symbol).map(&:to_h)
end

.csv_to_json(text) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/five9tools/helpers.rb', line 53

def csv_to_json(text)
  csv_arr = CSV.parse(text)
  arr_of_data = csv_arr[1..-1].map { |r|
    csv_arr[0].map.each_with_index { |header, i|
      [header, r[i]]
    }.to_h
  }.to_json
end

.decode_ivr_script_function(encoded_text) ⇒ Object



102
103
104
105
# File 'lib/five9tools/helpers.rb', line 102

def decode_ivr_script_function(encoded_text)
  dec = Base64.decode64(encoded_text)
  gunzip(dec)
end

.encode_ivr_script_function(text) ⇒ Object



107
108
109
110
# File 'lib/five9tools/helpers.rb', line 107

def encode_ivr_script_function(text)
  gzipped = gzip(text)
  Base64.encode64(gzipped)
end

.get_files_recursively(directory_path) ⇒ Object



15
16
17
18
# File 'lib/five9tools/helpers.rb', line 15

def get_files_recursively(directory_path)
  Dir["#{directory_path}/**/*"].each { |f| if File.file?(f) then puts "Getting #{f}".yellow.bold end }
  Dir["#{directory_path}/**/*"].filter_map { |f| if File.file?(f) then f end }
end

.get_json_from_text(text, index_json_to_return = 0) ⇒ Object



25
26
27
28
# File 'lib/five9tools/helpers.rb', line 25

def get_json_from_text(text, index_json_to_return=0)
  json_re = /[\[{](?>[^\[\]{}]+|\g<0>?)*[\]}]/m
  text.scan(json_re)[index_json_to_return]
end

.gunzip(text) ⇒ Object



30
31
32
33
# File 'lib/five9tools/helpers.rb', line 30

def gunzip(text)
  gz = Zlib::GzipReader.new(StringIO.new(text.to_s))
  gz.read
end

.gzip(text) ⇒ Object



35
36
37
38
39
# File 'lib/five9tools/helpers.rb', line 35

def gzip(text)
  gz = Zlib::GzipWriter.new(StringIO.new)
  gz << text
  gz.close.string
end

.json_to_csv(json_text, headers: true) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/five9tools/helpers.rb', line 62

def json_to_csv(json_text, headers: true)
  j = JSON.parse(json_text)
  case j
  when Hash
    if headers then headers = j.keys else headers = "" end
    CSV.generate do |csv|
      unless headers == "" then csv << headers end
      csv << j.values
    end
  when Array
    if headers then headers = j.first.keys else headers = "" end
    CSV.generate do |csv|
      unless headers == "" then csv << headers end
      j.each do |elem|
        csv << elem.values
      end
    end
  else
    raise StandardError.new <<-EOF
      Incompatible Json Format for CSV Conversion.\n
      Make sure to either use\n{\"k\": \"v\"} \n
      or\n
      [{\"k1\":\"v1\"}, {\"k2\", \"v2\"}]
      EOF
  end
end

.parse_xml(xml_string) ⇒ Object



45
46
47
# File 'lib/five9tools/helpers.rb', line 45

def parse_xml(xml_string)
  Nokogiri::XML(xml_string)
end

.pseudo_random_string(chars) ⇒ Object



89
90
91
# File 'lib/five9tools/helpers.rb', line 89

def pseudo_random_string(chars)
  (0...chars).map { ("a".."z").to_a[rand(26)] }.join
end

.to_base64(file_name) ⇒ Object



20
21
22
23
# File 'lib/five9tools/helpers.rb', line 20

def to_base64(file_name)
  data = File.open(file_name).read
  Base64.encode64(data)
end

.xml_to_hash(xml) ⇒ Object



49
50
51
# File 'lib/five9tools/helpers.rb', line 49

def xml_to_hash(xml)
  Hash.from_xml(xml)
end