Sha256: 6d490052f3c6d79539cdd77f223417232a3694c48f661f03728b28a7d6559197

Contents?: true

Size: 873 Bytes

Versions: 7

Compression:

Stored size: 873 Bytes

Contents

# frozen_string_literal: true

require "psych"
require "json"

module Openapi3Parser
  class SourceInput
    class StringParser
      def self.call(input, filename = nil)
        new(input, filename).call
      end

      def initialize(input, filename)
        @input = input
        @filename = filename
      end

      def call
        json? ? parse_json : parse_yaml
      end

      private_class_method :new

      private

      attr_reader :input, :filename

      def json?
        return false if filename && ::File.extname(filename) == ".yaml"

        json_filename = filename && ::File.extname(filename) == ".json"
        json_filename || input.strip[0] == "{"
      end

      def parse_json
        JSON.parse(input)
      end

      def parse_yaml
        Psych.safe_load(input, permitted_classes: [Date, Time], aliases: true)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
openapi3_parser-0.9.2 lib/openapi3_parser/source_input/string_parser.rb
mountapi-0.11.1 vendor/bundle/ruby/2.7.0/gems/openapi3_parser-0.9.1/lib/openapi3_parser/source_input/string_parser.rb
openapi3_parser-0.9.1 lib/openapi3_parser/source_input/string_parser.rb
openapi3_parser-0.9.0 lib/openapi3_parser/source_input/string_parser.rb
openapi3_parser-0.8.2 lib/openapi3_parser/source_input/string_parser.rb
openapi3_parser-0.8.1 lib/openapi3_parser/source_input/string_parser.rb
openapi3_parser-0.8.0 lib/openapi3_parser/source_input/string_parser.rb