class Krane::BindingsParser
Public Class Methods
new(initial_string = nil)
click to toggle source
# File lib/krane/bindings_parser.rb, line 12 def initialize(initial_string = nil) @raw_bindings = Array(initial_string) end
parse(string)
click to toggle source
# File lib/krane/bindings_parser.rb, line 8 def self.parse(string) new(string).parse end
Public Instance Methods
add(string)
click to toggle source
# File lib/krane/bindings_parser.rb, line 16 def add(string) @raw_bindings << string end
parse()
click to toggle source
# File lib/krane/bindings_parser.rb, line 20 def parse result = {} @raw_bindings.each do |string| bindings = parse_file(string) || parse_json(string) || parse_csv(string) unless bindings raise ArgumentError, "Failed to parse bindings." end result.deep_merge!(bindings) end result end
Private Instance Methods
parse_csv(string)
click to toggle source
# File lib/krane/bindings_parser.rb, line 67 def parse_csv(string) lines = CSV.parse(string) bindings = {} lines.each do |line| line.each do |binding| key, value = binding.split('=', 2) if key.blank? raise ArgumentError, "key is blank" end bindings[key] = value end end bindings rescue CSV::MalformedCSVError nil end
parse_file(string)
click to toggle source
# File lib/krane/bindings_parser.rb, line 34 def parse_file(string) return unless string =~ /\A@/ begin file_path = string.gsub(/\A@/, '') case File.extname(file_path) when '.json' bindings = parse_json(File.read(file_path)) when '.yaml', '.yml' bindings = YAML.safe_load(File.read(file_path), [], [], true, file_path) else raise ArgumentError, "Supplied file does not appear to be JSON or YAML" end bindings rescue Errno::ENOENT raise ArgumentError, "Supplied file does not exist: #{string}" end end
parse_json(string)
click to toggle source
# File lib/krane/bindings_parser.rb, line 55 def parse_json(string) bindings = JSON.parse(string) unless bindings.is_a?(Hash) raise ArgumentError, "Expected JSON data to be a hash." end bindings rescue JSON::ParserError nil end