class Crate::Raw
Constants
- REPLACE
Attributes
options[R]
Public Class Methods
new(raw, **options)
click to toggle source
# File lib/crate/raw.rb, line 71 def initialize(raw, **options) @options = { logger: Logger.new(nil), severity: Logger::WARN, **options } @raw = symbolize(make_hash(raw)) end
Public Instance Methods
gsub(raw)
click to toggle source
# File lib/crate/raw.rb, line 44 def gsub(raw) gsub_(raw).tap do |replaced| log_if_changed(raw, replaced) log_if_harmful(replaced) end end
gsub_(raw)
click to toggle source
# File lib/crate/raw.rb, line 15 def gsub_(raw) raw.dup.tap do |raw_| REPLACE.each do |pattern, replacement| raw_.gsub!(pattern, replacement) end end end
log(&block)
click to toggle source
# File lib/crate/raw.rb, line 23 def log(&block) options[:logger].add(options[:severity], &block) end
log_if_changed(raw, replaced)
click to toggle source
# File lib/crate/raw.rb, line 27 def log_if_changed(raw, replaced) return if raw == replaced log do "#{options[:fullname]}: Harmful characters are changed " \ "from: #{raw.inspect} to: #{replaced.inspect}" end end
log_if_harmful(replaced)
click to toggle source
# File lib/crate/raw.rb, line 35 def log_if_harmful(replaced) bytes = replaced.each_char.reject(&:ascii_only?).map(&:bytes) return if bytes.empty? log do "#{options[:fullname]}: Harmful characters are still remained: " \ "#{bytes.inspect}" end end
make_hash(raw)
click to toggle source
# File lib/crate/raw.rb, line 60 def make_hash(raw) case raw when String JSON.parse(gsub(raw), symbolize_names: true) when Hash raw else raise end end
symbolize(object)
click to toggle source
# File lib/crate/raw.rb, line 51 def symbolize(object) case object when Hash object.map { |key, value| [key.to_sym, symbolize(value)] }.to_h else object end end
to_hash()
click to toggle source
# File lib/crate/raw.rb, line 76 def to_hash @raw end