Sha256: 5ec56fa86ec11ff59b8ea045c66dc1ab3c85d097909005d5d118dcfbbf168abc

Contents?: true

Size: 1.28 KB

Versions: 8

Compression:

Stored size: 1.28 KB

Contents

require_relative 'base'

module Swaggard
  module Swagger
    module Parameters
      class Query < Base

        def initialize(string)
          @in = 'query'
          parse(string)
        end

        def to_doc
          doc = super

          doc.merge!('enum' => @options) if @options.any?

          doc
        end

        private

        # Example: [Array]     status            Filter by status. (e.g. status[]=1&status[]=2&status[]=3)
        # Example: [Array]     status(required)  Filter by status. (e.g. status[]=1&status[]=2&status[]=3)
        # Example: [Integer]   media[media_type_id]                          ID of the desired media type.
        def parse(string)
          data_type, name, required, options_and_description = string.match(/\A\[(\S*)\]\s*([\w\[\]]*)(\(required\))?\s*(.*)\Z/).captures
          allow_multiple = name.gsub!('[]', '')

          options, description = options_and_description.match(/\A(\[.*\])?(.*)\Z/).captures
          options = options ? options.gsub(/\[?\]?\s?/, '').split(',') : []

          @name = name
          @description = description
          @data_type = data_type.downcase
          @is_required = required.present?
          @allow_multiple = allow_multiple.present?
          @options = options
        end

      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
swaggard-0.5.4 lib/swaggard/swagger/parameters/query.rb
swaggard-0.5.3 lib/swaggard/swagger/parameters/query.rb
swaggard-0.5.2 lib/swaggard/swagger/parameters/query.rb
swaggard-0.5.1 lib/swaggard/swagger/parameters/query.rb
swaggard-0.5.0 lib/swaggard/swagger/parameters/query.rb
swaggard-0.4.0 lib/swaggard/swagger/parameters/query.rb
swaggard-0.3.0 lib/swaggard/swagger/parameters/query.rb
swaggard-0.2.1 lib/swaggard/swagger/parameters/query.rb