Sha256: bca20fcbd6e6ca8b73176fcef36086bff34bebcbba946b1c76d778cd275e1825

Contents?: true

Size: 1.37 KB

Versions: 17

Compression:

Stored size: 1.37 KB

Contents

# encoding: utf-8

require 'yaml'

module Apiary
  module Helpers
    def api_description_source_path(path)
      raise "Invalid path #{path}" unless File.exist? path
      return path if File.file? path
      source_path = choose_one(path)
      return source_path unless source_path.nil?
      raise 'No API Description Document found'
    end

    def api_description_source(path)
      source_path = api_description_source_path(path)
      source = nil
      File.open(source_path, 'r:bom|utf-8') { |file| source = file.read }
      source
    end

    def convert_from_json(add)
      JSON.parse(add).to_yaml
    rescue JSON::ParserError => e
      abort "Unable to convert input document to yaml: #{e.message.lines.first}"
    end

    protected

    def choose_one(path)
      apib_path = api_blueprint(path)
      swagger_path = swagger(path)

      if apib_path && swagger_path
        warn 'WARNING: Both apiary.apib and swagger.yaml are present. The apiary.apib file will be used. To override this selection specify path to desired file'
      end

      apib_path || swagger_path
    end

    def api_blueprint(path)
      source_path = File.join(path, 'apiary.apib')
      return source_path if File.exist? source_path
      nil
    end

    def swagger(path)
      source_path = File.join(path, 'swagger.yaml')
      return source_path if File.exist? source_path
      nil
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
apiaryio-0.17.0 lib/apiary/helpers.rb
apiaryio-0.16.1 lib/apiary/helpers.rb
apiaryio-0.16.0 lib/apiary/helpers.rb
apiaryio-0.15.2 lib/apiary/helpers.rb
apiaryio-0.15.1 lib/apiary/helpers.rb
apiaryio-0.15.0 lib/apiary/helpers.rb
apiaryio-0.14.1 lib/apiary/helpers.rb
apiaryio-0.14.0 lib/apiary/helpers.rb
apiaryio-0.13.0 lib/apiary/helpers.rb
apiaryio-0.12.0 lib/apiary/helpers.rb
apiaryio-0.11.1 lib/apiary/helpers.rb
apiaryio-0.11.0 lib/apiary/helpers.rb
apiaryio-0.10.2 lib/apiary/helpers.rb
apiaryio-0.10.1 lib/apiary/helpers.rb
apiaryio-0.9.1 lib/apiary/helpers.rb
apiaryio-0.9.0 lib/apiary/helpers.rb
apiaryio-0.8.1 lib/apiary/helpers.rb