Utils   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 34
rs 10
c 1
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A print_yaml() 0 6 1
B struct_to_hash() 0 7 6
A print_ruby() 0 3 1
A diff() 0 7 1
A print_json() 0 3 1
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