Module: RestDSL::DSLExtensions::FromFile
- Defined in:
- lib/rest_dsl/dsl.rb
Overview
Adds a DSL method for parsing information from a file, parser list can be overridden by redefining file_parsers If no parser is designed, for the file extension, loads the file as plain text
Instance Method Summary collapse
Instance Method Details
#file_parsers ⇒ Object
20 21 22 23 24 25 |
# File 'lib/rest_dsl/dsl.rb', line 20 def file_parsers { %w[.json] => JSON, %w[.yml .yaml] => Psych } end |
#from_file(file_name) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/rest_dsl/dsl.rb', line 27 def from_file(file_name) parser = file_parsers.find{|key, _| key.any? {|file_type| file_name.include? file_type}}&.[](1) result = if parser.eql?(Psych) parser.load_file(file_name) elsif parser.eql?(JSON) parser.parse(File.read(file_name)) end result ||= File.new(file_name) result rescue Errno::ENOENT => e e. << " relative to directory #{Dir.pwd}" raise e end |