Module: YamlHelpers

Defined in:
lib/m1_api/yaml_helpers.rb

Class Method Summary collapse

Class Method Details

.call_api_from_config(configs, api, data = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/m1_api/yaml_helpers.rb', line 63

def call_api_from_config(configs, api, data = {})
  config = configs[api].dup
  raise "no api defined for #{api}" unless config
  context = config.merge data
  parsed_config = replace_dynamic_hash(context)
  params = [parsed_config[:method], parsed_config[:url], parsed_config[:body], parsed_config[:headers]]
  params.delete(nil)
  res = RestClient.send(*params)
  { code: res.code, body: JSON.parse(res.body) }
rescue Exception => e
  return { code: res.code, body: res.body } if res
  puts "failed to call api for api #{api}: #{e}"
end

.call_api_from_yml(config_file, api, data = {}) ⇒ Object



77
78
79
80
# File 'lib/m1_api/yaml_helpers.rb', line 77

def call_api_from_yml(config_file, api, data = {})
  configs = load_yaml(config_file)
  call_api_from_config(configs, api, data)
end

.load_yaml(file_path) ⇒ Object



9
10
11
12
13
# File 'lib/m1_api/yaml_helpers.rb', line 9

def load_yaml(file_path)
  YAML.load(ERB.new(File.read(file_path)).result) || {}
rescue SystemCallError
  raise "Could not load file: '#{file_path}"
end

.replace_dynamic_array(array, context) ⇒ Object

need something to deal with uri encode



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/m1_api/yaml_helpers.rb', line 34

def replace_dynamic_array(array, context)
  raise 'input is not a array' unless array.is_a?(Array)
  dup = array.clone
  dup.each_with_index do |value, index|
    if value.is_a?(String)
      dup[index] = replace_dynamic_string(value, context)
    elsif value.is_a?(Array)
      dup[index] = replace_dynamic_array(value, context)
    elsif value.is_a?(Hash)
      dup[index] = replace_dynamic_hash(value, context)
    end
  end
  dup.join
end

.replace_dynamic_hash(hash, context = hash) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/m1_api/yaml_helpers.rb', line 49

def replace_dynamic_hash(hash, context = hash)
  raise 'input is not a hash' unless hash.is_a?(Hash)
  hash.each do |key, value|
    if value.is_a?(String)
      hash[key] = replace_dynamic_string(value, context)
    elsif value.is_a?(Array)
      hash[key] = replace_dynamic_array(value, context)
    elsif value.is_a?(Hash)
      hash[key] = replace_dynamic_hash(value, context)
    end
  end
  hash
end

.replace_dynamic_string(string, context) ⇒ Object

might have to convert everything to sym first



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/m1_api/yaml_helpers.rb', line 16

def replace_dynamic_string(string, context)
  raise 'input is not a string' unless string.is_a?(String)
  replace_targets = string.split('>>>').map { |target| target.match(/<<<.*/).to_s }
  replace_targets.each do |target|
    key = target.match(/<<<(.*)/)
    if key
      temp_value = context
      key[1].split(' ').each do |current_key|
        raise "no value '#{current_key}' defined in  context" unless temp_value.key?(current_key.to_sym)
        temp_value = temp_value[current_key.to_sym]
      end
      string = string.gsub("#{target}>>>", temp_value)
    end
  end
  string
end