Class: Canon::Formatters::YamlFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/canon/formatters/yaml_formatter.rb

Overview

YAML formatter for canonicalization

Class Method Summary collapse

Class Method Details

.format(yaml) ⇒ Object



9
10
11
12
# File 'lib/canon/formatters/yaml_formatter.rb', line 9

def self.format(yaml)
  parsed = parse(yaml)
  sort_yaml_keys(parsed).to_yaml
end

.parse(yaml) ⇒ Object



14
15
16
# File 'lib/canon/formatters/yaml_formatter.rb', line 14

def self.parse(yaml)
  YAML.safe_load(yaml)
end

.sort_yaml_keys(obj) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/canon/formatters/yaml_formatter.rb', line 18

def self.sort_yaml_keys(obj)
  case obj
  when Hash
    obj.transform_values { |v| sort_yaml_keys(v) }
      .sort.to_h
  when Array
    obj.map { |item| sort_yaml_keys(item) }
  else
    obj
  end
end