Sha256: 7333b01aea0fa526cfbdefde0e292c9544edf1e91d6c57b6d361ff9991742422

Contents?: true

Size: 1.32 KB

Versions: 2

Compression:

Stored size: 1.32 KB

Contents

require 'json'
require 'psych'

module RestDSL

  ##
  # A collection of DSL extensions for use with various worlds to allow the use of some simpler
  # dsl language in them.
  #
  # For rspec, in spec helper add config.extend RestDSL::DSLExtensions::<<extension>>
  # In cucumber World(RestDSL::DSLExtensions::<<extension>>)
  # Each extension can also be extended onto a class like normal to use it in that class
  module DSLExtensions

    ##
    # 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
    module FromFile

      def file_parsers
        {
          %w[.json] => JSON,
          %w[.yml .yaml] => Psych
        }
      end

      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.message << " relative to directory #{Dir.pwd}"
        raise e
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rest_dsl-0.1.10 lib/rest_dsl/dsl.rb
rest_dsl-0.1.8 lib/rest_dsl/dsl.rb