class SafeParser

Whiltelist based hash string parser

Constants

UnsafeError
VERSION

Attributes

string[R]

Public Class Methods

new(string) click to toggle source
# File lib/safe_parser.rb, line 10
def initialize(string)
  @string = string
end

Public Instance Methods

safe_load() click to toggle source
# File lib/safe_parser.rb, line 14
def safe_load
  parse(root_expression)
end

Private Instance Methods

parse(expression) click to toggle source
# File lib/safe_parser.rb, line 20
def parse(expression)
  case expression.head
  when :hash
    Hash[*parse_into_array(expression.values)]
  when :array
    parse_into_array(expression.values)
  when :true
    true
  when :false
    false
  when :nil
    nil
  when :lit, :str
    expression.value
  else
    raise UnsafeError, "#{ string } is a bad hash"
  end
end
parse_into_array(expression) click to toggle source
# File lib/safe_parser.rb, line 43
def parse_into_array(expression)
  expression.map { |child_expression| parse(child_expression) }
end
root_expression() click to toggle source
# File lib/safe_parser.rb, line 39
def root_expression
  @root_expression ||= RubyParser.new.parse(string)
end