Sha256: a107b38f9fe4d5c9b97e8c59e4089b387c39975bb3f9b67181a1c34ec74b63bd

Contents?: true

Size: 1.51 KB

Versions: 15

Compression:

Stored size: 1.51 KB

Contents

require 'ostruct'

module Prmd
  class Link
    def initialize(link_schema)
      @link_schema = link_schema 
    end

    def required_and_optional_parameters
      @params = {required: {}, optional: {} }
      recurse_properties(Schema.new(@link_schema["schema"]), "")
      [@params[:required], @params[:optional]]
    end

    private

    def recurse_properties(schema, prefix ="", parent_required= false )
      return unless schema.has_properties?

      schema.properties.keys.each do |prop_name|
        prop = schema.properties[prop_name]
        pref = "#{prefix}#{prop_name}"
        required = parent_required || schema.property_is_required?(prop_name)

        handle_property(prop, pref, required)
      end
    end

    def handle_property(property, prefix, required = false)
      case
      when property_is_object?(property["type"])
        recurse_properties(Schema.new(property), "#{prefix}:", required)
      else
        categorize_parameter(prefix, property, required)
      end
    end

    def property_is_object?(type)
      return false unless type
      type == "object" || type.include?("object")
    end

    def categorize_parameter(name, param,  required=false)
      @params[(required ? :required : :optional)][name] = param
    end

    class Schema < OpenStruct
      def has_properties?
        self.properties && !self.properties.empty?
      end

      def property_is_required?(property_name)
        return false unless required
        return required.include?(property_name)
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
prmd-0.14.0 lib/prmd/link.rb
prmd-0.13.0 lib/prmd/link.rb
prmd-0.12.0 lib/prmd/link.rb
prmd-0.11.11 lib/prmd/link.rb
prmd-0.11.10 lib/prmd/link.rb
prmd-0.11.9 lib/prmd/link.rb
prmd-0.11.8 lib/prmd/link.rb
prmd-0.11.7 lib/prmd/link.rb
prmd-0.11.6 lib/prmd/link.rb
prmd-0.11.5 lib/prmd/link.rb
prmd-0.11.4 lib/prmd/link.rb
prmd-0.11.3 lib/prmd/link.rb
prmd-0.11.2 lib/prmd/link.rb
prmd-0.11.1 lib/prmd/link.rb
prmd-0.11.0 lib/prmd/link.rb