Utils.struct_to_hash()   B
last analyzed

Complexity

Conditions 6

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
dl 0
loc 7
rs 8
c 1
b 0
f 0
1
module Rapis
2
  class Utils
3
    class << self
4
      def struct_to_hash(val)
5
        val = val.to_h if val.is_a?(Struct) || val.is_a?(OpenStruct)
6
        val = val.to_s if val.is_a?(Symbol)
7
        val = Hash[val.map { |k, v| [k.to_s, struct_to_hash(v)] }] if val.is_a?(Hash)
8
        val = val.map! { |v| struct_to_hash(v) } if val.is_a?(Array)
9
        val
10
      end
11
12
      def diff(hash1, hash2)
13
        Diffy::Diff.new(
14
          JSON.pretty_generate(hash1),
15
          JSON.pretty_generate(hash2),
16
          diff: '-u'
17
        ).to_s(:color)
18
      end
19
20
      def print_yaml(yaml)
21
        CodeRay::Encoders::Terminal::TOKEN_COLORS[:key] = {
22
          self: "\e[32m"
23
        }
24
        puts CodeRay.scan(yaml, :yaml).terminal
25
      end
26
27
      def print_ruby(ruby)
28
        puts CodeRay.scan(ruby, :ruby).terminal
29
      end
30
31
      def print_json(json)
32
        puts CodeRay.scan(json, :json).terminal
33
      end
34
    end
35
  end
36
end
37