Sha256: d783b0f0bd71b5f03fc658d2429f112d9c74d4776b8ba88e28a58daace765066

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

require "fileutils"
require "json"

module FileHelpers
  FakeResponse = Struct.new(:body)

  def create_schema(name, json)
    path = File.join(JsonMatchers.schema_root, "#{name}.json")

    File.open(path, "w") do |file|
      case json
      when NilClass, String
        file.write(json.to_s)
      else
        file.write(JSON.generate(json))
      end
    end
  end

  def response_for(json)
    response_body = case json
                    when String, NilClass
                      json.to_s
                    else
                      json.to_json
                    end
    FakeResponse.new(response_body)
  end

  def setup_fixtures(*pathnames)
    JSON::Validator.clear_cache
    original_schema_root = JsonMatchers.schema_root

    JsonMatchers.schema_root = File.join(*pathnames)
    FileUtils.mkdir_p(JsonMatchers.schema_root)

    original_schema_root
  end

  def teardown_fixtures(original_schema_root)
    FileUtils.rm_rf(JsonMatchers.schema_root)
    JsonMatchers.schema_root = original_schema_root
  end

  def ensure_fixtures(*pathnames)
    original_schema_root = setup_fixtures(*pathnames)

    yield

    teardown_fixtures(original_schema_root)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
json_matchers-0.7.3 spec/support/file_helpers.rb