Class: Rapis::Utils
- Inherits:
-
Object
- Object
- Rapis::Utils
- Defined in:
- lib/rapis/utils.rb
Class Method Summary collapse
- .diff(hash1, hash2, opts = {}) ⇒ Object
- .print_json(json) ⇒ Object
- .print_ruby(ruby) ⇒ Object
- .print_yaml(yaml) ⇒ Object
- .struct_to_hash(val) ⇒ Object
Class Method Details
.diff(hash1, hash2, opts = {}) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/rapis/utils.rb', line 15 def self.diff(hash1, hash2, opts={}) Diffy::Diff.new( JSON.pretty_generate(hash1), JSON.pretty_generate(hash2), :diff => '-u' ).to_s(:color) end |
.print_json(json) ⇒ Object
34 35 36 |
# File 'lib/rapis/utils.rb', line 34 def self.print_json(json) puts CodeRay.scan(json, :json).terminal end |
.print_ruby(ruby) ⇒ Object
30 31 32 |
# File 'lib/rapis/utils.rb', line 30 def self.print_ruby(ruby) puts CodeRay.scan(ruby, :ruby).terminal end |
.print_yaml(yaml) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/rapis/utils.rb', line 23 def self.print_yaml(yaml) CodeRay::Encoders::Terminal::TOKEN_COLORS[:key] = { self: "\e[32m", } puts CodeRay.scan(yaml, :yaml).terminal end |
.struct_to_hash(val) ⇒ Object
7 8 9 10 11 12 13 |
# File 'lib/rapis/utils.rb', line 7 def self.struct_to_hash(val) val = val.to_h if val.kind_of?(Struct) val = val.to_s if val.kind_of?(Symbol) val = Hash[val.map { |k,v| [k.to_s,struct_to_hash(v)] }] if val.kind_of?(Hash) val = val.map! { |v| struct_to_hash(v) } if val.kind_of?(Array) val end |