class Tolq::Parsers::Yaml::Unparser

Unparses a tolq response to YAML suitable for an I18n application

Public Instance Methods

unparse(tolq_response) click to toggle source

Unparses the tolq response

@param tolq_response [Hash] A parsed response from our api @return [String] A yaml formatted string

# File lib/yaml/unparser.rb, line 8
def unparse(tolq_response)
  translations = tolq_response['translations']
  split = split_keys(translations.values.first)
  { translations.keys.first => split }.to_yaml
end

Private Instance Methods

deep_merge(original, second) click to toggle source
# File lib/yaml/unparser.rb, line 28
def deep_merge(original, second)
  merger = Proc.new { |k, x, y| x.is_a?(Hash) ? x.merge(y, &merger) : x || y }
  original.merge(second, &merger)
end
split_keys(translations) click to toggle source

Splits a key like 'a.b.c' into { 'a' => 'b' => 'c' }

Basically for each key, we split the keys and build the hash bottom up. Then we pass in the result to the next key.

# File lib/yaml/unparser.rb, line 21
def split_keys(translations)
  translations.inject({}) do |acc, (k, v)|
    keys = k.split('.')
    deep_merge(acc, keys.reverse.inject(v) { |a, key| { key => a } })
  end
end