Class: ActsAsDoc::ResponseParser

Inherits:
Ripper::SexpBuilder
  • Object
show all
Defined in:
lib/acts_as_doc/response_parser.rb

Constant Summary collapse

SUPPORT_TYPES =
%w[
  string
  number
  integer
  boolean
  array
  object
].freeze
REF_FLAG =
'$'
TYPE_MAPPER =
{
  integer: 'integer',
  string: 'string',
  decimal: 'number',
  text: 'string',
  geography: 'string',
  json: 'object',
  jsonb: 'object',
  array: 'array'
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.props_recursion!(hash, name, type, desc = '', klass = nil) ⇒ Hash

Make props recursion

rubocop:disable all

Examples:

>> ActsAsDoc::ResponseParser.props_recursion!({}, '$a.$b.c', 'c', 'd')
# => {'a' => }

Returns:

  • (Hash)

    处理过的hash



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/acts_as_doc/response_parser.rb', line 46

def self.props_recursion!(hash, name, type, desc = '', klass = nil)
  arr = name.split('.')
  name = arr.shift.sub(/^\$/, '')

  hash[name] = {} unless hash.key?(name)

  if arr.empty?
    if klass && (matches = desc.match(/^\(([a-z,\s_A-Z]+)\)$/))
      columns = Object.const_get(klass).columns.map do |column|
        column_type = if column.respond_to?(:array?) && column.array?
                        'array'
                      else
                        TYPE_MAPPER[column.type]
                      end
        [ column.name.to_s, [column_type, column.comment] ]
      end.to_h

      matches[1].split(',').each do |attr_name|
        attr_name.strip!
        c_type, c_desc = columns[attr_name]
        self.props_recursion!(hash, "$#{name}.#{attr_name}", c_type, c_desc)
      end
    else
      hash[name].merge!(type: type, description: desc)
    end
  else
    nest_hash = hash[name]
    nest_hash[:properties] = {} unless nest_hash.key?(:properties)
    self.props_recursion!(nest_hash[:properties], arr.join('.'), type, desc, klass)
  end

  hash
end

Instance Method Details

#on_comment(token) ⇒ Object

处理注释

Parameters:

  • token (String)

    one line of comments



33
34
35
36
# File 'lib/acts_as_doc/response_parser.rb', line 33

def on_comment(token)
  @comments ||= []
  @comments << token[1..]
end

#schemaHash

rubocop:disable all

Returns:

  • (Hash)

    response schema



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/acts_as_doc/response_parser.rb', line 82

def schema
  schema = {}
  @comments.each do |comment|
    comment.strip!
    # Only need to deal with @success and @prop
    next unless comment =~ /@(success|prop)/

    arr = comment.split
    tag, type, name = arr[..2]

    if tag == '@prop'
      desc = arr[3..] ? arr[3..].join(' ') : ''

      matches = type.match(/\[(?<type>[a-zA-Z<>:]+)\]/)
      type = matches ? matches[:type] : 'string'

      klass_matches = type.match(/(?<type>[a-zA-Z]+)<(?<klass>[a-zA-Z:]+)>/)
      klass = nil
      if klass_matches
        type = SUPPORT_TYPES.include?(klass_matches[:type]) ? klass_matches[:type] : 'string'
        klass = klass_matches[:klass]
      end

      if name.start_with?(':')
        name = name.sub(/^:/, '')
        schema[name] = { type: type, description: desc }
      end

      if name.start_with?(REF_FLAG)
        self.class.props_recursion!(schema, name, type, desc, klass)
      end
    end
  end
  schema
end